vite-userscript-plugin 0.1.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/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # vite-userscript-plugin
2
+
3
+ ![ci](https://img.shields.io/github/workflow/status/crashmax-dev/vite-userscript-plugin/npm-publish)
4
+ ![npm](https://img.shields.io/npm/v/vite-userscript-plugin)
5
+ ![license](https://img.shields.io/github/license/crashmax-dev/vite-userscript-plugin)
6
+
7
+ > Tampermonkey userscript developing and build plugin based on [Vite](https://vitejs.dev).
8
+
9
+ ## Features
10
+
11
+ - 🔥 Hot reloading after changing any files.
12
+ - 🔧 Configure Tampermonkey's Userscript header.
13
+ - 💨 Import all `grant` by default in development mode.
14
+ - 📝 Automatically add used `grant` when building for production.
15
+
16
+ ## Install
17
+
18
+ ```
19
+ npm install vite-userscript-plugin -D
20
+ ```
21
+
22
+ ```
23
+ yarn add vite-userscript-plugin -D
24
+ ```
25
+
26
+ ```
27
+ pnpm add vite-userscript-plugin -D
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ### `vite.config.ts`
33
+
34
+ ```js
35
+ import { resolve } from 'path'
36
+ import { defineConfig } from 'vite'
37
+ import { UserscriptPlugin } from 'vite-userscript-plugin'
38
+ import { name, version } from './package.json'
39
+
40
+ export default defineConfig({
41
+ plugins: [
42
+ UserscriptPlugin({
43
+ entry: resolve(__dirname, 'src/index.ts'),
44
+ metadata: {
45
+ name,
46
+ version,
47
+ match: [
48
+ 'https://example.com',
49
+ 'https://example.org',
50
+ 'https://example.edu'
51
+ ]
52
+ }
53
+ })
54
+ ]
55
+ })
56
+ ```
57
+
58
+ ### `package.json`
59
+
60
+ ```json
61
+ {
62
+ "scripts": {
63
+ "dev": "vite build --watch",
64
+ "build": "vite build"
65
+ }
66
+ }
67
+ ```
68
+
69
+ ## Plugin Configuration
70
+
71
+ ```ts
72
+ export interface UserscriptPluginConfig {
73
+ /**
74
+ * Path of userscript entry.
75
+ */
76
+ entry: string
77
+
78
+ /**
79
+ * Userscript header config.
80
+ */
81
+ metadata: MetadataConfig
82
+
83
+ /**
84
+ * Import all `@grant` in development mode.
85
+ * @default true
86
+ */
87
+ autoGrants?: boolean
88
+
89
+ /**
90
+ * Server config (used in development mode for hot reloading).
91
+ */
92
+ server?: ServerConfig
93
+ }
94
+ ```
95
+
96
+ ## Example
97
+
98
+ See the [example](https://github.com/crashmax-dev/vite-userscript-plugin/tree/master/packages/demo) folder.
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.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PluginOption } from 'vite';
2
- import type { PluginConfig } from './types.js';
3
- declare function UserscriptPlugin(config: PluginConfig): PluginOption;
2
+ import type { UserscriptPluginConfig } from './types.js';
3
+ declare function UserscriptPlugin(config: UserscriptPluginConfig): PluginOption;
4
4
  export { UserscriptPlugin };
5
5
  export default UserscriptPlugin;
6
- export type { PluginConfig };
6
+ export type { UserscriptPluginConfig };
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
- : config.autoGrants
86
- ? defineGrants(file)
84
+ : config.autoGrants ?? true
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/dist/types.d.ts CHANGED
@@ -191,22 +191,27 @@ export declare type MetadataConfig = {
191
191
  };
192
192
  export interface ServerConfig {
193
193
  /**
194
+ * Server port.
194
195
  * @default 8000
195
196
  */
196
197
  port?: number;
197
198
  }
198
- export interface PluginConfig {
199
+ export interface UserscriptPluginConfig {
199
200
  /**
200
201
  * Path of userscript entry.
201
202
  */
202
203
  entry: string;
203
204
  /**
204
- * @default false
205
+ * Userscript header config.
206
+ */
207
+ metadata: MetadataConfig;
208
+ /**
209
+ * Import all `@grant` in development mode.
210
+ * @default true
205
211
  */
206
212
  autoGrants?: boolean;
207
- server?: ServerConfig;
208
213
  /**
209
- * Userscript Metadata config.
214
+ * Server config (used in development mode for hot reloading).
210
215
  */
211
- metadata: MetadataConfig;
216
+ server?: ServerConfig;
212
217
  }
package/package.json CHANGED
@@ -1,18 +1,40 @@
1
1
  {
2
2
  "name": "vite-userscript-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.js",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/crashmax-dev/vite-userscript-plugin.git"
13
+ },
14
+ "keywords": [
15
+ "vite",
16
+ "vite-plugin",
17
+ "userscript",
18
+ "tampermonkey"
19
+ ],
20
+ "author": {
21
+ "name": "Vitalij Ryndin",
22
+ "email": "sys@crashmax.ru",
23
+ "url": "https://crashmax.ru"
24
+ },
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/crashmax-dev/vite-userscript-plugin/issues"
28
+ },
10
29
  "dependencies": {
11
30
  "websocket": "^1.0.34"
12
31
  },
13
32
  "devDependencies": {
14
33
  "@types/websocket": "^1.0.5"
15
34
  },
35
+ "peerDependencies": {
36
+ "@types/tampermonkey": "latest"
37
+ },
16
38
  "scripts": {
17
39
  "dev": "tsc --build --watch",
18
40
  "build": "pnpm clean && tsc --build",