-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathbuild-common.js
More file actions
177 lines (162 loc) · 4.4 KB
/
Copy pathbuild-common.js
File metadata and controls
177 lines (162 loc) · 4.4 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const cp = require("child_process");
const fs = require("fs");
const path = require("path");
const async = require("neo-async");
const tc = require("./template-common");
const extraArgs = "";
/**
* @typedef {object} GlobalObj
* @property {boolean=} NO_TARGET_ARGS no target args flag
* @property {boolean=} NO_REASONS no stats reasons flag
* @property {boolean=} NO_STATS_OPTIONS no stats options flag
* @property {boolean=} NO_PUBLIC_PATH no public path flag
* @property {boolean=} STATS_COLORS no stats color flag
*/
const globalObj = /** @type {typeof globalThis & GlobalObj} */ (global);
const targetArgs = globalObj
? ""
: "--entry ./example.js --output-filename output.js";
const displayReasons = globalObj
? ""
: "--stats-reasons --stats-used-exports --stats-provided-exports";
const statsArgs = globalObj
? ""
: "--stats-chunks --stats-modules-space 99999 --stats-chunk-origins";
const publicPathArgs = globalObj.NO_PUBLIC_PATH
? ""
: '--output-public-path "dist/"';
const statsColorsArg = globalObj.STATS_COLORS ? "" : "--no-color";
const commonArgs = `${statsColorsArg} ${statsArgs} ${publicPathArgs} ${extraArgs} ${targetArgs}`;
let readme = fs.readFileSync(
require("path").join(process.cwd(), "template.md"),
"utf8"
);
/**
* @param {string} args args
* @param {string} prefix prefix
* @param {() => void} callback callback
*/
const doCompileAndReplace = (args, prefix, callback) => {
if (!tc.needResults(readme, prefix)) {
callback();
return;
}
/**
* @param {string} dir the directory for deleting
*/
const deleteFiles = (dir) => {
const targetDir = path.resolve("dist", dir);
if (path.extname(targetDir) === "") {
for (const file of fs.readdirSync(targetDir)) {
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 (err) {
throw new Error("Please install webpack-cli at root.", { cause: err });
}
/**
* @param {import("child_process").ChildProcess} subprocess a subprocess
*/
const connectIO = (subprocess) => {
const { stdin, stdout, stderr } = process;
const { stdin: _stdin, stdout: _stdout, stderr: _stderr } = subprocess;
/** @type {[NodeJS.ReadStream, import("stream").Writable][]} */
const inputPair = [
[stdin, /** @type {import("stream").Writable} */ (_stdin)]
];
/** @type {[NodeJS.WritableStream, import("stream").Readable][]} */
const outputPair = [
[stdout, /** @type {import("stream").Readable} */ (_stdout)],
[stderr, /** @type {import("stream").Readable} */ (_stderr)]
];
for (const pair of inputPair) {
pair[0].pipe(pair[1]);
}
for (const pair of outputPair) {
pair[1].pipe(pair[0]);
}
disconnectIO = () => {
for (const pair of inputPair) {
pair[0].unpipe(pair[1]);
}
for (const pair of outputPair) {
pair[1].unpipe(pair[0]);
}
};
};
/** @type {null | (() => void)} */
let disconnectIO = null;
const subprocess = cp.exec(
`node ${path.resolve(__dirname, "../bin/webpack.js")} ${args} ${displayReasons} ${commonArgs}`,
(error, stdout, stderr) => {
// eslint-disable-next-line no-unused-expressions
disconnectIO && disconnectIO();
if (stderr) console.log(stderr);
if (error !== null) {
console.log(error);
throw error;
}
try {
readme = tc.replaceResults(
readme,
process.cwd(),
stdout
.replace(/[\r?\n]*$/, "")
.replace(
/\d\d\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])/g,
"XXXX-XX-XX"
)
.replace(/([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/g, "XXXX:XX:XX")
.replace(/webpack [0-9.]+/g, "webpack X.X.X"),
prefix
);
} catch (err) {
console.log(stderr);
throw err;
}
callback();
}
);
connectIO(subprocess);
};
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, "utf8", () => {});
}
);