Skip to content

Commit 691427e

Browse files
committed
chore: refactor
1 parent d9686d3 commit 691427e

File tree

7 files changed

+31
-44
lines changed

7 files changed

+31
-44
lines changed

FAQ.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Tampermonkey FAQ
2+
3+
https://github.com/Tampermonkey/tampermonkey/issues/952#issuecomment-638373937
4+
https://github.com/Tampermonkey/tampermonkey/issues/977#issuecomment-657268478

examples/basic/vite.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ export default defineConfig((config) => {
1010
header: {
1111
name,
1212
version,
13-
match: [
14-
'*://example.com',
15-
'*://example.org',
16-
'*://example.edu'
17-
]
13+
match: '*://example.*'
1814
},
1915
server: {
2016
port: 3000

examples/jsx/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig((config) => {
1212
header: {
1313
name,
1414
version,
15-
match: '*://example.com'
15+
match: '*://example.*'
1616
},
1717
server: {
1818
port: 4000

src/hot-reload.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/index.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ export default function UserscriptPlugin(
120120
const userFilePath = resolve(rootDir, outDir, userFilename)
121121
const proxyFilePath = resolve(rootDir, outDir, proxyFilename)
122122
const metaFilePath = resolve(rootDir, outDir, metaFilename)
123-
const hotReloadPath = resolve(
124-
workdir,
125-
`hot-reload-${config.header.name}.js`
126-
)
123+
const wsPath = resolve(workdir, `ws-${config.header.name}.js`)
127124

128125
try {
129126
let source = readFileSync(outPath, 'utf8')
@@ -141,25 +138,22 @@ export default function UserscriptPlugin(
141138
)
142139

143140
if (isBuildWatch) {
144-
const hotReloadFile = readFileSync(
145-
resolve(workdir, 'hot-reload.js'),
146-
'utf8'
147-
)
141+
const wsFile = readFileSync(resolve(workdir, 'ws.js'), 'utf8')
148142

149-
const hotReloadScript = await transform({
150-
file: hotReloadFile.replace('__WS__', `ws://localhost:${port}`),
151-
name: hotReloadPath,
143+
const wsScript = await transform({
144+
file: wsFile.replace('__WS__', `ws://localhost:${port}`),
145+
name: wsPath,
152146
loader: 'js'
153147
})
154148

155-
writeFileSync(hotReloadPath, hotReloadScript)
149+
writeFileSync(wsPath, wsScript)
156150
writeFileSync(
157151
proxyFilePath,
158152
new Banner({
159153
...config.header,
160154
require: [
161155
...config.header.require!,
162-
'file://' + hotReloadPath,
156+
'file://' + wsPath,
163157
'file://' + outPath
164158
]
165159
}).generate()

src/ws.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function connection() {
2+
const ws = new WebSocket('__WS__')
3+
4+
ws.addEventListener('close', () => {
5+
setTimeout(connection, 1000)
6+
})
7+
8+
ws.addEventListener('error', () => {
9+
ws.close()
10+
})
11+
12+
ws.addEventListener('message', () => {
13+
location.reload()
14+
})
15+
}
16+
17+
connection()

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig((option) => {
1212
watch: option.watch
1313
},
1414
{
15-
entry: ['src/hot-reload.ts'],
15+
entry: ['src/ws.ts'],
1616
format: ['esm'],
1717
clean: true,
1818
minify: true,

0 commit comments

Comments
 (0)