-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.ts
More file actions
54 lines (46 loc) · 1.43 KB
/
plugin.ts
File metadata and controls
54 lines (46 loc) · 1.43 KB
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
import {HmrContext, Plugin} from "vite";
import colors from 'picocolors'
type SystemData = {
url: string,
basePath: string[],
funcName: string
}
const getSystemObject = (currentDirName: string, file: string, content: string): SystemData => {
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 = (): Plugin => {
let _server;
return {
name: 'vite-darker-engine',
enforce: 'pre',
handleHotUpdate: async (ctx: HmrContext) => {
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: (systemModule: any, systemData: SystemData) => void) => {
if (hot) {
hot.on('dev:system', async (systemData: SystemData) => {
const systemModule = await import(/* @vite-ignore */`/${systemData.url}?a=${Math.trunc(performance.now())}`)
onSwapSystem(systemModule, systemData)
})
}
}