1 |
3a515b92
|
cagy
|
#!/usr/bin/env node
|
2 |
|
|
|
3 |
|
|
/*
|
4 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
5 |
|
|
Author Tobias Koppers @sokra
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
const { NON_COMPILATION_ARGS } = require("./utils/constants");
|
9 |
|
|
|
10 |
|
|
(function() {
|
11 |
|
|
// wrap in IIFE to be able to use return
|
12 |
|
|
|
13 |
|
|
const importLocal = require("import-local");
|
14 |
|
|
// Prefer the local installation of webpack-cli
|
15 |
|
|
if (importLocal(__filename)) {
|
16 |
|
|
return;
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
require("v8-compile-cache");
|
20 |
|
|
|
21 |
|
|
const ErrorHelpers = require("./utils/errorHelpers");
|
22 |
|
|
|
23 |
|
|
const NON_COMPILATION_CMD = process.argv.find(arg => {
|
24 |
|
|
if (arg === "serve") {
|
25 |
|
|
global.process.argv = global.process.argv.filter(a => a !== "serve");
|
26 |
|
|
process.argv = global.process.argv;
|
27 |
|
|
}
|
28 |
|
|
return NON_COMPILATION_ARGS.find(a => a === arg);
|
29 |
|
|
});
|
30 |
|
|
|
31 |
|
|
if (NON_COMPILATION_CMD) {
|
32 |
|
|
return require("./utils/prompt-command")(NON_COMPILATION_CMD, ...process.argv);
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
const yargs = require("yargs").usage(`webpack-cli ${require("../package.json").version}
|
36 |
|
|
|
37 |
|
|
Usage: webpack-cli [options]
|
38 |
|
|
webpack-cli [options] --entry <entry> --output <output>
|
39 |
|
|
webpack-cli [options] <entries...> --output <output>
|
40 |
|
|
webpack-cli <command> [options]
|
41 |
|
|
|
42 |
|
|
For more information, see https://webpack.js.org/api/cli/.`);
|
43 |
|
|
|
44 |
|
|
require("./config/config-yargs")(yargs);
|
45 |
|
|
|
46 |
|
|
// yargs will terminate the process early when the user uses help or version.
|
47 |
|
|
// This causes large help outputs to be cut short (https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4#process).
|
48 |
|
|
// To prevent this we use the yargs.parse API and exit the process normally
|
49 |
|
|
yargs.parse(process.argv.slice(2), (err, argv, output) => {
|
50 |
|
|
Error.stackTraceLimit = 30;
|
51 |
|
|
|
52 |
|
|
// arguments validation failed
|
53 |
|
|
if (err && output) {
|
54 |
|
|
console.error(output);
|
55 |
|
|
process.exitCode = 1;
|
56 |
|
|
return;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
// help or version info
|
60 |
|
|
if (output) {
|
61 |
|
|
console.log(output);
|
62 |
|
|
return;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
if (argv.verbose) {
|
66 |
|
|
argv["display"] = "verbose";
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
let options;
|
70 |
|
|
try {
|
71 |
|
|
options = require("./utils/convert-argv")(argv);
|
72 |
|
|
} catch (err) {
|
73 |
|
|
if (err.code === "MODULE_NOT_FOUND") {
|
74 |
|
|
const moduleName = err.message.split("'")[1];
|
75 |
|
|
let instructions = "";
|
76 |
|
|
let errorMessage = "";
|
77 |
|
|
|
78 |
|
|
if (moduleName === "webpack") {
|
79 |
|
|
errorMessage = `\n${moduleName} not installed`;
|
80 |
|
|
instructions = `Install webpack to start bundling: \u001b[32m\n $ npm install --save-dev ${moduleName}\n`;
|
81 |
|
|
|
82 |
|
|
if (process.env.npm_execpath !== undefined && process.env.npm_execpath.includes("yarn")) {
|
83 |
|
|
instructions = `Install webpack to start bundling: \u001b[32m\n $ yarn add ${moduleName} --dev\n`;
|
84 |
|
|
}
|
85 |
|
|
Error.stackTraceLimit = 1;
|
86 |
|
|
console.error(`${errorMessage}\n\n${instructions}`);
|
87 |
|
|
process.exitCode = 1;
|
88 |
|
|
return;
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
if (err.name !== "ValidationError") {
|
93 |
|
|
throw err;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
const stack = ErrorHelpers.cleanUpWebpackOptions(err.stack, err.message);
|
97 |
|
|
const message = err.message + "\n" + stack;
|
98 |
|
|
|
99 |
|
|
if (argv.color) {
|
100 |
|
|
console.error(`\u001b[1m\u001b[31m${message}\u001b[39m\u001b[22m`);
|
101 |
|
|
} else {
|
102 |
|
|
console.error(message);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
process.exitCode = 1;
|
106 |
|
|
return;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* When --silent flag is present, an object with a no-op write method is
|
111 |
|
|
* used in place of process.stout
|
112 |
|
|
*/
|
113 |
|
|
const stdout = argv.silent ? { write: () => {} } : process.stdout;
|
114 |
|
|
|
115 |
|
|
function ifArg(name, fn, init) {
|
116 |
|
|
if (Array.isArray(argv[name])) {
|
117 |
|
|
if (init) init();
|
118 |
|
|
argv[name].forEach(fn);
|
119 |
|
|
} else if (typeof argv[name] !== "undefined") {
|
120 |
|
|
if (init) init();
|
121 |
|
|
fn(argv[name], -1);
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
function processOptions(options) {
|
126 |
|
|
// process Promise
|
127 |
|
|
if (typeof options.then === "function") {
|
128 |
|
|
options.then(processOptions).catch(function(err) {
|
129 |
|
|
console.error(err.stack || err);
|
130 |
|
|
// eslint-disable-next-line no-process-exit
|
131 |
|
|
process.exit(1);
|
132 |
|
|
});
|
133 |
|
|
return;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
const firstOptions = [].concat(options)[0];
|
137 |
|
|
const statsPresetToOptions = require("webpack").Stats.presetToOptions;
|
138 |
|
|
|
139 |
|
|
let outputOptions = options.stats;
|
140 |
|
|
if (typeof outputOptions === "boolean" || typeof outputOptions === "string") {
|
141 |
|
|
outputOptions = statsPresetToOptions(outputOptions);
|
142 |
|
|
} else if (!outputOptions) {
|
143 |
|
|
outputOptions = {};
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
ifArg("display", function(preset) {
|
147 |
|
|
outputOptions = statsPresetToOptions(preset);
|
148 |
|
|
});
|
149 |
|
|
|
150 |
|
|
outputOptions = Object.create(outputOptions);
|
151 |
|
|
if (Array.isArray(options) && !outputOptions.children) {
|
152 |
|
|
outputOptions.children = options.map(o => o.stats);
|
153 |
|
|
}
|
154 |
|
|
if (typeof outputOptions.context === "undefined") outputOptions.context = firstOptions.context;
|
155 |
|
|
|
156 |
|
|
ifArg("env", function(value) {
|
157 |
|
|
if (outputOptions.env) {
|
158 |
|
|
outputOptions._env = value;
|
159 |
|
|
}
|
160 |
|
|
});
|
161 |
|
|
|
162 |
|
|
ifArg("json", function(bool) {
|
163 |
|
|
if (bool) {
|
164 |
|
|
outputOptions.json = bool;
|
165 |
|
|
outputOptions.modules = bool;
|
166 |
|
|
}
|
167 |
|
|
});
|
168 |
|
|
|
169 |
|
|
if (typeof outputOptions.colors === "undefined") outputOptions.colors = require("supports-color").stdout;
|
170 |
|
|
|
171 |
|
|
ifArg("sort-modules-by", function(value) {
|
172 |
|
|
outputOptions.modulesSort = value;
|
173 |
|
|
});
|
174 |
|
|
|
175 |
|
|
ifArg("sort-chunks-by", function(value) {
|
176 |
|
|
outputOptions.chunksSort = value;
|
177 |
|
|
});
|
178 |
|
|
|
179 |
|
|
ifArg("sort-assets-by", function(value) {
|
180 |
|
|
outputOptions.assetsSort = value;
|
181 |
|
|
});
|
182 |
|
|
|
183 |
|
|
ifArg("display-exclude", function(value) {
|
184 |
|
|
outputOptions.exclude = value;
|
185 |
|
|
});
|
186 |
|
|
|
187 |
|
|
if (!outputOptions.json) {
|
188 |
|
|
if (typeof outputOptions.cached === "undefined") outputOptions.cached = false;
|
189 |
|
|
if (typeof outputOptions.cachedAssets === "undefined") outputOptions.cachedAssets = false;
|
190 |
|
|
|
191 |
|
|
ifArg("display-chunks", function(bool) {
|
192 |
|
|
if (bool) {
|
193 |
|
|
outputOptions.modules = false;
|
194 |
|
|
outputOptions.chunks = true;
|
195 |
|
|
outputOptions.chunkModules = true;
|
196 |
|
|
}
|
197 |
|
|
});
|
198 |
|
|
|
199 |
|
|
ifArg("display-entrypoints", function(bool) {
|
200 |
|
|
outputOptions.entrypoints = bool;
|
201 |
|
|
});
|
202 |
|
|
|
203 |
|
|
ifArg("display-reasons", function(bool) {
|
204 |
|
|
if (bool) outputOptions.reasons = true;
|
205 |
|
|
});
|
206 |
|
|
|
207 |
|
|
ifArg("display-depth", function(bool) {
|
208 |
|
|
if (bool) outputOptions.depth = true;
|
209 |
|
|
});
|
210 |
|
|
|
211 |
|
|
ifArg("display-used-exports", function(bool) {
|
212 |
|
|
if (bool) outputOptions.usedExports = true;
|
213 |
|
|
});
|
214 |
|
|
|
215 |
|
|
ifArg("display-provided-exports", function(bool) {
|
216 |
|
|
if (bool) outputOptions.providedExports = true;
|
217 |
|
|
});
|
218 |
|
|
|
219 |
|
|
ifArg("display-optimization-bailout", function(bool) {
|
220 |
|
|
if (bool) outputOptions.optimizationBailout = bool;
|
221 |
|
|
});
|
222 |
|
|
|
223 |
|
|
ifArg("display-error-details", function(bool) {
|
224 |
|
|
if (bool) outputOptions.errorDetails = true;
|
225 |
|
|
});
|
226 |
|
|
|
227 |
|
|
ifArg("display-origins", function(bool) {
|
228 |
|
|
if (bool) outputOptions.chunkOrigins = true;
|
229 |
|
|
});
|
230 |
|
|
|
231 |
|
|
ifArg("display-max-modules", function(value) {
|
232 |
|
|
outputOptions.maxModules = +value;
|
233 |
|
|
});
|
234 |
|
|
|
235 |
|
|
ifArg("display-cached", function(bool) {
|
236 |
|
|
if (bool) outputOptions.cached = true;
|
237 |
|
|
});
|
238 |
|
|
|
239 |
|
|
ifArg("display-cached-assets", function(bool) {
|
240 |
|
|
if (bool) outputOptions.cachedAssets = true;
|
241 |
|
|
});
|
242 |
|
|
|
243 |
|
|
if (!outputOptions.exclude) outputOptions.exclude = ["node_modules", "bower_components", "components"];
|
244 |
|
|
|
245 |
|
|
if (argv["display-modules"]) {
|
246 |
|
|
outputOptions.maxModules = Infinity;
|
247 |
|
|
outputOptions.exclude = undefined;
|
248 |
|
|
outputOptions.modules = true;
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
ifArg("hide-modules", function(bool) {
|
253 |
|
|
if (bool) {
|
254 |
|
|
outputOptions.modules = false;
|
255 |
|
|
outputOptions.chunkModules = false;
|
256 |
|
|
}
|
257 |
|
|
});
|
258 |
|
|
|
259 |
|
|
ifArg("info-verbosity", function(value) {
|
260 |
|
|
outputOptions.infoVerbosity = value;
|
261 |
|
|
});
|
262 |
|
|
|
263 |
|
|
ifArg("build-delimiter", function(value) {
|
264 |
|
|
outputOptions.buildDelimiter = value;
|
265 |
|
|
});
|
266 |
|
|
|
267 |
|
|
const webpack = require("webpack");
|
268 |
|
|
|
269 |
|
|
let lastHash = null;
|
270 |
|
|
let compiler;
|
271 |
|
|
try {
|
272 |
|
|
compiler = webpack(options);
|
273 |
|
|
} catch (err) {
|
274 |
|
|
if (err.name === "WebpackOptionsValidationError") {
|
275 |
|
|
if (argv.color) console.error(`\u001b[1m\u001b[31m${err.message}\u001b[39m\u001b[22m`);
|
276 |
|
|
else console.error(err.message);
|
277 |
|
|
// eslint-disable-next-line no-process-exit
|
278 |
|
|
process.exit(1);
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
throw err;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
if (argv.progress) {
|
285 |
|
|
const ProgressPlugin = require("webpack").ProgressPlugin;
|
286 |
|
|
new ProgressPlugin({
|
287 |
|
|
profile: argv.profile
|
288 |
|
|
}).apply(compiler);
|
289 |
|
|
}
|
290 |
|
|
if (outputOptions.infoVerbosity === "verbose") {
|
291 |
|
|
if (argv.w) {
|
292 |
|
|
compiler.hooks.watchRun.tap("WebpackInfo", compilation => {
|
293 |
|
|
const compilationName = compilation.name ? compilation.name : "";
|
294 |
|
|
console.error("\nCompilation " + compilationName + " starting…\n");
|
295 |
|
|
});
|
296 |
|
|
} else {
|
297 |
|
|
compiler.hooks.beforeRun.tap("WebpackInfo", compilation => {
|
298 |
|
|
const compilationName = compilation.name ? compilation.name : "";
|
299 |
|
|
console.error("\nCompilation " + compilationName + " starting…\n");
|
300 |
|
|
});
|
301 |
|
|
}
|
302 |
|
|
compiler.hooks.done.tap("WebpackInfo", compilation => {
|
303 |
|
|
const compilationName = compilation.name ? compilation.name : "";
|
304 |
|
|
console.error("\nCompilation " + compilationName + " finished\n");
|
305 |
|
|
});
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
function compilerCallback(err, stats) {
|
309 |
|
|
if (!options.watch || err) {
|
310 |
|
|
// Do not keep cache anymore
|
311 |
|
|
compiler.purgeInputFileSystem();
|
312 |
|
|
}
|
313 |
|
|
if (err) {
|
314 |
|
|
lastHash = null;
|
315 |
|
|
console.error(err.stack || err);
|
316 |
|
|
if (err.details) console.error(err.details);
|
317 |
|
|
process.exitCode = 1;
|
318 |
|
|
return;
|
319 |
|
|
}
|
320 |
|
|
if (outputOptions.json) {
|
321 |
|
|
stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
|
322 |
|
|
} else if (stats.hash !== lastHash) {
|
323 |
|
|
lastHash = stats.hash;
|
324 |
|
|
if (stats.compilation && stats.compilation.errors.length !== 0) {
|
325 |
|
|
const errors = stats.compilation.errors;
|
326 |
|
|
if (errors[0].name === "EntryModuleNotFoundError") {
|
327 |
|
|
console.error("\n\u001b[1m\u001b[31mInsufficient number of arguments or no entry found.");
|
328 |
|
|
console.error(
|
329 |
|
|
"\u001b[1m\u001b[31mAlternatively, run 'webpack(-cli) --help' for usage info.\u001b[39m\u001b[22m\n"
|
330 |
|
|
);
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
const statsString = stats.toString(outputOptions);
|
334 |
|
|
const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : "";
|
335 |
|
|
if (statsString) stdout.write(`${statsString}\n${delimiter}`);
|
336 |
|
|
}
|
337 |
|
|
if (!options.watch && stats.hasErrors()) {
|
338 |
|
|
process.exitCode = 2;
|
339 |
|
|
}
|
340 |
|
|
}
|
341 |
|
|
if (firstOptions.watch || options.watch) {
|
342 |
|
|
const watchOptions =
|
343 |
|
|
firstOptions.watchOptions || options.watchOptions || firstOptions.watch || options.watch || {};
|
344 |
|
|
if (watchOptions.stdin) {
|
345 |
|
|
process.stdin.on("end", function(_) {
|
346 |
|
|
process.exit(); // eslint-disable-line
|
347 |
|
|
});
|
348 |
|
|
process.stdin.resume();
|
349 |
|
|
}
|
350 |
|
|
compiler.watch(watchOptions, compilerCallback);
|
351 |
|
|
if (outputOptions.infoVerbosity !== "none") console.error("\nwebpack is watching the files…\n");
|
352 |
|
|
} else {
|
353 |
|
|
compiler.run((err, stats) => {
|
354 |
|
|
if (compiler.close) {
|
355 |
|
|
compiler.close(err2 => {
|
356 |
|
|
compilerCallback(err || err2, stats);
|
357 |
|
|
});
|
358 |
|
|
} else {
|
359 |
|
|
compilerCallback(err, stats);
|
360 |
|
|
}
|
361 |
|
|
});
|
362 |
|
|
}
|
363 |
|
|
}
|
364 |
|
|
processOptions(options);
|
365 |
|
|
});
|
366 |
|
|
})();
|