-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.server.js
170 lines (157 loc) · 4.13 KB
/
rollup.server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
/* eslint-disable no-console */
// yarn add esbuild rollup rollup-plugin-esbuild rollup-plugin-node-resolve rollup-plugin-uglify typescript -D
const rollup = require("rollup");
const { resolve } = require("path");
const pwd = (...args) => resolve(process.cwd(), ...args);
const pkg = require("./package.json");
const fs = require("fs-extra");
const esbuild = require("rollup-plugin-esbuild");
const argv = process.argv.splice(2);
const cluster = require("cluster");
const errorPath = "./node_modules/.cache_rollup_server_error.js";
if (cluster.isWorker) {
// 监听Promise没有被捕获的失败函数
process.on("unhandledRejection", function (err, promise) {
console.error("222", err);
});
try {
require("./dist/index.js");
} catch (error) {
console.error(error);
}
return;
}
let env = "dev";
let isWatch = false;
argv.forEach((item) => {
if (item === "-w") {
isWatch = true;
} else if (/=/.test(item)) {
const [k, v] = item.split("=");
if (k === "env") {
env = v;
}
}
});
function clearDir(dir) {
if (fs.existsSync(dir)) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
fs.remove("${dir}/${file}");
});
} else {
fs.mkdirSync("dist");
}
}
function haveArgv(...args) {
let isHave = false;
args.forEach((str) => {
argv.forEach((v) => {
if (v === str) {
isHave = true;
}
});
});
return isHave;
}
clearDir(pwd("dist"));
function copyEnv() {
// 拷贝环境变量
let envFile = `env/env.${env}.js`;
if (!fs.existsSync(envFile)) {
envFile = "env/env.test.js";
}
fs.copyFileSync(envFile, "dist/env.js");
}
copyEnv();
const watchOptions = {
external: [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.devDependencies || {}),
"crypto",
"cluster",
"path",
"fs",
"ws",
"http",
"url",
],
input: "./server/app.ts",
output: {
file: "./dist/index.js",
format: "cjs",
name: "fast",
sourcemap: true,
globals: {
react: "React",
},
},
plugins: [
esbuild({
// All options are optional
include: /\.(ts|tsx|js|jsx)?$/, // default, inferred from `loaders` option
exclude: /node_modules/, // default
sourceMap: false, // default
target: "es2017", // default, or 'es20XX', 'esnext'
// watch: process.argv.includes("--watch"),
// minify: process.env.NODE_ENV === "production",
}),
],
};
const watcher = rollup.watch(watchOptions);
const copyList = ["yarn.lock"];
const copyDirList = ["static"];
let firstForkLock = false;
// event.code can be one of:
// START — the watcher is (re)starting
// BUNDLE_START — building an individual bundle
// BUNDLE_END — finished building a bundle
// END — finished building all bundles
// ERROR — encountered an error while bundling
// FATAL — encountered an unrecoverable error
watcher.on("event", (event) => {
if (event.code === "ERROR") {
console.log("ERROR", event);
} else if (event.code === "BUNDLE_END") {
for (const id in cluster.workers) {
cluster.workers[id].process.kill();
}
// if (!isWatch && cluster.isMaster) {
// console.log("BUNDLE_END");
// }
} else if (event.code === "END") {
// yarn ser 自动重启处理
if (!firstForkLock && isWatch && cluster.isMaster) {
firstForkLock = true;
cluster.fork();
cluster.on("exit", (worker, code, signal) => {
cluster.fork();
});
}
Object.keys(pkg.devDependencies).forEach((k) => {
if (
/(@type|eslint|vite|cypress|rollup|prettier|husky|lint-staged|typescript|nodemon)/.test(
k
)
) {
delete pkg.devDependencies[k];
}
});
fs.writeJSONSync("./dist/package.json", pkg, { spaces: 2 });
copyList.forEach((f) => {
if (fs.existsSync(f)) {
fs.copyFileSync(f, "./dist/" + f);
}
});
copyEnv();
copyDirList.forEach((dir) => {
fs.readdirSync(dir).forEach((f) => {
fs.copySync(dir + "/" + f, "./dist/" + f);
});
});
if (!haveArgv("--watch", "-w")) {
watcher.close();
}
}
});