-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-script.watch.js
More file actions
113 lines (86 loc) · 2.89 KB
/
vite-script.watch.js
File metadata and controls
113 lines (86 loc) · 2.89 KB
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
import { C } from '@nuogz/pangu/index.js?config=_';
import { spawnSync } from 'node:child_process';
import { readFileSync, writeFileSync } from 'node:fs';
import { parse, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { build } from 'vite';
import pluginVue from '@vitejs/plugin-vue';
import pluginUno from 'unocss/vite';
if('2' in process.argv == false) { throw Error('缺少目标脚本参数'); }
const dirWorking = process.cwd();
const fileScript = process.argv[2];
const pathScript = resolve(dirWorking, fileScript);
const pathParsedScript = parse(pathScript);
const textScript = readFileSync(pathScript, 'utf-8');
const nameMetaScript = textScript.match(/==UserScript==.*(?:@name +(.+?)\n).*==\/UserScript==/ms)?.[1];
globalThis.console.log('脚本文件', pathParsedScript.base);
globalThis.console.log('脚本名称', nameMetaScript);
const dirPackage = fileURLToPath(new URL('.', import.meta.url));
const pathEntry = resolve(dirPackage, 'src', 'index.html');
writeFileSync(pathEntry,
readFileSync(pathEntry, 'utf-8').replace(
/src=".*?"/,
`src="${relative(parse(pathEntry).dir, pathScript).replaceAll('\\', '\\\\')}"`
)
);
/** @type {import('vite').Rollup.OutputPluginOption} */
const pluginUserscriptMixin = {
name: 'rollup-plugin-userscript-mixin',
generateBundle: {
order: 'post',
handler(options, bundle) {
const outputs = Object.values(bundle);
for(const key in bundle) { delete bundle[key]; }
let code = outputs.find(o => o.name == 'index' && o.code)?.code;
const assetStyle = outputs.find(o => o.fileName?.endsWith('.css') && o.source);
if(assetStyle) { code = `\nGM_addStyle(\`${assetStyle.source.trim()}\`);\n${code}`; }
if(code) {
this.emitFile({
type: 'prebuilt-chunk',
fileName: `${nameMetaScript}.user.js`,
code
});
}
}
}
};
/** @type {import('vite').Rollup.RollupWatcher} */
const watcherRollup = await build({
mode: 'production',
clearScreen: false,
plugins: [
pluginUno(),
pluginVue({
template: {
compilerOptions: {
isCustomElement: tag => /^((module-|comp-|p-).+?|module)$/.test(tag)
}
}
})
],
root: resolve(dirPackage, 'src'),
base: './',
build: {
target: 'esnext',
minify: false,
modulePreload: { polyfill: false },
write: false,
watch: { include: [fileScript] },
outDir: C.dirDist,
emptyOutDir: false,
rollupOptions: {
output: { plugins: [pluginUserscriptMixin] }
}
},
optimizeDeps: {
esbuildOptions: { target: 'esnext' }
},
});
watcherRollup.on('event', event => {
if(event.code == 'BUNDLE_START') { return globalThis.console.log('脚本构建开始'); }
if(event.code != 'BUNDLE_END') { return; }
globalThis.console.log('脚本构建完成');
const url = `http://userscript.localhost/${nameMetaScript}.user.js`;
globalThis.console.log('本地安装地址', url);
if(C.openLink && C.pathChrome) { spawnSync(C.pathChrome, [url]); }
});