-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
52 lines (49 loc) · 1.18 KB
/
vite.config.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
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { basename } from "node:path";
import { Plugin, defineConfig } from "vite";
import builtinModules from "builtin-modules";
const ViteNodeAddonPlugin = (): Plugin => {
return {
name: "native-addon",
apply: "build",
enforce: "pre",
async load(id) {
if (id.endsWith(".node") && existsSync(id)) {
const refId = this.emitFile({
type: "asset",
fileName: basename(id),
source: await readFile(id),
});
const runtimePath = `./${this.getFileName(refId)}`;
return (
`const id = ${JSON.stringify(runtimePath)};` +
`export default require(id);`
);
}
return null;
},
};
};
export default defineConfig({
resolve: {
mainFields: ["module", "main"],
},
plugins: [ViteNodeAddonPlugin()],
build: {
rollupOptions: {
input: "./src/index.js",
output: {
format: "cjs",
entryFileNames: "vite.cjs",
},
},
target: "esnext",
ssr: true,
ssrEmitAssets: true,
},
ssr: {
external: builtinModules as string[],
noExternal: true,
},
});