Merge branch 'webpack-1'

Conflicts:
	bin/config-optimist.js
	bin/convert-argv.js
	package.json
	test/browsertest/library2config.coffee
This commit is contained in:
Tobias Koppers 2015-11-14 16:32:54 +01:00
commit ff16bf4561
5 changed files with 260 additions and 228 deletions

View File

@ -1,7 +1,7 @@
module.exports = function(optimist) { module.exports = function(optimist) {
optimist optimist
.boolean("help").alias("help", "h").alias("help", "?").describe("help") .boolean("help").alias("help", "h").alias("help", "?").describe("help")
.string("config").alias("config", "c").describe("config") .string("config").describe("config")
.string("env").describe("env", "Enviroment passed to the config, when it is a function") .string("env").describe("env", "Enviroment passed to the config, when it is a function")
.string("context").describe("context") .string("context").describe("context")
.string("entry").describe("entry") .string("entry").describe("entry")
@ -27,7 +27,7 @@ module.exports = function(optimist) {
.boolean("watch-stdin").alias("watch-stdin", "stdin").describe("watch which closes when stdin ends") .boolean("watch-stdin").alias("watch-stdin", "stdin").describe("watch which closes when stdin ends")
.describe("watch-aggregate-timeout") .describe("watch-aggregate-timeout")
.describe("watch-poll") .describe("watch-poll")
.boolean("hot").alias("hot", "h").describe("hot") .boolean("hot").describe("hot")
.boolean("debug").describe("debug") .boolean("debug").describe("debug")
.string("devtool").describe("devtool") .string("devtool").describe("devtool")
.boolean("progress").describe("progress") .boolean("progress").describe("progress")

View File

@ -11,7 +11,7 @@ module.exports = function(optimist, argv, convertOptions) {
// Help // Help
if(argv.help) { if(argv.help) {
optimist.showHelp(); optimist.showHelp();
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
// Shortcuts // Shortcuts
@ -85,62 +85,78 @@ module.exports = function(optimist, argv, convertOptions) {
options = options(argv.env, argv); options = options(argv.env, argv);
} }
if(typeof options !== "object" || options === null) { return processConfiguredOptions(options);
console.log("Config did not export an object or a function returning an object.");
process.exit(-1);
}
if(Array.isArray(options)) { function processConfiguredOptions(options) {
options.forEach(processOptions); if(typeof options !== "object" || options === null) {
} else { console.log("Config did not export an object or a function returning an object.");
processOptions(options); process.exit(-1); // eslint-disable-line
}
if(argv.context) {
options.context = path.resolve(argv.context);
}
if(!options.context) {
options.context = process.cwd();
}
if(argv.watch) {
// TODO remove this in next major version
if(options.watch && typeof options.watch === "object") {
console.warn("options.watch is deprecated: Use 'options.watchOptions' instead");
options.watchOptions = options.watch;
} }
// TODO remove this in next major version
if(options.watchDelay) { // process Promise
console.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead"); if(typeof options.then === "function") {
return options.then(processConfiguredOptions);
}
// process ES6 default
if(typeof options === "object" && typeof options["default"] === "object") {
return processConfiguredOptions(options["default"]);
}
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();
}
if(argv.watch) {
// TODO remove this in next major version
if(options.watch && typeof options.watch === "object") {
console.warn("options.watch is deprecated: Use 'options.watchOptions' instead");
options.watchOptions = options.watch;
}
// TODO remove this in next major version
if(options.watchDelay) {
console.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = options.watchDelay;
}
options.watch = true;
}
if(argv["watch-delay"]) {
console.warn("--watch-delay is deprecated: Use '--watch-aggregate-timeout' instead");
options.watchOptions = options.watchOptions || {}; options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = options.watchDelay; options.watchOptions.aggregateTimeout = +argv["watch-delay"];
} }
options.watch = true;
}
if(argv["watch-delay"]) { if(argv["watch-aggregate-timeout"]) {
console.warn("--watch-delay is deprecated: Use '--watch-aggregate-timeout' instead"); options.watchOptions = options.watchOptions || {};
options.watchOptions = options.watchOptions || {}; options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"];
options.watchOptions.aggregateTimeout = +argv["watch-delay"]; }
}
if(argv["watch-aggregate-timeout"]) { if(argv["watch-poll"]) {
options.watchOptions = options.watchOptions || {}; options.watchOptions = options.watchOptions || {};
options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"]; if(typeof argv["watch-poll"] !== "boolean")
} options.watchOptions.poll = +argv["watch-poll"];
else
options.watchOptions.poll = true;
}
if(argv["watch-poll"]) { if(argv["watch-stdin"]) {
options.watchOptions = options.watchOptions || {}; options.watchOptions = options.watchOptions || {};
if(typeof argv["watch-poll"] !== "boolean") options.watchOptions.stdin = true;
options.watchOptions.poll = +argv["watch-poll"]; options.watch = true;
else }
options.watchOptions.poll = true;
}
if(argv["watch-stdin"]) { return options;
options.watchOptions = options.watchOptions || {};
options.watchOptions.stdin = true;
options.watch = true;
} }
function processOptions(options) { function processOptions(options) {
@ -214,7 +230,7 @@ module.exports = function(optimist, argv, convertOptions) {
} }
} catch(e) { } catch(e) {
console.log("Invalid plugin arguments " + name + " (" + e + ")."); console.log("Invalid plugin arguments " + name + " (" + e + ").");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
var path; var path;
@ -222,7 +238,7 @@ module.exports = function(optimist, argv, convertOptions) {
path = resolve.sync(process.cwd(), name); path = resolve.sync(process.cwd(), name);
} catch(e) { } catch(e) {
console.log("Cannot resolve plugin " + name + "."); console.log("Cannot resolve plugin " + name + ".");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
var Plugin; var Plugin;
try { try {
@ -514,7 +530,7 @@ module.exports = function(optimist, argv, convertOptions) {
} else { } else {
optimist.showHelp(); optimist.showHelp();
console.error("Output filename not configured."); console.error("Output filename not configured.");
process.exit(-1); process.exit(-1); // eslint-disable-line
} }
} }
@ -551,8 +567,5 @@ module.exports = function(optimist, argv, convertOptions) {
} }
}); });
} }
} }
return options;
}; };

View File

@ -48,136 +48,150 @@ function ifArg(name, fn, init) {
} }
} }
var firstOptions = Array.isArray(options) ? options[0] : options; function processOptions(options) {
// process Promise
var outputOptions = Object.create(options.stats || firstOptions.stats || {}); if(typeof options.then === "function") {
if(typeof outputOptions.context === "undefined") options.then(processOptions).catch(function(err) {
outputOptions.context = firstOptions.context; console.error(err.stack || err);
process.exit(); // eslint-disable-line
ifArg("json", function(bool) { });
if(bool)
outputOptions.json = bool;
});
if(typeof outputOptions.colors === "undefined")
outputOptions.colors = require("supports-color");
ifArg("sort-modules-by", function(value) {
outputOptions.modulesSort = value;
});
ifArg("sort-chunks-by", function(value) {
outputOptions.chunksSort = value;
});
ifArg("sort-assets-by", function(value) {
outputOptions.assetsSort = value;
});
ifArg("display-exclude", function(value) {
outputOptions.exclude = value;
});
if(!outputOptions.json) {
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = false;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = false;
ifArg("display-chunks", function(bool) {
outputOptions.modules = !bool;
outputOptions.chunks = bool;
});
ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
});
ifArg("display-error-details", function(bool) {
outputOptions.errorDetails = bool;
});
ifArg("display-origins", function(bool) {
outputOptions.chunkOrigins = bool;
});
ifArg("display-cached", function(bool) {
if(bool)
outputOptions.cached = true;
});
ifArg("display-cached-assets", function(bool) {
if(bool)
outputOptions.cachedAssets = true;
});
if(!outputOptions.exclude && !argv["display-modules"])
outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"];
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
outputOptions.chunkModules = true;
if(typeof outputOptions.reasons === "undefined")
outputOptions.reasons = true;
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true;
}
ifArg("hide-modules", function(bool) {
if(bool) {
outputOptions.modules = false;
outputOptions.chunkModules = false;
}
});
var webpack = require("../lib/webpack.js");
Error.stackTraceLimit = 30;
var lastHash = null;
var compiler = webpack(options);
function compilerCallback(err, stats) {
if(!options.watch) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
}
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
if(!options.watch) {
process.on("exit", function() {
process.exit(1);
});
}
return; return;
} }
if(outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n"); var firstOptions = Array.isArray(options) ? options[0] : options;
} else if(stats.hash !== lastHash) {
lastHash = stats.hash; var outputOptions = Object.create(options.stats || firstOptions.stats || {});
process.stdout.write(stats.toString(outputOptions) + "\n"); if(typeof outputOptions.context === "undefined")
} outputOptions.context = firstOptions.context;
if(!options.doWatch && stats.hasErrors()) {
process.on("exit", function() { ifArg("json", function(bool) {
process.exit(2); if(bool)
outputOptions.json = bool;
});
if(typeof outputOptions.colors === "undefined")
outputOptions.colors = require("supports-color");
ifArg("sort-modules-by", function(value) {
outputOptions.modulesSort = value;
});
ifArg("sort-chunks-by", function(value) {
outputOptions.chunksSort = value;
});
ifArg("sort-assets-by", function(value) {
outputOptions.assetsSort = value;
});
ifArg("display-exclude", function(value) {
outputOptions.exclude = value;
});
if(!outputOptions.json) {
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = false;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = false;
ifArg("display-chunks", function(bool) {
outputOptions.modules = !bool;
outputOptions.chunks = bool;
}); });
ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
});
ifArg("display-error-details", function(bool) {
outputOptions.errorDetails = bool;
});
ifArg("display-origins", function(bool) {
outputOptions.chunkOrigins = bool;
});
ifArg("display-cached", function(bool) {
if(bool)
outputOptions.cached = true;
});
ifArg("display-cached-assets", function(bool) {
if(bool)
outputOptions.cachedAssets = true;
});
if(!outputOptions.exclude && !argv["display-modules"])
outputOptions.exclude = ["node_modules", "bower_components", "jam", "components"];
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
outputOptions.chunkModules = true;
if(typeof outputOptions.reasons === "undefined")
outputOptions.reasons = true;
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true;
} }
ifArg("hide-modules", function(bool) {
if(bool) {
outputOptions.modules = false;
outputOptions.chunkModules = false;
}
});
var webpack = require("../lib/webpack.js");
Error.stackTraceLimit = 30;
var lastHash = null;
var compiler = webpack(options);
function compilerCallback(err, stats) {
if(!options.watch) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
}
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
if(!options.watch) {
process.on("exit", function() {
process.exit(1); // eslint-disable-line
});
}
return;
}
if(outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
} else if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n");
}
if(!options.doWatch && stats.hasErrors()) {
process.on("exit", function() {
process.exit(2); // eslint-disable-line
});
}
}
if(options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0];
var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {};
if(watchOptions.stdin) {
process.stdin.on('end', function() {
process.exit(0); // eslint-disable-line
});
process.stdin.resume();
}
compiler.watch(watchOptions, compilerCallback);
} else
compiler.run(compilerCallback);
} }
if(options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0]; processOptions(options);
var watchOptions = primaryOptions.watchOptions || primaryOptions.watch || {};
if(watchOptions.stdin) {
process.stdin.on('end', function() {
process.exit(0)
});
process.stdin.resume();
}
compiler.watch(watchOptions, compilerCallback);
} else
compiler.run(compilerCallback);

View File

@ -85,6 +85,6 @@
"beautify": "node ./scripts/beautify-rewrite", "beautify": "node ./scripts/beautify-rewrite",
"precover": "npm run lint && npm run beautify-lint", "precover": "npm run lint && npm run beautify-lint",
"cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha", "cover": "istanbul cover -x *.runtime.js node_modules/mocha/bin/_mocha",
"publish-patch": "mocha && npm version patch && git push && git push --tags && npm publish" "publish-patch": "npm run lint && npm run beautify-lint && mocha && npm version patch && git push && git push --tags && npm publish"
} }
} }

View File

@ -1,43 +1,48 @@
webpack = require("../../"); webpack = require("../../");
module.exports = exports.default = new Promise (resolve, reject) ->
entry: resolveIt = ->
common: "library2/lib/common" resolve
output: entry:
hashDigestLength: 5 common: "library2/lib/common"
module: output:
postLoaders: [ hashDigestLength: 5
{ test: /extra2?\.js/, loader: "raw!extra!val?cacheable" } module:
] postLoaders: [
amd: { test: /extra2?\.js/, loader: "raw!extra!val?cacheable" }
fromOptions: true ]
resolve: amd:
# cannot resolve should outside the outermost node_modules fromOptions: true
# so it is injected here resolve:
alias: # cannot resolve should outside the outermost node_modules
should: require.resolve "should" # so it is injected here
plugins: [ alias:
new webpack.optimize.LimitChunkCountPlugin should: require.resolve "should"
maxChunks: 3 plugins: [
new webpack.optimize.CommonsChunkPlugin "common", "library2.commons.js" new webpack.optimize.LimitChunkCountPlugin
new webpack.DefinePlugin maxChunks: 2
"typeof CONST_TYPEOF": JSON.stringify("typeof"), new webpack.optimize.CommonsChunkPlugin
CONST_UNDEFINED: undefined, name: "common"
CONST_NULL: null, filename: "library2.commons.js"
CONST_TRUE: true, new webpack.DefinePlugin
CONST_FALSE: false, "typeof CONST_TYPEOF": JSON.stringify("typeof"),
CONST_FUNCTION: -> return "ok"; CONST_UNDEFINED: undefined,
CONST_NUMBER: 123, CONST_NULL: "null",
CONST_NUMBER_EXPR: "(1*100+23)", CONST_TRUE: true,
CONST_OBJECT: { CONST_FALSE: false,
A: 1, CONST_FUNCTION: -> return "ok";
B: JSON.stringify("B"), CONST_NUMBER: 123,
C: -> return "C"; CONST_NUMBER_EXPR: "(1*100+23)",
} CONST_OBJECT: {
new webpack.ProvidePlugin A: 1,
s3: "submodule3" B: JSON.stringify("B"),
-> C: -> return "C";
this.plugin "normal-module-factory", (nmf) -> }
nmf.plugin "after-resolve", (data, callback) -> new webpack.ProvidePlugin
data.resource = data.resource.replace /extra\.js/, "extra2.js"; s3: "submodule3"
callback null, data; ->
] this.plugin "normal-module-factory", (nmf) ->
nmf.plugin "after-resolve", (data, callback) ->
data.resource = data.resource.replace /extra\.js/, "extra2.js";
callback null, data;
]
setTimeout resolveIt, 300