-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.mjs
70 lines (64 loc) · 1.65 KB
/
rollup.config.mjs
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
import typescript from "rollup-plugin-typescript2";
import babel from "rollup-plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import esbuild from "rollup-plugin-esbuild";
import MagicString from "magic-string";
// NOTE: 不知道为什么... nexe 打包的环境没法识别 node:xxx 的模块... 所以这里简单替换一下
function replaceNodePrefix() {
return {
name: "replace-node-prefix",
renderChunk(code) {
const magicString = new MagicString(code);
let hasReplaced = false;
const regex = /require\('node:(\w+)'\)/g;
let match;
while ((match = regex.exec(code)) !== null) {
magicString.overwrite(
match.index,
match.index + match[0].length,
`require('${match[1]}')`
);
hasReplaced = true;
}
if (!hasReplaced) {
return null;
}
return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true }),
};
},
};
}
export default {
input: "src/server.ts",
output: {
dir: "output",
format: "cjs",
},
plugins: [
json(),
commonjs({
include: /node_modules/,
}),
esbuild({
include: /\.[jt]sx?$/,
exclude: [],
sourceMap: false,
minify: process.env.NODE_ENV === "production",
target: "node14",
define: {
"process.env.NODE_ENV": `"${process.env.NODE_ENV}"`,
},
tsconfig: "tsconfig.json",
loaders: {
".json": "json",
".js": "jsx",
},
}),
resolve(),
replaceNodePrefix(),
],
};