-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-script.js
More file actions
83 lines (61 loc) · 2.13 KB
/
vite-script.js
File metadata and controls
83 lines (61 loc) · 2.13 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
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('\\', '\\\\')}"`
)
);
const { output: outputs } = 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,
},
optimizeDeps: {
esbuildOptions: {
target: 'esnext'
}
},
});
let code = outputs.find(o => o.name == 'index' && o.code)?.code;
const codeStyle = outputs.find(o => o.fileName?.endsWith('.css') && o.source);
if(codeStyle) { code = `\nGM_addStyle(\`${codeStyle.source.trim()}\`);\n` + code; }
if(code) {
writeFileSync(resolve(C.dirDist, `${nameMetaScript}.user.js`), code);
}
const url = `http://userscript.localhost/${nameMetaScript}.user.js`;
globalThis.console.log('本地安装地址', url);
if(C.openLink && C.pathChrome) { spawnSync(C.pathChrome, [url]); }