Skip to content

Commit e9e7000

Browse files
authored
Add options parameter, add ignoreAutomaticGrants option (#11)
* feat: add option to ignore automatic grants * feat: merge `transform` into `userOptions`, add overloads to allow for any parameter combination This allows for either providing just a transform function, just an options object with an (optional) transform function, or both as separate parameters. All parameters being optional! * feat: remove 2-parameter overload You can now either only pass a transform function, an object with a transform function and other options, or nothing at all. As requested in a code review.
1 parent bafb758 commit e9e7000

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ import { collectGrants, getMetadata } from './util';
55

66
const suffix = '?userscript-metadata';
77

8-
export default (transform?: (metadata: string) => string): Plugin => {
8+
type TransformFn = (metadata: string) => string;
9+
10+
export interface UserscriptMetaOptions {
11+
transform?: TransformFn;
12+
ignoreAutomaticGrants?: string[];
13+
}
14+
15+
function userscriptPlugin(transform?: TransformFn): Plugin;
16+
function userscriptPlugin(options?: UserscriptMetaOptions): Plugin;
17+
18+
function userscriptPlugin(
19+
transformOrUserOptions?: TransformFn | UserscriptMetaOptions
20+
): Plugin {
21+
const userOptions = typeof transformOrUserOptions === 'function'
22+
? { transform: transformOrUserOptions }
23+
: transformOrUserOptions;
24+
925
const metadataMap = new Map();
1026
const grantMap = new Map();
1127
return {
@@ -45,12 +61,16 @@ export default (transform?: (metadata: string) => string): Plugin => {
4561
const grantSetPerFile = grantMap.get(id);
4662
if (grantSetPerFile) {
4763
for (const item of grantSetPerFile) {
64+
if (userOptions.ignoreAutomaticGrants?.includes(item)) {
65+
continue;
66+
}
67+
4868
grantSet.add(item);
4969
}
5070
}
5171
}
5272
metadata = getMetadata(metadata, grantSet);
53-
if (transform) metadata = transform(metadata);
73+
if (userOptions.transform) metadata = userOptions.transform(metadata);
5474
const s = new MagicString(code);
5575
s.prepend(`${metadata}\n\n`);
5676
return {
@@ -60,3 +80,5 @@ export default (transform?: (metadata: string) => string): Plugin => {
6080
},
6181
};
6282
};
83+
84+
export default userscriptPlugin;

0 commit comments

Comments
 (0)