build.js
1.57 KB
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
"use strict";
process.env.NODE_ENV = "production";
const path = require("path");
const webpack = require("webpack");
const config = require("./webpack.prod.conf");
const compiler = webpack(config);
// 以包的形式包装rm -rf命令,删除文件夹
const rm = require("rimraf");
// 美化node控制台进度
const ora = require("ora");
const spinner = ora({
text: "building for production loading...",
color: "yellow",
spinner: { interval: 80, frames: ["-", "+", "-"] },
});
// 开启loading动画
spinner.start();
// 美化控制台输出
const chalk = require("chalk");
const isAdmin = process.env.NODE_ENV_TYPE === "admin";
// 打包前先删除之前可能已打包过的文件
const rmFile = path.resolve(
__dirname,
`../puclic/${isAdmin ? "admin" : "client"}`
);
rm(rmFile, (rmErr) => {
if (rmErr) throw rmErr;
compiler.run((err, stats) => {
spinner.stop();
if (err) {
throw err;
}
if (stats.hasErrors()) {
console.log(chalk.redBright("Build failed with errors.\n"));
process.exit(1);
}
process.stdout.write(
stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false,
}) + "\n\n"
);
console.log(chalk.greenBright("Build completed with success.\n"));
compiler.close((closeErr) => {
if (closeErr) {
console.log(
chalk.redBright(
`compiler close failed with message ${closeErr.message}`
)
);
}
});
});
});