vite-userscript-plugin 0.3.0 → 0.4.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/hmr.d.ts +0 -1
- package/dist/hmr.js +1 -1
- package/dist/index.js +38 -22
- package/package.json +1 -1
package/dist/hmr.d.ts
CHANGED
package/dist/hmr.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -12,7 +12,13 @@ function UserscriptPlugin(config) {
|
|
|
12
12
|
let isBuildWatch;
|
|
13
13
|
let socketConnection = null;
|
|
14
14
|
const port = config.server?.port || 8000;
|
|
15
|
-
const server = createServer()
|
|
15
|
+
const server = createServer((_, res) => {
|
|
16
|
+
// const index = resolve(
|
|
17
|
+
// dirname(fileURLToPath(import.meta.url)), '..', 'src', 'index.html'
|
|
18
|
+
// )
|
|
19
|
+
// res.writeHead(200, { 'Content-Type': 'html' })
|
|
20
|
+
// res.end(readFileSync(index))
|
|
21
|
+
});
|
|
16
22
|
server.listen(port);
|
|
17
23
|
const WebSocketServer = websocket.server;
|
|
18
24
|
const ws = new WebSocketServer({ httpServer: server });
|
|
@@ -33,6 +39,7 @@ function UserscriptPlugin(config) {
|
|
|
33
39
|
},
|
|
34
40
|
rollupOptions: {
|
|
35
41
|
output: {
|
|
42
|
+
exports: 'none',
|
|
36
43
|
extend: true
|
|
37
44
|
}
|
|
38
45
|
}
|
|
@@ -65,36 +72,45 @@ function UserscriptPlugin(config) {
|
|
|
65
72
|
if (regexpScripts.test(fileName)) {
|
|
66
73
|
const rootDir = pluginConfig.root;
|
|
67
74
|
const outDir = pluginConfig.build.outDir;
|
|
68
|
-
const
|
|
75
|
+
const outPath = resolve(rootDir, outDir, fileName);
|
|
76
|
+
const hmrPath = resolve(rootDir, outDir, 'hmr.js');
|
|
69
77
|
const proxyFilePath = resolve(rootDir, outDir, `${config.metadata.name}.proxy.user.js`);
|
|
70
78
|
const userFilePath = resolve(rootDir, outDir, `${config.metadata.name}.user.js`);
|
|
71
79
|
try {
|
|
72
|
-
let
|
|
73
|
-
encoding: 'utf8'
|
|
74
|
-
});
|
|
75
|
-
const hmrScript = readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), 'hmr.js'), 'utf-8');
|
|
76
|
-
file = file.replace(template, `
|
|
77
|
-
${css.inject()}
|
|
78
|
-
const port = ${port}
|
|
79
|
-
${hmrScript}
|
|
80
|
-
`);
|
|
81
|
-
file = await transform({ file, name: fileName, loader: 'js' });
|
|
80
|
+
let source = readFileSync(outPath, 'utf8');
|
|
82
81
|
// prettier-ignore
|
|
83
82
|
config.metadata.grant = removeDuplicates(isBuildWatch
|
|
84
83
|
? grants
|
|
85
84
|
: config.autoGrants ?? true
|
|
86
|
-
? defineGrants(
|
|
85
|
+
? defineGrants(source)
|
|
87
86
|
: [...(config.metadata.grant ?? []), 'GM_addStyle', 'GM_info']);
|
|
88
87
|
// prettier-ignore-end
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
if (isBuildWatch) {
|
|
89
|
+
const hmrScript = await transform({
|
|
90
|
+
file: `
|
|
91
|
+
${readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), 'hmr.js'), 'utf8')}
|
|
92
|
+
`.replace('__PORT__', port.toString()),
|
|
93
|
+
name: hmrPath,
|
|
94
|
+
loader: 'js'
|
|
95
|
+
});
|
|
96
|
+
writeFileSync(hmrPath, hmrScript);
|
|
97
|
+
writeFileSync(proxyFilePath, banner({
|
|
98
|
+
...config.metadata,
|
|
99
|
+
require: [
|
|
100
|
+
...config.metadata.require,
|
|
101
|
+
'file://' + hmrPath,
|
|
102
|
+
'file://' + outPath
|
|
103
|
+
]
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
source = source.replace(template, `${css.inject()}`);
|
|
107
|
+
source = await transform({
|
|
108
|
+
file: source,
|
|
109
|
+
name: fileName,
|
|
110
|
+
loader: 'js'
|
|
111
|
+
});
|
|
112
|
+
writeFileSync(outPath, source);
|
|
113
|
+
writeFileSync(userFilePath, `${banner(config.metadata)}\n\n${source}`);
|
|
98
114
|
}
|
|
99
115
|
catch (err) {
|
|
100
116
|
console.log(err);
|