vite-userscript-plugin 0.2.0 → 0.5.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
- declare const ws: WebSocket;
1
+ declare function HRM(): void;
package/dist/hmr.js CHANGED
@@ -1,11 +1,22 @@
1
1
  "use strict";
2
- const ws = new WebSocket(`ws://localhost:${port}`);
3
- ws.addEventListener('message', () => {
4
- location.reload();
5
- });
6
- ws.addEventListener('open', () => {
7
- const { script } = GM_info;
8
- console.group(`${script.name} / ${script.version}`);
9
- console.log(GM_info);
10
- console.groupEnd();
11
- });
2
+ function HRM() {
3
+ const ws = new WebSocket('ws://localhost:__PORT__');
4
+ ws.addEventListener('open', () => {
5
+ console.clear();
6
+ const { script } = GM_info;
7
+ console.group(`${script.name}@${script.version}`);
8
+ console.log(GM_info);
9
+ console.groupEnd();
10
+ });
11
+ ws.addEventListener('close', (event) => {
12
+ console.warn('Socket is closed. Reconnect will be attempted in 1 second.', event.reason);
13
+ setTimeout(HRM, 1000);
14
+ });
15
+ ws.addEventListener('error', () => {
16
+ ws.close();
17
+ });
18
+ ws.addEventListener('message', () => {
19
+ location.reload();
20
+ });
21
+ }
22
+ HRM();
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,44 @@ 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 hmrFile = readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), 'hmr.js'), 'utf8');
90
+ const hmrScript = await transform({
91
+ file: hmrFile.replace('__PORT__', port.toString()),
92
+ name: hmrPath,
93
+ loader: 'js'
94
+ });
95
+ writeFileSync(hmrPath, hmrScript);
96
+ writeFileSync(proxyFilePath, banner({
97
+ ...config.metadata,
98
+ require: [
99
+ ...config.metadata.require,
100
+ 'file://' + hmrPath,
101
+ 'file://' + outPath
102
+ ]
103
+ }));
104
+ }
105
+ source = source.replace(template, `${css.inject()}`);
106
+ source = await transform({
107
+ file: source,
108
+ name: fileName,
109
+ loader: 'js'
110
+ });
111
+ writeFileSync(outPath, source);
112
+ writeFileSync(userFilePath, `${banner(config.metadata)}\n\n${source}`);
98
113
  }
99
114
  catch (err) {
100
115
  console.log(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-userscript-plugin",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.js",
@@ -32,6 +32,9 @@
32
32
  "devDependencies": {
33
33
  "@types/websocket": "^1.0.5"
34
34
  },
35
+ "peerDependencies": {
36
+ "@types/tampermonkey": "latest"
37
+ },
35
38
  "scripts": {
36
39
  "dev": "tsc --build --watch",
37
40
  "build": "pnpm clean && tsc --build",