1 | var _path = require("path");
|
2 | var _fs = require("fs");
|
3 | var _webpack = (obj = require("webpack")) && obj.__esModule ? obj : {
|
4 | default: obj
|
5 | };
|
6 | var obj;
|
7 | var _format = require("dr-js/library/common/format");
|
8 | var _File = require("dr-js/library/node/file/File");
|
9 | var _ExitListener = require("dr-js/library/node/system/ExitListener");
|
10 | var _main = require("./main");
|
11 | const getStatsCheck = (onError, onStats) => (error, statsData) => {
|
12 | if (error) return onError(error);
|
13 | if (statsData.hasErrors() || statsData.hasWarnings()) {
|
14 | const {errors = [], warnings = []} = statsData.toJson();
|
15 | errors.forEach(message => console.error(message));
|
16 | warnings.forEach(message => console.warn(message));
|
17 | if (statsData.hasErrors()) return onError(new Error("webpack stats Error"));
|
18 | }
|
19 | onStats(statsData);
|
20 | };
|
21 | const joinTag = (...args) => args.filter(Boolean).join(",");
|
22 | exports.compileWithWebpack = (async ({config, isWatch, profileOutput, assetMapOutput, logger}) => {
|
23 | const {log} = logger;
|
24 | if (profileOutput) {
|
25 | isWatch && console.warn("[watch] warning: skipped generate profileOutput");
|
26 | config.profile = !0;
|
27 | }
|
28 | const compiler = (0, _webpack.default)(config);
|
29 | const logStats = ((isWatch, {padLog, log}) => {
|
30 | const logSingleStats = ({compilation: {assets = {}, chunks = []}, startTime, endTime}) => {
|
31 | startTime && endTime && padLog(`[${isWatch ? "watch" : "compile"}] time: ${(0, _format.time)(endTime - startTime)}`);
|
32 | const table = [];
|
33 | Object.entries(assets).forEach(([name, sourceInfo]) => table.push([ "asset", name, `${(0, _format.binary)(sourceInfo.size())}B`, joinTag(sourceInfo.emitted && "emitted") ]));
|
34 | _main.__VERBOSE__ && chunks.forEach(chunk => table.push([ "chunk", chunk.name || chunk.id, `${(0, _format.binary)(chunk.modulesSize())}B`, joinTag(chunk.canBeInitial() && "initial", chunk.hasRuntime() && "entry", chunk.rendered && "rendered") ]));
|
35 | log(`output:\n ${(0, _format.padTable)({
|
36 | table,
|
37 | padFuncList: [ "L", "R", "R", "L" ],
|
38 | cellPad: " | ",
|
39 | rowPad: "\n "
|
40 | })}`);
|
41 | };
|
42 | return statsData => {
|
43 | if (statsData.compilation) return logSingleStats(statsData);
|
44 | if (statsData.stats) return statsData.stats.map(logSingleStats);
|
45 | console.warn("[getLogStats] unexpected statData", statsData);
|
46 | throw new Error("[getLogStats] unexpected statData");
|
47 | };
|
48 | })(isWatch, logger);
|
49 | if (!isWatch) {
|
50 | log("[compile] start");
|
51 | const statsData = await new Promise((resolve, reject) => compiler.run(getStatsCheck(reject, resolve)));
|
52 | logStats(statsData);
|
53 | let statsDataObject;
|
54 | const getStatsDataObject = () => {
|
55 | void 0 === statsDataObject && (statsDataObject = statsData.toJson());
|
56 | return statsDataObject;
|
57 | };
|
58 | profileOutput && (0, _fs.writeFileSync)(profileOutput, JSON.stringify(getStatsDataObject()));
|
59 | profileOutput && log(`[compile] generated profileOutput at: ${profileOutput}`);
|
60 | assetMapOutput && (0, _fs.writeFileSync)(assetMapOutput, JSON.stringify(getStatsDataObject().assetsByChunkName || {}));
|
61 | assetMapOutput && log(`[compile] generated assetMapOutput at: ${assetMapOutput}`);
|
62 | return statsData;
|
63 | }
|
64 | log("[watch] start");
|
65 | compiler.watch({
|
66 | aggregateTimeout: 512,
|
67 | poll: void 0
|
68 | }, getStatsCheck(error => log(`error: ${error}`), logStats));
|
69 | (0, _ExitListener.addExitListenerSync)(exitState => log(`[watch] exit with state: ${JSON.stringify(exitState)}`));
|
70 | });
|
71 | exports.commonFlag = (async ({argvFlag, fromRoot, profileOutput = fromRoot(".temp-gitignore/profile-stat.json"), assetMapOutput = "", logger: {log}}) => {
|
72 | const mode = argvFlag("development", "production") || "production";
|
73 | const isWatch = Boolean(argvFlag("watch"));
|
74 | const isProduction = "production" === mode;
|
75 | argvFlag("profile") || (profileOutput = null);
|
76 | profileOutput && await (0, _File.createDirectory)((0, _path.dirname)(profileOutput));
|
77 | assetMapOutput && await (0, _File.createDirectory)((0, _path.dirname)(assetMapOutput));
|
78 | log(`compile flag: ${JSON.stringify({
|
79 | mode,
|
80 | isWatch,
|
81 | isProduction,
|
82 | profileOutput,
|
83 | assetMapOutput
|
84 | }, null, " ")}`);
|
85 | return {
|
86 | mode,
|
87 | isWatch,
|
88 | isProduction,
|
89 | profileOutput,
|
90 | assetMapOutput
|
91 | };
|
92 | }); |
\ | No newline at end of file |