@aniyajs/rotor
Version:
基于webpack5开发的一款专注于打包、运行的工具
113 lines (92 loc) • 2.98 kB
JavaScript
;
// 确定当前构建环境
process.env.NODE_ENV = "production";
// 抛出所有未被捕获的错误
process.on("unhandledRejection", (error) => {
throw error;
});
const fs = require("fs-extra");
const paths = require("../utils/paths");
if (!fs.existsSync(paths.appConfigTempIndexJs)) {
process.exit(1);
}
const chalk = require("chalk");
const { checkBrowsers } = require("../utils/browsHelper");
const webpack = require("webpack");
const clientEnv = require("./env")();
const formatWebpackMessages = require("../utils/formatWebpackMessages");
const printBuildError = require("../utils/printBuildError");
const { printFileSizesAfterBuild } = require("../utils/FileSizeReporter");
const isInteractive = process.stdout.isTTY;
// Generate config.
const config = clientEnv.latestConfig;
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// Delete everything, but save the directory.
fs.emptyDirSync(paths.appBuild);
// Merge with a public folder.
fs.existsSync(paths.appPublic) &&
fs.copySync(paths.appPublic, paths.appBuild, {
dereference: true,
});
// Start the webpack build.
return build();
})
.then(
async ({ stats, warnings }) => {
console.log(
// eslint-disable-next-line quotes
`${chalk.bgGreen(` DONE `)} ${chalk.green("Compiled successfully.")}\n`,
);
await printFileSizesAfterBuild(stats, paths.appBuild, paths.appPath);
},
(err) => {
printBuildError(err);
},
)
.catch((err) => {
console.log(err.message || err);
process.exit(1);
});
// Creating the production build and printing the deploy information.
function build() {
console.log("Create a optimize production build....");
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
let messages;
if (err) {
if (!err.message) {
return reject(err);
}
let errMessage = err.message;
// Add additional information for postcss errors.
if (Object.prototype.hasOwnProperty.call(err, "postcssNode")) {
errMessage +=
"\nCompileError:Begins at CSS selector " + err.postcssNode.selector;
}
messages = formatWebpackMessages({
errors: [errMessage],
warnings: [],
});
} else {
messages = formatWebpackMessages(
stats.toJson({ all: false, warnings: true, errors: true }),
);
}
if (messages.errors.length) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (messages.errors.length > 1) {
messages.errors.length = 1;
}
return reject(new Error(messages.errors.join("\n\n")));
}
const resolveArgs = {
stats,
warnings: messages.warnings,
};
return resolve(resolveArgs);
});
});
}