2012-03-10 20:11:23 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2012-03-12 04:50:55 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2012-03-10 20:11:23 +08:00
|
|
|
var path = require("path");
|
2012-08-06 03:52:00 +08:00
|
|
|
// Local version replace global one
|
|
|
|
try {
|
|
|
|
var localWebpack = require.resolve(path.join(process.cwd(), "node_modules", "webpack", "bin", "webpack.js"));
|
|
|
|
if(__filename != localWebpack) {
|
|
|
|
return require(localWebpack);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
2012-03-10 20:11:23 +08:00
|
|
|
var fs = require("fs");
|
2012-03-15 07:05:29 +08:00
|
|
|
var util = require("util");
|
2013-01-31 01:49:25 +08:00
|
|
|
var optimist = require("optimist")
|
2012-08-06 03:52:00 +08:00
|
|
|
.usage("webpack " + require("../package.json").version + "\n" +
|
2014-01-14 16:57:26 +08:00
|
|
|
"Usage: http://webpack.github.io/docs/webpack-detailed-usage.html")
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
require("./config-optimist")(optimist);
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
optimist
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.boolean("json").alias("json", "j").describe("json")
|
|
|
|
|
|
|
|
.boolean("colors").alias("colors", "c").describe("colors")
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.string("sort-modules-by").describe("sort-modules-by")
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.string("sort-chunks-by").describe("sort-chunks-by")
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.string("sort-assets-by").describe("sort-assets-by")
|
2012-03-12 04:50:55 +08:00
|
|
|
|
2014-02-14 00:31:52 +08:00
|
|
|
.boolean("hide-modules").describe("hide-modules")
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.boolean("display-chunks").describe("display-chunks")
|
2012-03-14 23:33:46 +08:00
|
|
|
|
2014-01-21 23:24:17 +08:00
|
|
|
.boolean("display-error-details").describe("display-error-details")
|
|
|
|
|
2014-01-23 22:31:40 +08:00
|
|
|
.boolean("display-origins").describe("display-origins")
|
|
|
|
|
2014-05-17 06:00:11 +08:00
|
|
|
.boolean("display-cached").describe("display-cached")
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
.boolean("display-reasons").alias("display-reasons", "verbose").alias("display-reasons", "v").describe("display-reasons");
|
2014-01-21 23:24:17 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
var argv = optimist.argv;
|
2012-06-30 02:54:24 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var options = require("./convert-argv")(optimist, argv);
|
2012-04-03 22:26:08 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function ifArg(name, fn, init) {
|
|
|
|
if(Array.isArray(argv[name])) {
|
|
|
|
if(init) init();
|
|
|
|
argv[name].forEach(fn);
|
|
|
|
} else if(typeof argv[name] != "undefined") {
|
|
|
|
if(init) init();
|
|
|
|
fn(argv[name], -1);
|
|
|
|
}
|
2012-03-10 20:11:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-03 22:26:08 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
var outputOptions = {
|
|
|
|
cached: false,
|
|
|
|
context: options.context
|
|
|
|
};
|
2012-06-30 02:54:24 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
ifArg("json", function(bool) {
|
|
|
|
outputOptions.json = bool;
|
|
|
|
});
|
2012-05-02 03:33:59 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
ifArg("colors", function(bool) {
|
|
|
|
outputOptions.colors = bool;
|
|
|
|
});
|
2012-09-25 22:45:53 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
ifArg("sort-modules-by", function(value) {
|
|
|
|
outputOptions.modulesSort = value;
|
|
|
|
});
|
2012-09-25 22:45:53 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
ifArg("sort-chunks-by", function(value) {
|
|
|
|
outputOptions.chunksSort = value;
|
|
|
|
});
|
2012-05-12 23:30:41 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
ifArg("sort-assets-by", function(value) {
|
|
|
|
outputOptions.assetsSort = value;
|
|
|
|
});
|
2012-03-10 20:11:23 +08:00
|
|
|
|
2013-02-19 18:11:43 +08:00
|
|
|
if(!outputOptions.json) {
|
|
|
|
ifArg("display-chunks", function(bool) {
|
2014-02-14 00:31:52 +08:00
|
|
|
outputOptions.modules = !bool;
|
2013-02-19 18:11:43 +08:00
|
|
|
outputOptions.chunks = bool;
|
|
|
|
});
|
|
|
|
|
|
|
|
ifArg("display-reasons", function(bool) {
|
|
|
|
outputOptions.reasons = bool;
|
|
|
|
});
|
2014-01-21 23:24:17 +08:00
|
|
|
|
|
|
|
ifArg("display-error-details", function(bool) {
|
|
|
|
outputOptions.errorDetails = bool;
|
|
|
|
});
|
2014-01-23 22:31:40 +08:00
|
|
|
|
|
|
|
ifArg("display-origins", function(bool) {
|
|
|
|
outputOptions.chunkOrigins = bool;
|
|
|
|
});
|
2014-05-17 06:00:11 +08:00
|
|
|
|
|
|
|
ifArg("display-cached", function(bool) {
|
|
|
|
if(bool)
|
|
|
|
outputOptions.cached = true;
|
|
|
|
});
|
2013-02-19 18:11:43 +08:00
|
|
|
} else {
|
|
|
|
outputOptions.chunks = true;
|
|
|
|
outputOptions.modules = true;
|
|
|
|
outputOptions.chunkModules = true;
|
|
|
|
outputOptions.reasons = true;
|
|
|
|
}
|
2012-03-16 20:59:19 +08:00
|
|
|
|
2014-02-14 17:07:42 +08:00
|
|
|
ifArg("hide-modules", function(bool) {
|
|
|
|
if(bool) {
|
|
|
|
outputOptions.modules = false;
|
|
|
|
outputOptions.chunkModules = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-10 20:11:23 +08:00
|
|
|
var webpack = require("../lib/webpack.js");
|
2012-09-19 00:40:48 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
Error.stackTrackLimit = 30;
|
2014-06-03 06:15:54 +08:00
|
|
|
var lastHash = null;
|
2014-02-26 16:36:49 +08:00
|
|
|
var compiler = webpack(options, function(err, stats) {
|
|
|
|
if(!options.watch) {
|
|
|
|
// Do not keep cache anymore
|
|
|
|
var ifs = compiler.inputFileSystem;
|
|
|
|
if(ifs && ifs.purge) ifs.purge();
|
|
|
|
}
|
2012-09-19 00:40:48 +08:00
|
|
|
if(err) {
|
2014-06-03 06:15:54 +08:00
|
|
|
lastHash = null;
|
2013-01-31 01:49:25 +08:00
|
|
|
console.error(err.stack || err);
|
2014-02-26 16:37:10 +08:00
|
|
|
if(err.details) console.error(err.details);
|
2014-02-26 16:36:28 +08:00
|
|
|
if(!options.watch) {
|
|
|
|
process.on("exit", function() {
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
}
|
2012-09-19 00:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-06-03 06:15:54 +08:00
|
|
|
if(outputOptions.json) {
|
2013-12-04 03:27:06 +08:00
|
|
|
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
|
2014-06-12 04:26:50 +08:00
|
|
|
} else if(stats.hash !== lastHash) {
|
|
|
|
lastHash = stats.hash;
|
2013-12-04 03:27:06 +08:00
|
|
|
process.stdout.write(stats.toString(outputOptions) + "\n");
|
2012-09-19 00:40:48 +08:00
|
|
|
}
|
|
|
|
});
|