mirror of https://github.com/webpack/webpack.git
Added experimental MultiCompiler
webpack(...) takes an array support an array in webpack.config.js
This commit is contained in:
parent
8bfb752c52
commit
44bb43854f
|
@ -26,6 +26,33 @@ module.exports = function(optimist, argv, convertOptions) {
|
||||||
argv["optimize-occurence-order"] = true;
|
argv["optimize-occurence-order"] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(argv.config) {
|
||||||
|
options = require(path.resolve(argv.config));
|
||||||
|
} else {
|
||||||
|
var configPath = path.resolve("webpack.config.js");
|
||||||
|
if(fs.existsSync(configPath)) {
|
||||||
|
options = require(configPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(typeof options !== "object" || options === null) {
|
||||||
|
console.log("Config did not export a object.");
|
||||||
|
process.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Array.isArray(options)) {
|
||||||
|
options.forEach(processOptions);
|
||||||
|
} else {
|
||||||
|
processOptions(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argv.context) {
|
||||||
|
options.context = path.resolve(argv.context)
|
||||||
|
}
|
||||||
|
if(!options.context) {
|
||||||
|
options.context = process.cwd();
|
||||||
|
}
|
||||||
|
|
||||||
|
function processOptions(options) {
|
||||||
function ifArg(name, fn, init, finalize) {
|
function ifArg(name, fn, init, finalize) {
|
||||||
if(Array.isArray(argv[name])) {
|
if(Array.isArray(argv[name])) {
|
||||||
if(init) {
|
if(init) {
|
||||||
|
@ -120,24 +147,6 @@ module.exports = function(optimist, argv, convertOptions) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(argv.config) {
|
|
||||||
options = require(path.resolve(argv.config));
|
|
||||||
} else {
|
|
||||||
var configPath = path.resolve("webpack.config.js");
|
|
||||||
if(fs.existsSync(configPath)) {
|
|
||||||
options = require(configPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(typeof options !== "object" || options === null) {
|
|
||||||
console.log("Config did not export a object.");
|
|
||||||
process.exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
mapArgToPath("context", "context");
|
|
||||||
if(!options.context) {
|
|
||||||
options.context = process.cwd();
|
|
||||||
}
|
|
||||||
|
|
||||||
ifArgPair("entry", function(name, entry) {
|
ifArgPair("entry", function(name, entry) {
|
||||||
options.entry[name] = entry;
|
options.entry[name] = entry;
|
||||||
}, function() {
|
}, function() {
|
||||||
|
@ -452,5 +461,7 @@ module.exports = function(optimist, argv, convertOptions) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
};
|
};
|
|
@ -145,8 +145,8 @@ var compiler = webpack(options, function(err, stats) {
|
||||||
}
|
}
|
||||||
if(outputOptions.json) {
|
if(outputOptions.json) {
|
||||||
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
|
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
|
||||||
} else if(stats.compilation.hash !== lastHash) {
|
} else if(stats.hash !== lastHash) {
|
||||||
lastHash = stats.compilation.hash;
|
lastHash = stats.hash;
|
||||||
process.stdout.write(stats.toString(outputOptions) + "\n");
|
process.stdout.write(stats.toString(outputOptions) + "\n");
|
||||||
}
|
}
|
||||||
});
|
});
|
|
@ -126,7 +126,6 @@ function Compiler() {
|
||||||
this.outputPath = "";
|
this.outputPath = "";
|
||||||
this.outputFileSystem = null;
|
this.outputFileSystem = null;
|
||||||
this.inputFileSystem = null;
|
this.inputFileSystem = null;
|
||||||
this.separateExecutor = null;
|
|
||||||
|
|
||||||
this.recordsInputPath = null;
|
this.recordsInputPath = null;
|
||||||
this.recordsOutputPath = null;
|
this.recordsOutputPath = null;
|
||||||
|
|
|
@ -0,0 +1,132 @@
|
||||||
|
/*
|
||||||
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||||
|
Author Tobias Koppers @sokra
|
||||||
|
*/
|
||||||
|
var Tapable = require("tapable");
|
||||||
|
var async = require("async");
|
||||||
|
var Stats = require("./Stats");
|
||||||
|
|
||||||
|
function MultiWatching(watchings) {
|
||||||
|
this.watchings = watchings;
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiWatching.prototype.invalidate = function() {
|
||||||
|
this.watchings.forEach(function(watching) {
|
||||||
|
watching.invalidate();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
MultiWatching.prototype.close = function(callback) {
|
||||||
|
async.forEach(this.watchings, function(watching, callback) {
|
||||||
|
watching.close(callback);
|
||||||
|
}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
function MultiCompiler(compilers) {
|
||||||
|
Tapable.call(this);
|
||||||
|
if(!Array.isArray(compilers)) {
|
||||||
|
compilers = Object.keys(compilers).map(function(name) {
|
||||||
|
compilers[name].name = name;
|
||||||
|
return compilers[name];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.compilers = compilers;
|
||||||
|
|
||||||
|
function delegateProperty(name) {
|
||||||
|
Object.defineProperty(this, name, {
|
||||||
|
configurable: false,
|
||||||
|
get: function() {
|
||||||
|
throw new Error("Cannot read " + name + " of a MultiCompiler");
|
||||||
|
},
|
||||||
|
set: function(value) {
|
||||||
|
this.compilers.forEach(function(compiler) {
|
||||||
|
compiler[name] = value;
|
||||||
|
});
|
||||||
|
}.bind(this)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
delegateProperty.call(this, "outputFileSystem");
|
||||||
|
delegateProperty.call(this, "inputFileSystem");
|
||||||
|
|
||||||
|
var doneCompilers = 0;
|
||||||
|
var compilerStats = [];
|
||||||
|
this.compilers.forEach(function(compiler, idx) {
|
||||||
|
var compilerDone = false;
|
||||||
|
compiler.plugin("done", function(stats) {
|
||||||
|
if(!compilerDone) {
|
||||||
|
compilerDone = true;
|
||||||
|
doneCompilers++;
|
||||||
|
}
|
||||||
|
compilerStats[idx] = stats;
|
||||||
|
if(doneCompilers === this.compilers.length) {
|
||||||
|
this.applyPlugins("done", new MultiStats(compilerStats));
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
|
compiler.plugin("invalid", function() {
|
||||||
|
this.applyPlugins("invalid");
|
||||||
|
});
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
module.exports = MultiCompiler;
|
||||||
|
|
||||||
|
MultiCompiler.prototype = Object.create(Tapable.prototype);
|
||||||
|
|
||||||
|
MultiCompiler.prototype.watch = function(watchDelay, handler) {
|
||||||
|
var watchings = this.compilers.map(function(compiler) {
|
||||||
|
return compiler.watch(watchDelay, handler);
|
||||||
|
});
|
||||||
|
return new MultiWatching(watchings);
|
||||||
|
};
|
||||||
|
|
||||||
|
MultiCompiler.prototype.run = function(callback) {
|
||||||
|
async.map(this.compilers, function(compiler, callback) {
|
||||||
|
compiler.run(callback);
|
||||||
|
}, function(err, stats) {
|
||||||
|
if(err) return callback(err);
|
||||||
|
callback(null, new MultiStats(stats));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function MultiStats(stats) {
|
||||||
|
this.stats = stats;
|
||||||
|
this.hash = stats.map(function(stat) {
|
||||||
|
return stat.hash;
|
||||||
|
}).join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiStats.prototype.hasErrors = function() {
|
||||||
|
return this.stats.map(function(stat) {
|
||||||
|
return stat.hasErrors();
|
||||||
|
}).reduce(function(a, b) { return a || b; }, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
MultiStats.prototype.hasWarnings = function() {
|
||||||
|
return this.stats.map(function(stat) {
|
||||||
|
return stat.hasWarnings();
|
||||||
|
}).reduce(function(a, b) { return a || b; }, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
MultiStats.prototype.toJson = function(options, forToString) {
|
||||||
|
var jsons = this.stats.map(function(stat) {
|
||||||
|
var obj = stat.toJson(options, forToString);
|
||||||
|
obj.name = stat.compilation && stat.compilation.name;
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
version: require("../package.json").version,
|
||||||
|
hash: this.hash,
|
||||||
|
errors: jsons.reduce(function(arr, j) {
|
||||||
|
return arr.concat(j.errors.map(function(msg) {
|
||||||
|
return "(" + j.name + ") " + msg
|
||||||
|
}));
|
||||||
|
}, []),
|
||||||
|
warnings: jsons.reduce(function(arr, j) {
|
||||||
|
return arr.concat(j.warnings.map(function(msg) {
|
||||||
|
return "(" + j.name + ") " + msg
|
||||||
|
}));
|
||||||
|
}, []),
|
||||||
|
children: jsons
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
MultiStats.prototype.toString = Stats.prototype.toString;
|
|
@ -6,6 +6,7 @@ var RequestShortener = require("./RequestShortener");
|
||||||
|
|
||||||
function Stats(compilation) {
|
function Stats(compilation) {
|
||||||
this.compilation = compilation;
|
this.compilation = compilation;
|
||||||
|
this.hash = compilation.hash;
|
||||||
}
|
}
|
||||||
module.exports = Stats;
|
module.exports = Stats;
|
||||||
|
|
||||||
|
@ -79,7 +80,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
|
||||||
warnings: compilation.warnings.map(formatError)
|
warnings: compilation.warnings.map(formatError)
|
||||||
};
|
};
|
||||||
|
|
||||||
if(showHash) obj.hash = compilation.hash;
|
if(showHash) obj.hash = this.hash;
|
||||||
if(showTimings && this.startTime && this.endTime) {
|
if(showTimings && this.startTime && this.endTime) {
|
||||||
obj.time = this.endTime - this.startTime;
|
obj.time = this.endTime - this.startTime;
|
||||||
}
|
}
|
||||||
|
@ -557,9 +558,13 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
|
||||||
}
|
}
|
||||||
if(obj.children) {
|
if(obj.children) {
|
||||||
obj.children.forEach(function(child) {
|
obj.children.forEach(function(child) {
|
||||||
|
if(child.name) {
|
||||||
normal("Child ");
|
normal("Child ");
|
||||||
bold(child.name);
|
bold(child.name);
|
||||||
normal(":");
|
normal(":");
|
||||||
|
} else {
|
||||||
|
normal("Child");
|
||||||
|
}
|
||||||
newline();
|
newline();
|
||||||
buf.push(" ");
|
buf.push(" ");
|
||||||
buf.push(Stats.jsonToString(child, useColors).replace(/\n/g, "\n "));
|
buf.push(Stats.jsonToString(child, useColors).replace(/\n/g, "\n "));
|
||||||
|
|
|
@ -65,6 +65,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
|
||||||
compiler.outputPath = options.output.path;
|
compiler.outputPath = options.output.path;
|
||||||
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
|
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
|
||||||
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
|
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
|
||||||
|
compiler.name = options.name;
|
||||||
if(typeof options.target === "string") {
|
if(typeof options.target === "string") {
|
||||||
switch(options.target) {
|
switch(options.target) {
|
||||||
case "web":
|
case "web":
|
||||||
|
|
|
@ -3,17 +3,25 @@
|
||||||
Author Tobias Koppers @sokra
|
Author Tobias Koppers @sokra
|
||||||
*/
|
*/
|
||||||
var Compiler = require("./Compiler");
|
var Compiler = require("./Compiler");
|
||||||
|
var MultiCompiler = require("./MultiCompiler");
|
||||||
var NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
var NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
||||||
var WebpackOptionsApply = require("./WebpackOptionsApply");
|
var WebpackOptionsApply = require("./WebpackOptionsApply");
|
||||||
var WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
|
var WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
|
||||||
|
|
||||||
function webpack(options, callback) {
|
function webpack(options, callback) {
|
||||||
|
var compiler;
|
||||||
|
if(Array.isArray(options)) {
|
||||||
|
compiler = new MultiCompiler(options.map(function(options) {
|
||||||
|
return webpack(options);
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
new WebpackOptionsDefaulter().process(options);
|
new WebpackOptionsDefaulter().process(options);
|
||||||
|
|
||||||
var compiler = new Compiler();
|
compiler = new Compiler();
|
||||||
compiler.options = options;
|
compiler.options = options;
|
||||||
compiler.options = new WebpackOptionsApply().process(options, compiler);
|
compiler.options = new WebpackOptionsApply().process(options, compiler);
|
||||||
new NodeEnvironmentPlugin().apply(compiler);
|
new NodeEnvironmentPlugin().apply(compiler);
|
||||||
|
}
|
||||||
if(callback) {
|
if(callback) {
|
||||||
if(options.watch) {
|
if(options.watch) {
|
||||||
return compiler.watch(options.watchDelay, callback);
|
return compiler.watch(options.watchDelay, callback);
|
||||||
|
@ -28,6 +36,7 @@ exports = module.exports = webpack;
|
||||||
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
|
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
|
||||||
webpack.WebpackOptionsApply = WebpackOptionsApply;
|
webpack.WebpackOptionsApply = WebpackOptionsApply;
|
||||||
webpack.Compiler = Compiler;
|
webpack.Compiler = Compiler;
|
||||||
|
webpack.MultiCompiler = MultiCompiler;
|
||||||
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
|
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
|
||||||
|
|
||||||
function exportPlugins(exports, path, plugins) {
|
function exportPlugins(exports, path, plugins) {
|
||||||
|
|
Loading…
Reference in New Issue