Always use a MultiCompiler

This commit is contained in:
Florent Cailhol 2018-12-12 07:59:24 +01:00
parent e06b4853d6
commit 5f5d80fd20
2 changed files with 17 additions and 22 deletions

View File

@ -34,6 +34,10 @@ class MultiStats {
}
toJson(options, forToString) {
if (this.stats.length === 1) {
return this.stats[0].toJson(options, forToString);
}
if (typeof options === "boolean" || typeof options === "string") {
options = Stats.presetToOptions(options);
} else if (!options) {
@ -81,6 +85,10 @@ class MultiStats {
}
toString(options) {
if (this.stats.length === 1) {
return this.stats[0].toString(options);
}
if (typeof options === "boolean" || typeof options === "string") {
options = Stats.presetToOptions(options);
} else if (!options) {

View File

@ -70,38 +70,25 @@ const createCompiler = options => {
/**
* @param {WebpackOptions | WebpackOptions[]} options options object
* @param {Callback<Stats> | Callback<MultiStats>=} callback callback
* @returns {Compiler | MultiCompiler} the compiler object
* @param {Callback<MultiStats>=} callback callback
* @returns {MultiCompiler} the compiler object
*/
const webpack = (options, callback) => {
const validationErrors = validateSchema(webpackOptionsSchema, options);
if (validationErrors.length) {
throw new WebpackOptionsValidationError(validationErrors);
}
/** @type {TODO} */
let compiler;
let watch = false;
let watchOptions;
if (Array.isArray(options)) {
compiler = createMultiCompiler(options);
watch = options.some(options => options.watch);
watchOptions = options.map(options => options.watchOptions || {});
} else {
compiler = createCompiler(options);
watch = options.watch;
watchOptions = options.watchOptions || {};
}
const compilerOptions = Array.isArray(options) ? options : [options];
const compiler = createMultiCompiler(compilerOptions);
if (callback) {
const watch = compilerOptions.some(options => options.watch);
if (watch) {
const watchOptions = compilerOptions.map(
options => options.watchOptions || {}
);
compiler.watch(watchOptions, callback);
} else {
compiler.run((err, stats) => {
compiler.close(err2 => {
// @ts-ignore
// TODO fix the typings
callback(err || err2, stats);
});
});
compiler.run(callback);
}
}
return compiler;