Added experimental MultiCompiler

webpack(...) takes an array
support an array in webpack.config.js
This commit is contained in:
Tobias Koppers 2014-06-11 22:26:50 +02:00
parent 8bfb752c52
commit 44bb43854f
7 changed files with 567 additions and 410 deletions

View File

@ -26,6 +26,33 @@ module.exports = function(optimist, argv, convertOptions) {
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) {
if(Array.isArray(argv[name])) {
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) {
options.entry[name] = entry;
}, function() {
@ -452,5 +461,7 @@ module.exports = function(optimist, argv, convertOptions) {
});
}
}
return options;
};

View File

@ -145,8 +145,8 @@ var compiler = webpack(options, function(err, stats) {
}
if(outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
} else if(stats.compilation.hash !== lastHash) {
lastHash = stats.compilation.hash;
} else if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n");
}
});

View File

@ -126,7 +126,6 @@ function Compiler() {
this.outputPath = "";
this.outputFileSystem = null;
this.inputFileSystem = null;
this.separateExecutor = null;
this.recordsInputPath = null;
this.recordsOutputPath = null;

132
lib/MultiCompiler.js Normal file
View File

@ -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;

View File

@ -6,6 +6,7 @@ var RequestShortener = require("./RequestShortener");
function Stats(compilation) {
this.compilation = compilation;
this.hash = compilation.hash;
}
module.exports = Stats;
@ -79,7 +80,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
warnings: compilation.warnings.map(formatError)
};
if(showHash) obj.hash = compilation.hash;
if(showHash) obj.hash = this.hash;
if(showTimings && this.startTime && this.endTime) {
obj.time = this.endTime - this.startTime;
}
@ -557,9 +558,13 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
}
if(obj.children) {
obj.children.forEach(function(child) {
if(child.name) {
normal("Child ");
bold(child.name);
normal(":");
} else {
normal("Child");
}
newline();
buf.push(" ");
buf.push(Stats.jsonToString(child, useColors).replace(/\n/g, "\n "));

View File

@ -65,6 +65,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
compiler.outputPath = options.output.path;
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
compiler.name = options.name;
if(typeof options.target === "string") {
switch(options.target) {
case "web":

View File

@ -3,17 +3,25 @@
Author Tobias Koppers @sokra
*/
var Compiler = require("./Compiler");
var MultiCompiler = require("./MultiCompiler");
var NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
var WebpackOptionsApply = require("./WebpackOptionsApply");
var WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
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);
var compiler = new Compiler();
compiler = new Compiler();
compiler.options = options;
compiler.options = new WebpackOptionsApply().process(options, compiler);
new NodeEnvironmentPlugin().apply(compiler);
}
if(callback) {
if(options.watch) {
return compiler.watch(options.watchDelay, callback);
@ -28,6 +36,7 @@ exports = module.exports = webpack;
webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter;
webpack.WebpackOptionsApply = WebpackOptionsApply;
webpack.Compiler = Compiler;
webpack.MultiCompiler = MultiCompiler;
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
function exportPlugins(exports, path, plugins) {