vite-userscript-plugin 1.10.0 → 2.0.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 +156 -82
- package/dist/index.d.ts +254 -180
- package/dist/index.js +902 -4
- package/package.json +55 -49
- package/types/greasemonkey.d.ts +277 -218
- package/types/tampermonkey.d.ts +1273 -658
- package/types/violentmonkey-ambient.d.ts +149 -0
- package/types/violentmonkey.d.ts +362 -154
- package/virtual.d.ts +7 -0
- package/dist/index.cjs +0 -5
- package/dist/index.d.cts +0 -187
- package/dist/ws.js +0 -1
- package/types/README.md +0 -21
package/README.md
CHANGED
|
@@ -1,126 +1,200 @@
|
|
|
1
1
|
# vite-userscript-plugin
|
|
2
2
|
|
|
3
3
|
[](https://npmjs.com/vite-userscript-plugin)
|
|
4
|
-
[](./LICENCE)
|
|
5
|
+
[](https://github.com/greasify/vite-userscript-template)
|
|
6
6
|
|
|
7
|
-
>
|
|
7
|
+
> A Vite plugin for developing and building Tampermonkey, Greasemonkey and Violentmonkey userscripts.
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
- 🔥
|
|
12
|
-
- 🔧 Configure
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
11
|
+
- 🔥 Vite HMR
|
|
12
|
+
- 🔧 Configure Userscript header
|
|
13
|
+
- 🎨 Inject CSS from imports and SFC components (Vue, Svelte)
|
|
14
|
+
- 💨 All `@grant`s in the header in dev mode
|
|
15
|
+
- 📝 Only used `@grant`s in the production build
|
|
16
|
+
- 📦 Built-in types for Tampermonkey, Greasemonkey and Violentmonkey
|
|
17
|
+
- 📄 Virtual module with script metadata
|
|
16
18
|
|
|
17
|
-
##
|
|
19
|
+
## Getting started
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
npm install vite-userscript-plugin -D
|
|
21
|
-
```
|
|
21
|
+
Requires **Vite 8** and Node `>=22`.
|
|
22
22
|
|
|
23
|
-
```
|
|
24
|
-
yarn add vite-userscript-plugin -D
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
```
|
|
23
|
+
```bash
|
|
28
24
|
pnpm add vite-userscript-plugin -D
|
|
29
25
|
```
|
|
30
26
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```js
|
|
27
|
+
```ts
|
|
34
28
|
import { defineConfig } from 'vite'
|
|
35
|
-
import
|
|
36
|
-
import
|
|
37
|
-
|
|
38
|
-
export default defineConfig(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
server: {
|
|
53
|
-
port: 3000
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
]
|
|
57
|
-
}
|
|
29
|
+
import userscript from 'vite-userscript-plugin'
|
|
30
|
+
import pkg from './package.json' with { type: 'json' }
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [
|
|
34
|
+
userscript({
|
|
35
|
+
entry: 'src/index.ts',
|
|
36
|
+
header: {
|
|
37
|
+
name: pkg.name,
|
|
38
|
+
version: pkg.version,
|
|
39
|
+
match: [
|
|
40
|
+
'https://example.com/',
|
|
41
|
+
'https://example.org/'
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
]
|
|
58
46
|
})
|
|
59
47
|
```
|
|
60
48
|
|
|
61
|
-
### Setup NPM scripts
|
|
62
|
-
|
|
63
49
|
```json
|
|
64
|
-
// package.json
|
|
65
50
|
{
|
|
66
51
|
"scripts": {
|
|
67
|
-
"dev": "vite
|
|
52
|
+
"dev": "vite",
|
|
68
53
|
"build": "vite build"
|
|
69
54
|
}
|
|
70
55
|
}
|
|
71
56
|
```
|
|
72
57
|
|
|
73
|
-
|
|
58
|
+
Add types: Vite, **one** manager (`tampermonkey`, `greasemonkey`, or `violentmonkey`), and the virtual module.
|
|
59
|
+
|
|
60
|
+
`src/vite-env.d.ts`:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
/// <reference types="vite/client" />
|
|
64
|
+
/// <reference types="vite-userscript-plugin/types/tampermonkey" />
|
|
65
|
+
/// <reference types="vite-userscript-plugin/virtual" />
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Or `tsconfig.json`:
|
|
74
69
|
|
|
75
70
|
```json
|
|
76
|
-
// tsconfig.json
|
|
77
71
|
{
|
|
78
72
|
"compilerOptions": {
|
|
79
73
|
"types": [
|
|
80
|
-
"vite
|
|
74
|
+
"vite/client",
|
|
75
|
+
"vite-userscript-plugin/types/tampermonkey",
|
|
76
|
+
"vite-userscript-plugin/virtual"
|
|
81
77
|
]
|
|
82
78
|
}
|
|
83
79
|
}
|
|
84
80
|
```
|
|
85
81
|
|
|
86
|
-
|
|
82
|
+
Details: [types/README.md](./types/README.md).
|
|
87
83
|
|
|
88
|
-
|
|
89
|
-
interface ServerConfig {
|
|
90
|
-
/**
|
|
91
|
-
* {@link https://github.com/sindresorhus/get-port}
|
|
92
|
-
*/
|
|
93
|
-
port?: number;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @default false
|
|
97
|
-
*/
|
|
98
|
-
open?: boolean;
|
|
99
|
-
}
|
|
84
|
+
`vite` prints `/{fileName}.dev.user.js` — install that URL once. HMR covers code and styles.
|
|
100
85
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
86
|
+
> [!IMPORTANT]
|
|
87
|
+
> Changing `@match`, `@grant`, or `@name` needs a reinstall.
|
|
88
|
+
|
|
89
|
+
`vite build` writes `{fileName}.user.js` to `dist/`.
|
|
90
|
+
|
|
91
|
+
## Multiple scripts
|
|
92
|
+
|
|
93
|
+
Pass an array of configs. Each item is one full script — no shared `header`.
|
|
94
|
+
|
|
95
|
+
See [examples/multiple-entries](./examples/multiple-entries).
|
|
96
|
+
|
|
97
|
+
## Styles
|
|
98
|
+
|
|
99
|
+
CSS from imports and SFC `<style>` (Vue, Svelte) is collected and injected into the userscript.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import './style.css'
|
|
119
103
|
```
|
|
120
104
|
|
|
105
|
+
> [!NOTE]
|
|
106
|
+
> Do not put userscript assets in `public/` — those URLs hit the host site and 404. Import the file so Vite inlines it.
|
|
107
|
+
|
|
108
|
+
## HTML pages
|
|
109
|
+
|
|
110
|
+
`index.html` is a normal Vite app next to the userscript. `vite` serves it at `/`. `vite build` writes it to `dist/` beside `{fileName}.user.js`.
|
|
111
|
+
|
|
112
|
+
> [!WARNING]
|
|
113
|
+
> Keep the page's `<script>` entries distinct from `entry`.
|
|
114
|
+
|
|
115
|
+
Script metadata: import `virtual:vite-userscript-plugin` (`name`, `version`, `file`).
|
|
116
|
+
|
|
117
|
+
See [examples/sourcemap](./examples/sourcemap).
|
|
118
|
+
|
|
119
|
+
## Production
|
|
120
|
+
|
|
121
|
+
`vite build` writes `{fileName}.user.js` and `{fileName}.meta.js`. `index.html` is written too, when present.
|
|
122
|
+
|
|
123
|
+
Minify is off (`build.minify`). Sourcemaps are inlined into `.user.js` when `build.sourcemap` is on.
|
|
124
|
+
|
|
125
|
+
See [examples/sourcemap](./examples/sourcemap).
|
|
126
|
+
|
|
127
|
+
## Options
|
|
128
|
+
|
|
129
|
+
`userscript(config)` or `userscript([config, config, …])`. Options are not shared across the array.
|
|
130
|
+
|
|
131
|
+
| Option | Default | Description |
|
|
132
|
+
| --- | --- | --- |
|
|
133
|
+
| `entry` | — | Userscript entry. Required. |
|
|
134
|
+
| `header` | — | Metablock. Required: `name`, `version`, `match`. |
|
|
135
|
+
| `fileName` | sanitized `header.name` | Output base name (`{fileName}.user.js`). |
|
|
136
|
+
| `server.open` | `false` | Open the `.dev.user.js` install URL when Vite starts. |
|
|
137
|
+
| `server.prefix` | `'server:'` | Prefix for `@name` in serve mode. `false` disables it. |
|
|
138
|
+
| `cssInject` | `'auto'` | How production CSS is injected. `'auto'` uses `GM_addStyle` or a `<style>` node. |
|
|
139
|
+
| `align` | `1` | Extra spaces after the longest `@key`. `false` — one space. |
|
|
140
|
+
| `generate` | — | Rewrite the generated metablock. |
|
|
141
|
+
| `autoMetaUrls` | `false` | Fill empty `updateURL` / `downloadURL` from `homepage` / `homepageURL` / `website` / `source`. |
|
|
142
|
+
| `metaFile` | `true` | Emit `{fileName}.meta.js`. |
|
|
143
|
+
|
|
144
|
+
Everything else on `header` follows the manager metablock (`@grant`, `@require`, `@connect`, …).
|
|
145
|
+
|
|
146
|
+
In serve mode the header lists every grant. In production the plugin scans the bundle and writes only the grants in use. `grant: "none"` disables GM APIs and is never mixed with the scan.
|
|
147
|
+
|
|
148
|
+
> [!WARNING]
|
|
149
|
+
> Keep `metaFile: true` if you use `autoMetaUrls`. Otherwise `@updateURL` points at a file that is not emitted.
|
|
150
|
+
|
|
121
151
|
## Examples
|
|
122
152
|
|
|
123
|
-
|
|
153
|
+
| Example | What it shows |
|
|
154
|
+
| --- | --- |
|
|
155
|
+
| [basic](./examples/basic) | Vanilla + SCSS. |
|
|
156
|
+
| [react](./examples/react) | JSX, CSS, React refresh. |
|
|
157
|
+
| [vue](./examples/vue) | SFC `<style>`, minify, sourcemap. |
|
|
158
|
+
| [svelte](./examples/svelte) | SFC `<style>`. |
|
|
159
|
+
| [multiple-entries](./examples/multiple-entries) | Two scripts. |
|
|
160
|
+
| [sourcemap](./examples/sourcemap) | Inline map, HTML page, virtual module. |
|
|
161
|
+
|
|
162
|
+
## FAQ
|
|
163
|
+
|
|
164
|
+
### Scripts fail on Firefox because of CSP
|
|
165
|
+
|
|
166
|
+
> [!WARNING]
|
|
167
|
+
> The host page can block Vite modules from `localhost`. Use a CSP-disable extension, or a browser profile without the site CSP.
|
|
168
|
+
>
|
|
169
|
+
> https://github.com/Tampermonkey/tampermonkey/issues/952#issuecomment-638373937
|
|
170
|
+
|
|
171
|
+
### HTTPS site, HTTP Vite — mixed content
|
|
172
|
+
|
|
173
|
+
> [!WARNING]
|
|
174
|
+
> `https://example.com` will not load `http://localhost:5173`. Serve Vite over HTTPS: [`vite-plugin-mkcert`](https://github.com/liuweiGL/vite-plugin-mkcert) before `userscript()`, or `server.https`.
|
|
175
|
+
|
|
176
|
+
### `public/` assets 404 on the target site
|
|
177
|
+
|
|
178
|
+
> [!NOTE]
|
|
179
|
+
> Userscripts run on someone else’s origin. Import the file so Vite inlines it. `public/` only works for the `index.html` app on the Vite origin.
|
|
180
|
+
|
|
181
|
+
### `@run-at document-start` feels late in dev
|
|
182
|
+
|
|
183
|
+
> [!NOTE]
|
|
184
|
+
> Serve injects `type="module"` (async). Production is a synchronous IIFE unless you use top-level `await`.
|
|
185
|
+
|
|
186
|
+
## Migration from v1
|
|
187
|
+
|
|
188
|
+
| v1 | v2 |
|
|
189
|
+
| --- | --- |
|
|
190
|
+
| `vite build --watch` | `vite` |
|
|
191
|
+
| `esbuildTransformOptions` | removed |
|
|
192
|
+
| `server.port` | Vite `server.port` |
|
|
193
|
+
| minify on by default | off; set `build.minify` |
|
|
194
|
+
| `*.proxy.user.js` + `file://` | `*.dev.user.js` from Vite |
|
|
195
|
+
| Vite 3–7 | Vite 8 |
|
|
196
|
+
| `scripts` + shared `header` | `userscript([config, config, …])` |
|
|
197
|
+
| `ScriptOptions` | removed |
|
|
124
198
|
|
|
125
199
|
## License
|
|
126
200
|
|