-
Notifications
You must be signed in to change notification settings - Fork 62
/
types.ts
333 lines (312 loc) · 10.2 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import type { FinalUserScript, Format, MonkeyUserScript } from './userscript';
import { AlignFunc } from './userscript/common';
export type IPromise<T> = T | Promise<T>;
export type IArray<T = unknown> = T | T[];
/**
* key is language code or ''
* @see https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
* @example
* {
* '':'default_name',
* 'zh-CN':'名字',
* ja: '名前'
* }
*/
export type LocaleType<T = unknown> = Record<string, T>;
export type PkgOptions = {
name: string;
version: string;
importName: string;
resolveName: string;
resourceUrl: string;
resourceName: string;
};
export type Pkg2UrlFn = (pkg: {
name: string;
version: string;
importName: string;
resolveName: string;
}) => IPromise<string>;
/**
* @param importName 'name/subname' in example
* @returns url or exportVarName
* @example
* const mod = await import('name/subname')
*/
export type Mod2UrlFn = (
version: string,
name: string,
importName: string,
) => IPromise<string>;
export type ModuleToUrlFc = (
version: string,
name: string,
importName?: string,
resolveName?: string,
) => string;
export type ExternalGlobals =
| Record<string, IArray<string | Mod2UrlFn>>
| [string, IArray<string | Mod2UrlFn>][];
export type ExternalResource = Record<
string,
| string
| Pkg2UrlFn
| {
resourceUrl: Pkg2UrlFn | string;
/**
* @default importName
*/
resourceName?: Pkg2UrlFn | string;
loader?: (pkgOptions: PkgOptions) => unknown;
nodeLoader?: string | ((pkgOptions: PkgOptions) => IPromise<string>);
}
| Array<
| string
| ((
version: string,
name: string,
importName: string,
resolveName: string,
) => IPromise<string>)
>
>;
export type FinalMonkeyOption = {
entry: string;
format: {
align: number | boolean | AlignFunc;
generate: (uOptions: {
userscript: string;
mode: `serve` | `build` | `meta`;
}) => IPromise<string>;
};
userscript: FinalUserScript;
clientAlias: string;
server: {
open: boolean;
prefix: (name: string) => string;
mountGmApi: boolean;
};
build: {
fileName: string;
metaFileName?: () => string;
autoGrant: boolean;
externalGlobals: [string, IArray<string | Mod2UrlFn>][];
externalResource: Record<
string,
{
resourceUrl: Pkg2UrlFn;
resourceName: Pkg2UrlFn;
loader?: (pkgOptions: PkgOptions) => unknown;
nodeLoader?: (pkgOptions: PkgOptions) => IPromise<string>;
}
>;
};
collectRequireUrls: string[];
collectResource: Record<string, string>;
globalsPkg2VarName: Record<string, string>;
requirePkgList: { moduleName: string; url: string }[];
systemjs: 'inline' | ModuleToUrlFc;
cssSideEffects: (css: string) => Promise<string>;
};
export type MonkeyOption = {
/**
* userscript entry file path
*/
entry: string;
userscript?: MonkeyUserScript;
format?: Format;
/**
* alias of vite-plugin-monkey/dist/client
* @default '$'
* @example
* // vite-env.d.ts for type hint
*
* // if you use default value `$`
* /// <reference types="vite-plugin-monkey/client" />
*
* // if you use other_alias
* declare module other_alias {
* export * from 'vite-plugin-monkey/dist/client';
* }
*/
clientAlias?: string;
server?: {
/**
* auto open install url in default browser when userscript comment change
*
* and set `viteConfig.server.open ??= monkeyConfig.server.open`
* @default
* process.platform == 'win32' || process.platform == 'darwin' // if platform is Win/Mac
*/
open?: boolean;
/**
* name prefix, distinguish server.user.js and build.user.js in monkey extension install list, if you not want prefix, set false
* @default 'server:'
*/
prefix?: string | ((name: string) => string) | false;
/**
* mount GM_api to unsafeWindow, not recommend it, you should use GM_api by ESM import, or use [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)
* @default false
* @example
* // if set true, you can use `vite-plugin-monkey/global` for type hint
* // vite-env.d.ts
* /// <reference types="vite-plugin-monkey/global" />
*/
mountGmApi?: boolean;
};
build?: {
/**
* build bundle userscript file name
*
* it should end with '.user.js'
* @default (package.json.name??'monkey')+'.user.js'
*/
fileName?: string;
/**
* build bundle userscript comment file name, this file is only include comment
*
* it can be used by userscript.updateURL, when checking for updates, just download this small file instead of downloading the entire script
*
* it should end with '.meta.js', if set false, will not generate this file
*
* if set true, will equal to fileName.replace(/\\.user\\.js$/,'.meta.js')
*
* @default false
*/
metaFileName?: string | boolean | ((fileName: string) => string);
/**
* this config can be array or object, array=Object.entries(object)
*
* if value is string or function, it or its return value is exportVarName
*
* if value is Array, the first [item or its return value] is exportVarName, the items after it all are url that is [require url]
*
* if module is unimported, plugin will not add require url to userscript
*
* @example
* { // map structure
* vue:'Vue',
* // if set this
* // you need manually set userscript.require = ['https://unpkg.com/vue@3.0.0/dist/vue.global.js'], when `vite build`
*
* vuex:['Vuex', (version, name)=>`https://unpkg.com/${name}@${version}/dist/vuex.global.js`],
* // plugin will auto add this url to userscript.require
*
* 'prettier/parser-babel': [
* 'prettierPlugins.babel',
* (version, name, importName) => {
* // name == `prettier`
* // importName == `prettier/parser-babel`
* const subpath = `${importName.split('/').at(-1)}.js`;
* return `https://cdn.jsdelivr.net/npm/${name}@${version}/${subpath}`;
* },
* ],
* // sometimes importName deffers from package name
* }
* @example
* [ // array structure, this example come from [playground/ex-vue-demi](https://github.com/lisonge/vite-plugin-monkey/tree/main/playground/ex-vue-demi)
* [
* 'vue',
* cdn
* .jsdelivr('Vue', 'dist/vue.global.prod.js')
* .concat('https://unpkg.com/vue-demi@latest/lib/index.iife.js')
* .concat(
* await util.fn2dataUrl(() => {
* window.Vue = Vue;
* }),
* ),
* ],
* ['pinia', cdn.jsdelivr('Pinia', 'dist/pinia.iife.prod.js')],
* [
* 'element-plus',
* cdn.jsdelivr('ElementPlus', 'dist/index.full.min.js'),
* ],
* ]
*/
externalGlobals?: ExternalGlobals;
/**
* according to final code bundle, auto inject GM_* or GM.* to userscript comment grant
*
* tree shaking code, then if code.includes('GM_xxx'), add \@grant GM_xxx to userscript
* @default true
*/
autoGrant?: boolean;
/**
* @deprecated use [viteConfig.build.cssMinify](https://vitejs.dev/config/build-options.html#build-cssminify) in vite>=4.2.0
*
* now minifyCss will not work
*/
minifyCss?: boolean;
/**
* @example
* { // resourceName default value is pkg.importName
* 'element-plus/dist/index.css': pkg=>`https://unpkg.com/${pkg.name}@${pkg.version}/${pkg.resolveName}`,
* 'element-plus/dist/index.css': {
* resourceName: pkg=>pkg.importName,
* resourceUrl: pkg=>`https://unpkg.com/${pkg.name}@${pkg.version}/${pkg.resolveName}`,
* loader: pkg=>{ // there are default loaders that support [css, json, the assets that vite support, ?url, ?raw] file/name suffix
* const css = GM_getResourceText(pkg.resourceName);
* GM_addStyle(css);
* return css;
* },
* nodeLoader: pkg=>{
* return [
* `export default (()=>{`,
* `const css = GM_getResourceText(${JSON.stringify(pkg.resourceName)});`,
* `GM_addStyle(css);`,
* `return css;`,
* `})();`
* ].join('');
* },
* },
* 'element-plus/dist/index.css': [
* (version, name, importName, resolveName)=>importName,
* (version, name, importName, resolveName)=>`https://unpkg.com/${name}@${version}/${resolveName}`,
* // for compat externalGlobals cdn function, if (version/name/importName/resolveName) == '', plugin will use their own default values
* ],
* 'element-plus/dist/index.css': cdn.jsdelivr(),
* }
*/
externalResource?: ExternalResource;
/**
* when use dynamic-import, plugin will use systemjs build your code
*
* `cdn.jsdelivr()[1]` example -> [dynamic-import.user.js](https://github.com/lisonge/vite-plugin-monkey/blob/7645b185605faf9b48c43116db5ea01726188e03/playground/dynamic-import/dist/dynamic-import.user.js)
*
* `'inline'` exmple -> [test-v3.user.js](https://github.com/lisonge/vite-plugin-monkey/blob/7645b185605faf9b48c43116db5ea01726188e03/playground/test-v3/dist/test-v3.user.js)
*
* @default
* cdn.jsdelivr()[1]
*/
systemjs?: 'inline' | ModuleToUrlFc;
/**
* @default
* const defaultFc = () => {
* return (e: string) => {
* if (typeof GM_addStyle == 'function') {
* GM_addStyle(e);
* return;
* }
* const o = document.createElement('style');
* o.textContent = e;
* document.head.append(o);
* };
* };
* @example
* const defaultFc1 = () => {
* return (e: string) => {
* const o = document.createElement('style');
* o.textContent = e;
* document.head.append(o);
* };
* };
* const defaultFc2 = (css:string)=>{
* const t = JSON.stringify(css)
* return `(e=>{const o=document.createElement("style");o.textContent=e,document.head.append(o)})(${t})`
* }
*/
cssSideEffects?: (
css: string,
) => IPromise<string | ((css: string) => void)>;
};
};