-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
48 lines (40 loc) · 1.19 KB
/
plugin.js
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
import colors from 'picocolors'
const getSystemObject = (currentDirName, file, content) => {
const url = file.replace(currentDirName, '')
const parts = url.split('/');
const match = /export const (.*): /.exec(content)
return {
url,
basePath: parts.slice(0, -1),
funcName: match[1],
};
}
export const plugin = () => {
let _server;
return {
name: 'vite-darker-engine',
enforce: 'pre',
handleHotUpdate: async (ctx) => {
if(ctx.file.indexOf('.system.ts') === -1) return
const currentDirName = ctx.server.config.envDir.replaceAll('\\', '/') + '/'
const systemObject = getSystemObject(currentDirName, ctx.file, await ctx.read())
_server.config.logger.info(colors.yellow(`system hot-reload `) + colors.dim(systemObject.funcName), {
clear: true,
timestamp: true,
})
_server.ws.send('dev:system', systemObject)
return []
},
configureServer(server) {
_server = server
},
}
}
export const initViteDarkerEnginePlugin = (hot, onSwapSystem) => {
if (hot) {
hot.on('dev:system', async (systemData) => {
const systemModule = await import(/* @vite-ignore */`/${systemData.url}?a=${Math.trunc(performance.now())}`)
onSwapSystem(systemModule, systemData)
})
}
}