forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-common.js
75 lines (64 loc) · 2.31 KB
/
build-common.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const cp = require("child_process");
const path = require("path");
const tc = require("./template-common");
const fs = require("fs");
const async = require("neo-async");
const extraArgs = "";
const targetArgs = global.NO_TARGET_ARGS ? "" : "--entry ./example.js --output-filename output.js";
const displayReasons = global.NO_REASONS ? "" : "--stats-reasons --stats-used-exports --stats-provided-exports";
const statsArgs = global.NO_STATS_OPTIONS ? "" : "--stats-chunks --stats-modules-space 99999 --stats-chunk-origins";
const publicPathArgs = global.NO_PUBLIC_PATH ? "" : '--output-public-path "dist/"';
const commonArgs = `--no-stats-colors ${statsArgs} ${publicPathArgs} ${extraArgs} ${targetArgs}`;
let readme = fs.readFileSync(require("path").join(process.cwd(), "template.md"), "utf-8");
const doCompileAndReplace = (args, prefix, callback) => {
if (!tc.needResults(readme, prefix)) {
callback();
return;
}
const deleteFiles = (dir) => {
const targetDir = path.resolve("dist", dir);
if (path.extname(targetDir) === "") {
fs.readdirSync(targetDir).forEach((file) => {
deleteFiles(path.join(targetDir, file));
});
} else {
fs.unlinkSync(targetDir);
}
};
if (fs.existsSync("dist")) {
for (const dir of fs.readdirSync("dist")) {
deleteFiles(dir);
}
}
try {
require.resolve("webpack-cli");
} catch (e) {
throw new Error("Please install webpack-cli at root.");
}
cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${args} ${displayReasons} ${commonArgs}`, (error, stdout, stderr) => {
if (stderr)
console.log(stderr);
if (error !== null)
console.log(error);
try {
readme = tc.replaceResults(readme, process.cwd(), stdout.replace(/[\r?\n]*$/, ""), prefix);
} catch (e) {
console.log(stderr);
throw e;
}
callback();
});
};
async.series([
callback => doCompileAndReplace("--mode production --env production", "production", callback),
callback => doCompileAndReplace("--mode development --env development --devtool none", "development", callback),
callback => doCompileAndReplace("--mode none --env none --output-pathinfo verbose", "", callback)
], () => {
readme = tc.replaceBase(readme);
fs.writeFile("README.md", readme, "utf-8", function () { });
});