forked from neighbads/chatgpt-utools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.preload.ts
42 lines (36 loc) · 1.04 KB
/
vite.preload.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
// vite-plugin-preload.ts
import commonjs from '@rollup/plugin-commonjs'
import nodeResolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import path from 'path'
import { rollup, RollupOptions } from 'rollup'
import { Plugin } from 'vite'
export function vitePluginPreload(preloadPath: string, hook: string): Plugin {
preloadPath = path.resolve(process.cwd(), preloadPath)
async function buildPreload() {
const options: RollupOptions = {
input: preloadPath,
output: {
inlineDynamicImports: true,
name: 'preload',
file: './dist/preload.js',
format: 'cjs',
},
plugins: [nodeResolve(), commonjs(), typescript()],
}
const bundle = await rollup(options)
await bundle.write(options.output as any)
}
return {
name: 'vite-plugin-preload',
enforce: 'post',
[hook]: async () => {
await buildPreload()
},
async handleHotUpdate(ctx) {
if (ctx.file === preloadPath) {
await buildPreload()
}
},
}
}