-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-build.js
56 lines (46 loc) · 1.78 KB
/
rollup-build.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
// rollup-build.js
const fs = require("fs");
const rollup = require("rollup");
const { babel, getBabelOutputPlugin } = require("@rollup/plugin-babel");
const del = require("rollup-plugin-delete");
const json = require("@rollup/plugin-json");
const commonjs = require("@rollup/plugin-commonjs");
const { terser } = require("rollup-plugin-terser");
// 获取根目录的'package.json'
const packageJSON = require("./package.json");
// 读取 生成模式 下需要的依赖包
const packageJSONForProduction = {
name: packageJSON.name,
dependencies: packageJSON.dependencies,
};
const inputOptions = {
input: "./app.js",
plugins: [
// 打包前先清空输出文件夹
del({ targets: "./server/*" }),
// babel 相关的配置, 主要是做兼容
getBabelOutputPlugin({
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
plugins: [["@babel/plugin-transform-runtime", { useESModules: false }]],
}),
babel({ babelHelpers: "bundled", exclude: "node_modules/**" }),
// 这里是把入口文件(app.js)以外的业务代码也进行打包(require进来的文件)
json(),
commonjs(),
// 代码的压缩或混淆
terser(),
],
};
const outputOptions = { dir: "./server", format: "cjs" };
async function build() {
// create a bundle
const bundle = await rollup.rollup(inputOptions);
// generate code and a sourcemap
// const { code, map } = await bundle.generate(outputOptions);
// or write the bundle to disk
await bundle.write(outputOptions);
// 生成生产模式的 package.json, 在服务器上使用
const writeStream = fs.createWriteStream("./server/package.json");
writeStream.write(JSON.stringify(packageJSONForProduction));
}
build();