| 
									
										
										
										
											2012-03-12 04:50:55 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Compiler = require("./Compiler"); | 
					
						
							|  |  |  | const MultiCompiler = require("./MultiCompiler"); | 
					
						
							|  |  |  | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin"); | 
					
						
							|  |  |  | const WebpackOptionsApply = require("./WebpackOptionsApply"); | 
					
						
							|  |  |  | const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter"); | 
					
						
							|  |  |  | const validateSchema = require("./validateSchema"); | 
					
						
							|  |  |  | const WebpackOptionsValidationError = require("./WebpackOptionsValidationError"); | 
					
						
							|  |  |  | const webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function webpack(options, callback) { | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 	const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, options); | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 	if(webpackOptionsValidationErrors.length) { | 
					
						
							|  |  |  | 		throw new WebpackOptionsValidationError(webpackOptionsValidationErrors); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 	let compiler; | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 	if(Array.isArray(options)) { | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 		compiler = new MultiCompiler(options.map(options => webpack(options))); | 
					
						
							| 
									
										
										
										
											2015-01-21 19:38:57 +08:00
										 |  |  | 	} else if(typeof options === "object") { | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 		new WebpackOptionsDefaulter().process(options); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 		compiler = new Compiler(); | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		compiler.context = options.context; | 
					
						
							|  |  |  | 		compiler.options = options; | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 		new NodeEnvironmentPlugin().apply(compiler); | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		if(options.plugins && Array.isArray(options.plugins)) { | 
					
						
							|  |  |  | 			compiler.apply.apply(compiler, options.plugins); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-09 06:53:05 +08:00
										 |  |  | 		compiler.applyPlugins("environment"); | 
					
						
							|  |  |  | 		compiler.applyPlugins("after-environment"); | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 		compiler.options = new WebpackOptionsApply().process(options, compiler); | 
					
						
							| 
									
										
										
										
											2015-01-21 19:38:57 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		throw new Error("Invalid argument: options"); | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	if(callback) { | 
					
						
							| 
									
										
										
										
											2015-01-21 19:38:57 +08:00
										 |  |  | 		if(typeof callback !== "function") throw new Error("Invalid argument: callback"); | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 		if(options.watch === true || (Array.isArray(options) && options.some(o => o.watch))) { | 
					
						
							| 
									
										
										
										
											2017-02-02 07:14:18 +08:00
										 |  |  | 			const watchOptions = Array.isArray(options) ? options.map(o => o.watchOptions || {}) : (options.watchOptions || {}); | 
					
						
							| 
									
										
										
										
											2015-05-10 19:50:15 +08:00
										 |  |  | 			return compiler.watch(watchOptions, callback); | 
					
						
							| 
									
										
										
										
											2012-03-10 20:11:23 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-10 19:50:15 +08:00
										 |  |  | 		compiler.run(callback); | 
					
						
							| 
									
										
										
										
											2012-10-20 21:08:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	return compiler; | 
					
						
							| 
									
										
										
										
											2012-05-07 15:01:28 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | exports = module.exports = webpack; | 
					
						
							| 
									
										
										
										
											2012-05-07 15:01:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | webpack.WebpackOptionsDefaulter = WebpackOptionsDefaulter; | 
					
						
							|  |  |  | webpack.WebpackOptionsApply = WebpackOptionsApply; | 
					
						
							|  |  |  | webpack.Compiler = Compiler; | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | webpack.MultiCompiler = MultiCompiler; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin; | 
					
						
							| 
									
										
										
										
											2016-11-03 22:02:14 +08:00
										 |  |  | webpack.validate = validateSchema.bind(this, webpackOptionsSchema); | 
					
						
							|  |  |  | webpack.validateSchema = validateSchema; | 
					
						
							| 
									
										
										
										
											2016-11-03 00:27:02 +08:00
										 |  |  | webpack.WebpackOptionsValidationError = WebpackOptionsValidationError; | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function exportPlugins(exports, path, plugins) { | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 	plugins.forEach(name => { | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 		Object.defineProperty(exports, name, { | 
					
						
							|  |  |  | 			configurable: false, | 
					
						
							|  |  |  | 			enumerable: true, | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 			get() { | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 				return require(`${path}/${name}`); | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exportPlugins(exports, ".", [ | 
					
						
							|  |  |  | 	"DefinePlugin", | 
					
						
							|  |  |  | 	"NormalModuleReplacementPlugin", | 
					
						
							|  |  |  | 	"ContextReplacementPlugin", | 
					
						
							|  |  |  | 	"IgnorePlugin", | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 	"WatchIgnorePlugin", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	"BannerPlugin", | 
					
						
							|  |  |  | 	"PrefetchPlugin", | 
					
						
							| 
									
										
										
										
											2015-02-05 06:21:55 +08:00
										 |  |  | 	"AutomaticPrefetchPlugin", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	"ProvidePlugin", | 
					
						
							|  |  |  | 	"HotModuleReplacementPlugin", | 
					
						
							| 
									
										
										
										
											2014-06-24 06:41:10 +08:00
										 |  |  | 	"SourceMapDevToolPlugin", | 
					
						
							| 
									
										
										
										
											2015-03-02 12:49:43 +08:00
										 |  |  | 	"EvalSourceMapDevToolPlugin", | 
					
						
							| 
									
										
										
										
											2014-06-24 06:41:10 +08:00
										 |  |  | 	"EvalDevToolModulePlugin", | 
					
						
							|  |  |  | 	"CachePlugin", | 
					
						
							| 
									
										
										
										
											2014-08-25 15:50:26 +08:00
										 |  |  | 	"ExtendedAPIPlugin", | 
					
						
							| 
									
										
										
										
											2014-06-24 06:41:10 +08:00
										 |  |  | 	"ExternalsPlugin", | 
					
						
							|  |  |  | 	"JsonpTemplatePlugin", | 
					
						
							|  |  |  | 	"LibraryTemplatePlugin", | 
					
						
							|  |  |  | 	"LoaderTargetPlugin", | 
					
						
							|  |  |  | 	"MemoryOutputFileSystem", | 
					
						
							|  |  |  | 	"ProgressPlugin", | 
					
						
							|  |  |  | 	"SetVarMainTemplatePlugin", | 
					
						
							|  |  |  | 	"UmdMainTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2014-09-03 20:16:17 +08:00
										 |  |  | 	"NoErrorsPlugin", | 
					
						
							| 
									
										
										
										
											2016-12-30 16:52:37 +08:00
										 |  |  | 	"NoEmitOnErrorsPlugin", | 
					
						
							| 
									
										
										
										
											2014-10-09 06:53:05 +08:00
										 |  |  | 	"NewWatchingPlugin", | 
					
						
							| 
									
										
										
										
											2015-05-18 05:29:40 +08:00
										 |  |  | 	"EnvironmentPlugin", | 
					
						
							|  |  |  | 	"DllPlugin", | 
					
						
							| 
									
										
										
										
											2015-06-28 04:47:51 +08:00
										 |  |  | 	"DllReferencePlugin", | 
					
						
							| 
									
										
										
										
											2015-11-17 06:11:15 +08:00
										 |  |  | 	"LoaderOptionsPlugin", | 
					
						
							| 
									
										
										
										
											2015-11-30 03:16:01 +08:00
										 |  |  | 	"NamedModulesPlugin", | 
					
						
							| 
									
										
										
										
											2017-03-26 13:23:44 +08:00
										 |  |  | 	"NamedChunksPlugin", | 
					
						
							| 
									
										
										
										
											2016-08-17 18:52:02 +08:00
										 |  |  | 	"HashedModuleIdsPlugin", | 
					
						
							|  |  |  | 	"ModuleFilenameHelpers" | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | ]); | 
					
						
							|  |  |  | exportPlugins(exports.optimize = {}, "./optimize", [ | 
					
						
							| 
									
										
										
										
											2014-02-05 19:37:37 +08:00
										 |  |  | 	"AggressiveMergingPlugin", | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	"AggressiveSplittingPlugin", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	"CommonsChunkPlugin", | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 	"ChunkModuleIdRangePlugin", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	"DedupePlugin", | 
					
						
							|  |  |  | 	"LimitChunkCountPlugin", | 
					
						
							|  |  |  | 	"MinChunkSizePlugin", | 
					
						
							| 
									
										
										
										
											2014-02-25 15:51:40 +08:00
										 |  |  | 	"OccurrenceOrderPlugin", | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 	"UglifyJsPlugin" | 
					
						
							|  |  |  | ]); | 
					
						
							| 
									
										
										
										
											2016-09-09 03:41:03 +08:00
										 |  |  | exportPlugins(exports.dependencies = {}, "./dependencies", []); |