| 
									
										
										
										
											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" + | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		"Usage: https://github.com/webpack/docs/wiki/webpack-detailed-usage") | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	.boolean("display-chunks").describe("display-chunks") | 
					
						
							| 
									
										
										
										
											2012-03-14 23:33:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	.boolean("display-reasons").alias("display-reasons", "verbose").alias("display-reasons", "v").describe("display-reasons"); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 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) { | 
					
						
							|  |  |  | 		outputOptions.modules = !bool ; | 
					
						
							|  |  |  | 		outputOptions.chunks = bool; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ifArg("display-reasons", function(bool) { | 
					
						
							|  |  |  | 		outputOptions.reasons = bool; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  | 	outputOptions.chunks = true; | 
					
						
							|  |  |  | 	outputOptions.modules = true; | 
					
						
							|  |  |  | 	outputOptions.chunkModules = true; | 
					
						
							|  |  |  | 	outputOptions.reasons = true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-03-16 20:59:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | webpack(options, function(err, stats) { | 
					
						
							| 
									
										
										
										
											2012-09-19 00:40:48 +08:00
										 |  |  | 	if(err) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		console.error(err.stack || err); | 
					
						
							| 
									
										
										
										
											2012-09-19 00:40:48 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	if(outputOptions.json) | 
					
						
							|  |  |  | 		console.log(JSON.stringify(stats.toJson(outputOptions), null, 2)); | 
					
						
							| 
									
										
										
										
											2012-09-19 00:40:48 +08:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		console.log(stats.toString(outputOptions)); | 
					
						
							| 
									
										
										
										
											2012-09-19 00:40:48 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-10-15 01:28:35 +08:00
										 |  |  | 	if(!options.watch) { | 
					
						
							|  |  |  | 		// Do not keep cache anymore
 | 
					
						
							|  |  |  | 		process.exit(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-09-19 00:40:48 +08:00
										 |  |  | }); |