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 CHANGED
@@ -1,2 +1 @@
1
- declare const port: number;
2
1
  declare const ws: WebSocket;
package/dist/hmr.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const ws = new WebSocket(`ws://localhost:${port}`);
2
+ const ws = new WebSocket('ws://localhost:__PORT__');
3
3
  ws.addEventListener('message', () => {
4
4
  location.reload();
5
5
  });
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 filePath = resolve(rootDir, outDir, fileName);
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 file = readFileSync(filePath, {
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(file)
85
+ ? defineGrants(source)
87
86
  : [...(config.metadata.grant ?? []), 'GM_addStyle', 'GM_info']);
88
87
  // prettier-ignore-end
89
- // source
90
- writeFileSync(filePath, file);
91
- // production
92
- writeFileSync(userFilePath, `${banner(config.metadata)}\n\n${file}`);
93
- // development
94
- writeFileSync(proxyFilePath, banner({
95
- ...config.metadata,
96
- require: [...config.metadata.require, 'file://' + filePath]
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-userscript-plugin",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.js",