| 
									
										
										
										
											2012-03-12 04:50:55 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 03:05:04 +08:00
										 |  |  | const util = require("util"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const version = require("../package.json").version; | 
					
						
							|  |  |  | const webpackOptionsSchema = require("../schemas/WebpackOptions.json"); | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | const Compiler = require("./Compiler"); | 
					
						
							|  |  |  | const MultiCompiler = require("./MultiCompiler"); | 
					
						
							|  |  |  | const WebpackOptionsApply = require("./WebpackOptionsApply"); | 
					
						
							|  |  |  | const WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter"); | 
					
						
							|  |  |  | const WebpackOptionsValidationError = require("./WebpackOptionsValidationError"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin"); | 
					
						
							|  |  |  | const validateSchema = require("./validateSchema"); | 
					
						
							| 
									
										
										
										
											2018-02-27 01:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const webpack = (options, callback) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	const webpackOptionsValidationErrors = validateSchema( | 
					
						
							|  |  |  | 		webpackOptionsSchema, | 
					
						
							|  |  |  | 		options | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	if (webpackOptionsValidationErrors.length) { | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 		throw new WebpackOptionsValidationError(webpackOptionsValidationErrors); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 	let compiler; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (Array.isArray(options)) { | 
					
						
							| 
									
										
										
										
											2017-01-02 03:19:00 +08:00
										 |  |  | 		compiler = new MultiCompiler(options.map(options => webpack(options))); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	} else if (typeof options === "object") { | 
					
						
							| 
									
										
										
										
											2017-09-14 15:22:29 +08:00
										 |  |  | 		options = new WebpackOptionsDefaulter().process(options); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 17:44:22 +08:00
										 |  |  | 		compiler = new Compiler(options.context); | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		compiler.options = options; | 
					
						
							| 
									
										
										
										
											2014-06-12 04:26:50 +08:00
										 |  |  | 		new NodeEnvironmentPlugin().apply(compiler); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (options.plugins && Array.isArray(options.plugins)) { | 
					
						
							|  |  |  | 			for (const plugin of options.plugins) { | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				plugin.apply(compiler); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-27 22:27:37 +08:00
										 |  |  | 		compiler.hooks.environment.call(); | 
					
						
							|  |  |  | 		compiler.hooks.afterEnvironment.call(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (callback) { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (typeof callback !== "function") { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			throw new Error("Invalid argument: callback"); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			options.watch === true || | 
					
						
							|  |  |  | 			(Array.isArray(options) && options.some(o => o.watch)) | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			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; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | exports = module.exports = webpack; | 
					
						
							| 
									
										
										
										
											2018-02-26 16:40:38 +08:00
										 |  |  | exports.version = version; | 
					
						
							| 
									
										
										
										
											2018-02-27 01:22:25 +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; | 
					
						
							| 
									
										
										
										
											2018-07-05 13:07:46 +08:00
										 |  |  | // @ts-ignore Global @this directive is not supported
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const exportPlugins = (obj, mappings) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	for (const name of Object.keys(mappings)) { | 
					
						
							| 
									
										
										
										
											2017-07-21 15:25:07 +08:00
										 |  |  | 		Object.defineProperty(obj, name, { | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 			configurable: false, | 
					
						
							|  |  |  | 			enumerable: true, | 
					
						
							| 
									
										
										
										
											2017-06-23 20:55:22 +08:00
										 |  |  | 			get: mappings[name] | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-12-18 06:21:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 20:55:22 +08:00
										 |  |  | exportPlugins(exports, { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	AutomaticPrefetchPlugin: () => require("./AutomaticPrefetchPlugin"), | 
					
						
							|  |  |  | 	BannerPlugin: () => require("./BannerPlugin"), | 
					
						
							|  |  |  | 	CachePlugin: () => require("./CachePlugin"), | 
					
						
							|  |  |  | 	ContextExclusionPlugin: () => require("./ContextExclusionPlugin"), | 
					
						
							|  |  |  | 	ContextReplacementPlugin: () => require("./ContextReplacementPlugin"), | 
					
						
							|  |  |  | 	DefinePlugin: () => require("./DefinePlugin"), | 
					
						
							| 
									
										
										
										
											2018-03-03 22:41:30 +08:00
										 |  |  | 	Dependency: () => require("./Dependency"), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	DllPlugin: () => require("./DllPlugin"), | 
					
						
							|  |  |  | 	DllReferencePlugin: () => require("./DllReferencePlugin"), | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 	EntryPlugin: () => require("./EntryPlugin"), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	EnvironmentPlugin: () => require("./EnvironmentPlugin"), | 
					
						
							|  |  |  | 	EvalDevToolModulePlugin: () => require("./EvalDevToolModulePlugin"), | 
					
						
							|  |  |  | 	EvalSourceMapDevToolPlugin: () => require("./EvalSourceMapDevToolPlugin"), | 
					
						
							|  |  |  | 	ExtendedAPIPlugin: () => require("./ExtendedAPIPlugin"), | 
					
						
							|  |  |  | 	ExternalsPlugin: () => require("./ExternalsPlugin"), | 
					
						
							|  |  |  | 	HotModuleReplacementPlugin: () => require("./HotModuleReplacementPlugin"), | 
					
						
							|  |  |  | 	IgnorePlugin: () => require("./IgnorePlugin"), | 
					
						
							|  |  |  | 	LibraryTemplatePlugin: () => require("./LibraryTemplatePlugin"), | 
					
						
							|  |  |  | 	LoaderOptionsPlugin: () => require("./LoaderOptionsPlugin"), | 
					
						
							|  |  |  | 	LoaderTargetPlugin: () => require("./LoaderTargetPlugin"), | 
					
						
							|  |  |  | 	MemoryOutputFileSystem: () => require("./MemoryOutputFileSystem"), | 
					
						
							| 
									
										
										
										
											2018-03-03 22:41:30 +08:00
										 |  |  | 	Module: () => require("./Module"), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	ModuleFilenameHelpers: () => require("./ModuleFilenameHelpers"), | 
					
						
							|  |  |  | 	NoEmitOnErrorsPlugin: () => require("./NoEmitOnErrorsPlugin"), | 
					
						
							|  |  |  | 	NormalModuleReplacementPlugin: () => | 
					
						
							|  |  |  | 		require("./NormalModuleReplacementPlugin"), | 
					
						
							|  |  |  | 	PrefetchPlugin: () => require("./PrefetchPlugin"), | 
					
						
							|  |  |  | 	ProgressPlugin: () => require("./ProgressPlugin"), | 
					
						
							|  |  |  | 	ProvidePlugin: () => require("./ProvidePlugin"), | 
					
						
							| 
									
										
										
										
											2018-08-16 03:05:04 +08:00
										 |  |  | 	SingleEntryPlugin: util.deprecate( | 
					
						
							|  |  |  | 		() => require("./EntryPlugin"), | 
					
						
							|  |  |  | 		"SingleEntryPlugin was renamed to EntryPlugin" | 
					
						
							|  |  |  | 	), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	SetVarMainTemplatePlugin: () => require("./SetVarMainTemplatePlugin"), | 
					
						
							|  |  |  | 	SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"), | 
					
						
							|  |  |  | 	Stats: () => require("./Stats"), | 
					
						
							| 
									
										
										
										
											2018-03-03 22:41:30 +08:00
										 |  |  | 	Template: () => require("./Template"), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	UmdMainTemplatePlugin: () => require("./UmdMainTemplatePlugin"), | 
					
						
							|  |  |  | 	WatchIgnorePlugin: () => require("./WatchIgnorePlugin") | 
					
						
							| 
									
										
										
										
											2017-06-23 20:55:22 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-06-08 16:34:38 +08:00
										 |  |  | exportPlugins((exports.dependencies = {}), { | 
					
						
							|  |  |  | 	DependencyReference: () => require("./dependencies/DependencyReference") | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | exportPlugins((exports.ids = {}), { | 
					
						
							|  |  |  | 	ChunkModuleIdRangePlugin: () => require("./ids/ChunkModuleIdRangePlugin"), | 
					
						
							|  |  |  | 	NaturalModuleIdsPlugin: () => require("./ids/NaturalModuleIdsPlugin"), | 
					
						
							|  |  |  | 	OccurrenceModuleIdsPlugin: () => require("./ids/OccurrenceModuleIdsPlugin"), | 
					
						
							|  |  |  | 	NamedModuleIdsPlugin: () => require("./ids/NamedModuleIdsPlugin"), | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 	NamedChunkIdsPlugin: () => require("./ids/NamedChunkIdsPlugin"), | 
					
						
							|  |  |  | 	OccurrenceChunkIdsPlugin: () => require("./ids/OccurrenceChunkIdsPlugin"), | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | 	HashedModuleIdsPlugin: () => require("./ids/HashedModuleIdsPlugin") | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | exportPlugins((exports.optimize = {}), { | 
					
						
							|  |  |  | 	AggressiveMergingPlugin: () => require("./optimize/AggressiveMergingPlugin"), | 
					
						
							|  |  |  | 	AggressiveSplittingPlugin: () => | 
					
						
							|  |  |  | 		require("./optimize/AggressiveSplittingPlugin"), | 
					
						
							|  |  |  | 	LimitChunkCountPlugin: () => require("./optimize/LimitChunkCountPlugin"), | 
					
						
							|  |  |  | 	MinChunkSizePlugin: () => require("./optimize/MinChunkSizePlugin"), | 
					
						
							|  |  |  | 	ModuleConcatenationPlugin: () => | 
					
						
							|  |  |  | 		require("./optimize/ModuleConcatenationPlugin"), | 
					
						
							|  |  |  | 	RuntimeChunkPlugin: () => require("./optimize/RuntimeChunkPlugin"), | 
					
						
							|  |  |  | 	SideEffectsFlagPlugin: () => require("./optimize/SideEffectsFlagPlugin"), | 
					
						
							|  |  |  | 	SplitChunksPlugin: () => require("./optimize/SplitChunksPlugin") | 
					
						
							| 
									
										
										
										
											2017-06-23 20:55:22 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | exportPlugins((exports.web = {}), { | 
					
						
							|  |  |  | 	FetchCompileWasmTemplatePlugin: () => | 
					
						
							|  |  |  | 		require("./web/FetchCompileWasmTemplatePlugin"), | 
					
						
							|  |  |  | 	JsonpTemplatePlugin: () => require("./web/JsonpTemplatePlugin") | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | exportPlugins((exports.webworker = {}), { | 
					
						
							|  |  |  | 	WebWorkerTemplatePlugin: () => require("./webworker/WebWorkerTemplatePlugin") | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | exportPlugins((exports.node = {}), { | 
					
						
							|  |  |  | 	NodeTemplatePlugin: () => require("./node/NodeTemplatePlugin"), | 
					
						
							|  |  |  | 	ReadFileCompileWasmTemplatePlugin: () => | 
					
						
							|  |  |  | 		require("./node/ReadFileCompileWasmTemplatePlugin") | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | exportPlugins((exports.debug = {}), { | 
					
						
							|  |  |  | 	ProfilingPlugin: () => require("./debug/ProfilingPlugin") | 
					
						
							| 
									
										
										
										
											2017-12-26 11:12:08 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-03-23 02:52:11 +08:00
										 |  |  | exportPlugins((exports.util = {}), { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 	createHash: () => require("./util/createHash"), | 
					
						
							|  |  |  | 	comparators: () => require("./util/comparators") | 
					
						
							| 
									
										
										
										
											2018-03-23 02:52:11 +08:00
										 |  |  | }); |