vite-userscript-plugin 1.11.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,187 +1,271 @@
1
- import { EsbuildTransformOptions, PluginOption } from 'vite';
2
-
3
- declare const GM: readonly ["setValue", "getValue", "deleteValue", "listValues", "setClipboard", "addStyle", "addElement", "addValueChangeListener", "removeValueChangeListener", "registerMenuCommand", "unregisterMenuCommand", "download", "getTab", "getTabs", "saveTab", "openInTab", "notification", "getResourceURL", "getResourceText", "xmlhttpRequest", "log", "info"];
1
+ import { Plugin } from "vite";
2
+ //#region src/grants/catalog.d.ts
3
+ declare const GM: readonly ["setValue", "getValue", "deleteValue", "listValues", "setValues", "getValues", "deleteValues", "setClipboard", "addStyle", "addElement", "addValueChangeListener", "removeValueChangeListener", "registerMenuCommand", "unregisterMenuCommand", "download", "getTab", "getTabs", "saveTab", "openInTab", "notification", "getResourceURL", "getResourceText", "xmlhttpRequest", "webRequest", "cookie", "audio", "log", "info"];
4
4
  declare const GMwindow: readonly ["unsafeWindow", "window.onurlchange", "window.focus", "window.close"];
5
-
5
+ declare const GM_DOT_ALIASES: readonly ["GM.xmlHttpRequest", "GM.getResourceUrl"];
6
+ type CatalogGrant = `GM_${(typeof GM)[number]}` | `GM.${(typeof GM)[number]}` | (typeof GMwindow)[number] | (typeof GM_DOT_ALIASES)[number];
7
+ //#endregion
8
+ //#region src/grants/types.d.ts
9
+ type Grants = CatalogGrant;
10
+ //#endregion
11
+ //#region src/types.d.ts
6
12
  type RunAt = 'document-start' | 'document-body' | 'document-end' | 'document-idle' | 'context-menu';
7
- type GMLiterals<T extends string> = [`GM_${T}` | `GM.${T}`];
8
- type GMWindow = (typeof GMwindow)[number];
9
- type Grants = GMWindow | GMLiterals<(typeof GM)[number]>[number];
10
13
  type HeaderConfig = {
11
- [property: string]: any;
12
- /**
13
- * @see https://www.tampermonkey.net/documentation.php#meta:name
14
- */
15
- name: string;
16
- /**
17
- * @see https://www.tampermonkey.net/documentation.php#meta:namespace
18
- */
19
- namespace?: string;
20
- /**
21
- * @see https://www.tampermonkey.net/documentation.php#meta:copyright
22
- */
23
- copyright?: string;
24
- /**
25
- * @see https://www.tampermonkey.net/documentation.php#meta:version
26
- */
27
- version: string;
28
- /**
29
- * @see https://www.tampermonkey.net/documentation.php#meta:description
30
- */
31
- description?: string;
32
- /**
33
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
34
- */
35
- icon?: string;
36
- /**
37
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
38
- */
39
- iconURL?: string;
40
- /**
41
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
42
- */
43
- defaulticon?: string;
44
- /**
45
- * @see https://www.tampermonkey.net/documentation.php#meta:icon64
46
- */
47
- icon64?: string;
48
- /**
49
- * @see https://www.tampermonkey.net/documentation.php#meta:icon64
50
- */
51
- icon64URL?: string;
52
- /**
53
- * @see https://www.tampermonkey.net/documentation.php#meta:grant
54
- */
55
- grant?: Grants[];
56
- /**
57
- * @see https://www.tampermonkey.net/documentation.php#meta:author
58
- */
59
- author?: string;
60
- /**
61
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
62
- */
63
- homepage?: string;
64
- /**
65
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
66
- */
67
- homepageURL?: string;
68
- /**
69
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
70
- */
71
- website?: string;
72
- /**
73
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
74
- */
75
- source?: string;
76
- /**
77
- * @see https://www.tampermonkey.net/documentation.phpmeta:antifeature
78
- */
79
- antifeature?: [type: string, description: string][];
80
- /**
81
- * @see https://www.tampermonkey.net/documentation.php#meta:require
82
- */
83
- require?: string[] | string;
84
- /**
85
- * @see https://www.tampermonkey.net/documentation.php#meta:resource
86
- */
87
- resource?: [key: string, value: string][];
88
- /**
89
- * @see https://www.tampermonkey.net/documentation.php#meta:include
90
- */
91
- include?: string[] | string;
92
- /**
93
- * @see https://www.tampermonkey.net/documentation.php#meta:match
94
- * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
95
- */
96
- match: string[] | string;
97
- /**
98
- * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
99
- */
100
- 'exclude-match'?: string[] | string;
101
- /**
102
- * @see https://www.tampermonkey.net/documentation.php#meta:exclude
103
- */
104
- exclude?: string[] | string;
105
- /**
106
- * @see https://www.tampermonkey.net/documentation.php#meta:run_at
107
- */
108
- 'run-at'?: RunAt;
109
- /**
110
- * @see https://www.tampermonkey.net/documentation.phpmeta:sandbox
111
- */
112
- sandbox?: string;
113
- /**
114
- * @see https://www.tampermonkey.net/documentation.php#meta:connect
115
- */
116
- connect?: string[] | string;
117
- /**
118
- * @see https://www.tampermonkey.net/documentation.php#meta:noframes
119
- */
120
- noframes?: boolean;
121
- /**
122
- * @see https://www.tampermonkey.net/documentation.php#meta:updateURL
123
- */
124
- updateURL?: string;
125
- /**
126
- * @see https://www.tampermonkey.net/documentation.php#meta:downloadURL
127
- */
128
- downloadURL?: string;
129
- /**
130
- * @see https://www.tampermonkey.net/documentation.php#meta:supportURL
131
- */
132
- supportURL?: string;
133
- /**
134
- * @see https://www.tampermonkey.net/documentation.php#meta:webRequest
135
- */
136
- webRequest?: string[];
137
- /**
138
- * @see https://www.tampermonkey.net/documentation.php#meta:unwrap
139
- */
140
- unwrap?: boolean;
14
+ [property: string]: any;
15
+ /**
16
+ * @see https://www.tampermonkey.net/documentation.php#meta:name
17
+ */
18
+ 'name': string;
19
+ /**
20
+ * @see https://www.tampermonkey.net/documentation.php#meta:namespace
21
+ */
22
+ 'namespace'?: string;
23
+ /**
24
+ * @see https://www.tampermonkey.net/documentation.php#meta:copyright
25
+ */
26
+ 'copyright'?: string;
27
+ /**
28
+ * @see https://www.tampermonkey.net/documentation.php#meta:version
29
+ */
30
+ 'version': string;
31
+ /**
32
+ * @see https://www.tampermonkey.net/documentation.php#meta:description
33
+ */
34
+ 'description'?: string;
35
+ /**
36
+ * @see https://www.tampermonkey.net/documentation.php#meta:icon
37
+ */
38
+ 'icon'?: string;
39
+ /**
40
+ * @see https://www.tampermonkey.net/documentation.php#meta:icon
41
+ */
42
+ 'iconURL'?: string;
43
+ /**
44
+ * @see https://www.tampermonkey.net/documentation.php#meta:icon
45
+ */
46
+ 'defaulticon'?: string;
47
+ /**
48
+ * @see https://www.tampermonkey.net/documentation.php#meta:icon64
49
+ */
50
+ 'icon64'?: string;
51
+ /**
52
+ * @see https://www.tampermonkey.net/documentation.php#meta:icon64
53
+ */
54
+ 'icon64URL'?: string;
55
+ /**
56
+ * `@grant none` disables GM APIs. Do not mix with auto-detected grants.
57
+ *
58
+ * @see https://www.tampermonkey.net/documentation.php#meta:grant
59
+ */
60
+ 'grant'?: Grants[] | 'none';
61
+ /**
62
+ * @see https://www.tampermonkey.net/documentation.php#meta:author
63
+ */
64
+ 'author'?: string;
65
+ /**
66
+ * @see https://www.tampermonkey.net/documentation.php#meta:homepage
67
+ */
68
+ 'homepage'?: string;
69
+ /**
70
+ * @see https://www.tampermonkey.net/documentation.php#meta:homepage
71
+ */
72
+ 'homepageURL'?: string;
73
+ /**
74
+ * @see https://www.tampermonkey.net/documentation.php#meta:homepage
75
+ */
76
+ 'website'?: string;
77
+ /**
78
+ * @see https://www.tampermonkey.net/documentation.php#meta:homepage
79
+ */
80
+ 'source'?: string;
81
+ /**
82
+ * @see https://www.tampermonkey.net/documentation.php#meta:antifeature
83
+ */
84
+ 'antifeature'?: [type: string, description: string][];
85
+ /**
86
+ * @see https://www.tampermonkey.net/documentation.php#meta:require
87
+ */
88
+ 'require'?: string[] | string;
89
+ /**
90
+ * @see https://www.tampermonkey.net/documentation.php#meta:resource
91
+ */
92
+ 'resource'?: [key: string, value: string][];
93
+ /**
94
+ * @see https://www.tampermonkey.net/documentation.php#meta:include
95
+ */
96
+ 'include'?: string[] | string;
97
+ /**
98
+ * @see https://www.tampermonkey.net/documentation.php#meta:match
99
+ * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
100
+ */
101
+ 'match': string[] | string;
102
+ /**
103
+ * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
104
+ */
105
+ 'exclude-match'?: string[] | string;
106
+ /**
107
+ * @see https://www.tampermonkey.net/documentation.php#meta:exclude
108
+ */
109
+ 'exclude'?: string[] | string;
110
+ /**
111
+ * @see https://www.tampermonkey.net/documentation.php#meta:run_at
112
+ */
113
+ 'run-at'?: RunAt;
114
+ /**
115
+ * @see https://www.tampermonkey.net/documentation.php#meta:sandbox
116
+ */
117
+ 'sandbox'?: string;
118
+ /**
119
+ * @see https://www.tampermonkey.net/documentation.php#meta:connect
120
+ */
121
+ 'connect'?: string[] | string;
122
+ /**
123
+ * @see https://www.tampermonkey.net/documentation.php#meta:noframes
124
+ */
125
+ 'noframes'?: boolean;
126
+ /**
127
+ * @see https://www.tampermonkey.net/documentation.php#meta:updateURL
128
+ */
129
+ 'updateURL'?: string;
130
+ /**
131
+ * @see https://www.tampermonkey.net/documentation.php#meta:downloadURL
132
+ */
133
+ 'downloadURL'?: string;
134
+ /**
135
+ * @see https://www.tampermonkey.net/documentation.php#meta:supportURL
136
+ */
137
+ 'supportURL'?: string;
138
+ /**
139
+ * @see https://www.tampermonkey.net/documentation.php#meta:webRequest
140
+ */
141
+ 'webRequest'?: string[];
142
+ /**
143
+ * @see https://www.tampermonkey.net/documentation.php#meta:unwrap
144
+ */
145
+ 'unwrap'?: boolean;
141
146
  };
142
147
  interface ServerConfig {
143
- /**
144
- * {@link https://github.com/sindresorhus/get-port}
145
- */
146
- port?: number;
147
- /**
148
- * @default false
149
- */
150
- open?: boolean;
148
+ /**
149
+ * Open the install target when Vite starts.
150
+ * HMR: `.dev.user.js` URL. `file`: `{fileName}.proxy.user.js` URL.
151
+ *
152
+ * @default false
153
+ */
154
+ open?: boolean;
155
+ /**
156
+ * Prefix applied to `@name` in serve mode.
157
+ * Set `false` to disable.
158
+ *
159
+ * @default 'server:'
160
+ */
161
+ prefix?: string | false;
162
+ /**
163
+ * Watch-build into `outDir`: headerless `{fileName}.js` and `{fileName}.proxy.user.js`
164
+ * with `@require file://` pointing at the IIFE.
165
+ * Install the proxy from the printed `/{fileName}.proxy.user.js` URL. No HMR for this script.
166
+ *
167
+ * @default false
168
+ */
169
+ file?: boolean;
151
170
  }
152
- interface UserscriptPluginConfig {
153
- /**
154
- * Path of userscript entry.
155
- */
156
- entry: string;
157
- /**
158
- * Userscript file name.
159
- */
160
- fileName?: string;
161
- /**
162
- * Userscript header config.
163
- *
164
- * @see https://www.tampermonkey.net/documentation.php
165
- */
166
- header: HeaderConfig;
167
- /**
168
- * Server config.
169
- */
170
- server?: ServerConfig;
171
- /**
172
- * Override default esbuild transform options.
173
- *
174
- * @default
175
- * ```json
176
- * {
177
- * "minify": true,
178
- * "legalComments": "none"
179
- * }
180
- * ```
181
- */
182
- esbuildTransformOptions?: Omit<EsbuildTransformOptions, 'format' | 'target' | 'loader' | 'sourcemap'>;
171
+ type HeaderMode = 'serve' | 'build' | 'meta';
172
+ interface HeaderGenerateContext {
173
+ userscript: string;
174
+ mode: HeaderMode;
183
175
  }
184
-
185
- declare function UserscriptPlugin(config: UserscriptPluginConfig): PluginOption;
186
-
187
- export { type UserscriptPluginConfig, UserscriptPlugin as default };
176
+ type CssInject = 'auto' | string | ((css: string) => void);
177
+ /**
178
+ * One userscript. Pass an object, or an array of these, to {@link UserscriptPluginConfig}.
179
+ */
180
+ interface UserscriptConfig {
181
+ /**
182
+ * Path of the userscript entry.
183
+ */
184
+ entry: string;
185
+ /**
186
+ * Output base name (`{fileName}.user.js`).
187
+ *
188
+ * @default sanitized `header.name`
189
+ */
190
+ fileName?: string;
191
+ /**
192
+ * Userscript header (`name`, `version`, `match` required).
193
+ */
194
+ header: HeaderConfig;
195
+ /**
196
+ * Serve-mode options.
197
+ */
198
+ server?: ServerConfig;
199
+ /**
200
+ * How to inject collected CSS in production builds.
201
+ *
202
+ * @default 'auto'
203
+ */
204
+ cssInject?: CssInject;
205
+ /**
206
+ * Extra spaces after the longest `@key`, or `false` for a single space.
207
+ *
208
+ * @default 1
209
+ */
210
+ align?: number | false;
211
+ /**
212
+ * Rewrite the generated metablock.
213
+ */
214
+ generate?: (ctx: HeaderGenerateContext) => string;
215
+ /**
216
+ * Derive `updateURL` / `downloadURL` from `homepage`, `homepageURL`,
217
+ * `website`, or `source` when those fields are empty. Warns if no homepage
218
+ * alias is set, or if `metaFile` is `false` (`@updateURL` would point at a
219
+ * missing file).
220
+ *
221
+ * @default false
222
+ */
223
+ autoMetaUrls?: boolean;
224
+ /**
225
+ * Emit `{fileName}.meta.js` alongside the userscript.
226
+ * Keep enabled when using `autoMetaUrls`, otherwise `@updateURL` 404s.
227
+ *
228
+ * @default true
229
+ */
230
+ metaFile?: boolean;
231
+ }
232
+ type UserscriptPluginConfig = UserscriptConfig | UserscriptConfig[];
233
+ interface ResolvedScript {
234
+ entry: string;
235
+ fileName: string;
236
+ iifeName: string;
237
+ header: HeaderConfig;
238
+ server: {
239
+ open: boolean;
240
+ prefix: string | false;
241
+ file: boolean;
242
+ };
243
+ cssInject: CssInject;
244
+ align: number | false;
245
+ generate?: (ctx: HeaderGenerateContext) => string;
246
+ autoMetaUrls: boolean;
247
+ metaFile: boolean;
248
+ }
249
+ //#endregion
250
+ //#region src/header.d.ts
251
+ interface HeaderOptions {
252
+ align?: number | false;
253
+ autoMetaUrls?: boolean;
254
+ fileName?: string;
255
+ generate?: (ctx: HeaderGenerateContext) => string;
256
+ mode?: HeaderMode;
257
+ }
258
+ declare function resolveHomePage(header: HeaderConfig): string | undefined;
259
+ declare function resolvePublicFileUrl(header: HeaderConfig, fileName: string): string | undefined;
260
+ declare function generateHeader(config: HeaderConfig, options?: HeaderOptions): string;
261
+ declare class Header {
262
+ private readonly config;
263
+ private readonly options;
264
+ constructor(config: HeaderConfig, options?: HeaderOptions);
265
+ generate(): string;
266
+ }
267
+ //#endregion
268
+ //#region src/plugin.d.ts
269
+ declare function UserscriptPlugin(config: UserscriptPluginConfig): Plugin[];
270
+ //#endregion
271
+ export { type CssInject, Header, type HeaderConfig, type HeaderGenerateContext, type ResolvedScript, type ServerConfig, type UserscriptConfig, type UserscriptPluginConfig, UserscriptPlugin as default, generateHeader, resolveHomePage, resolvePublicFileUrl };