-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
vite-mv3-hmr.ts
105 lines (89 loc) · 3.38 KB
/
vite-mv3-hmr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { dirname, join } from 'path'
import type { HMRPayload, PluginOption } from 'vite'
import fs from 'fs-extra'
import { isWin, r } from './scripts/utils'
const targetDir = r('extension')
export const MV3Hmr = (): PluginOption => {
return {
name: 'vite-mv3-hmr',
apply: 'serve',
enforce: 'post',
async configureServer(server) {
const originWsSend: (payload: HMRPayload) => void = server.ws.send
server.ws.send = async function (payload: HMRPayload) {
if (payload.type === 'update') {
for (const update of payload.updates) {
await writeToDisk(update.path)
if (update.acceptedPath !== update.path)
await writeToDisk(update.acceptedPath)
}
payload.updates = payload.updates.map((update) => {
const isJsUpdate = update.type === 'js-update'
if (!isJsUpdate)
return update
return {
...update,
path: `${update.path}.js`,
acceptedPath: `${update.acceptedPath}.js`,
}
})
}
originWsSend.call(this, payload)
}
async function writeToDisk(url: string) {
const result = await server.transformRequest(url.replace(/^\/@id\//, ''))
let code = result?.code
if (!code)
return
const urlModule = await server.moduleGraph.getModuleByUrl(url)
const importedModules = urlModule?.importedModules
if (importedModules) {
for (const mod of importedModules) {
code = code.replace(mod.url, normalizeViteUrl(isWin
? mod.url.replace(/[A-Z]:\//, '').replace(/:/, '.')
: mod.url,
mod.type)) // fix invalid colon in /@fs/C:, /@id/plugin-vue:export-helper
writeToDisk(mod.url)
}
}
if (urlModule?.url) {
code = code
.replace(/\/@vite\/client/g, '/dist/mv3client.mjs')
.replace(/\/@id\//g, '/')
.replace(/__uno.css/g, '~~uno.css')
.replace(/__x00__plugin-vue:export-helper/g, '~~x00__plugin-vue:export-helper.js')
.replace(/(\/\.vite\/deps\/\S+?)\?v=\w+/g, '$1')
if (isWin) {
code = code
.replace(/(from\s+["']\/@fs\/)[A-Z]:\//g, '$1')
}
const targetFile = normalizeFsUrl(isWin
? urlModule.url.replace(/[A-Z]:\//, '').replace(/:/, '.')
: urlModule.url,
urlModule.type) // fix invalid colon in /@fs/C:, /@id/plugin-vue:export-helper
await fs.ensureDir(dirname(targetFile))
await fs.writeFile(targetFile, code)
}
}
Object.keys(server.config.build.rollupOptions.input!).map(entry => writeToDisk(`/${entry}/main.ts`))
},
}
}
function normalizeViteUrl(url: string, type: string) {
url = url.replace(/\?v=\w+$/, '')
if (type === 'js' && !url.endsWith('.js') && !url.endsWith('.mjs'))
url = `${url}.js`.replace(/vue\?/, 'vue.js_')
return url
}
function normalizeFsUrl(url: string, type: string) {
return join(
targetDir,
normalizeViteUrl(url, type)
.replace(/^\//, '')
// `\0plugin-vue:export-helper` EXPORT_HELPER_ID
// eslint-disable-next-line no-control-regex
.replace(/\u0000/g, '__x00__')
// filenames starting with "_" are reserved for use by the system.
.replace(/^_+/, match => '~'.repeat(match.length)),
)
}