| 
									
										
										
										
											2013-01-31 01:49:25 +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-03 01:00:08 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 21:36:15 +08:00
										 |  |  | const RuntimeGlobals = require("./RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | const WebpackError = require("./WebpackError"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const ConstDependency = require("./dependencies/ConstDependency"); | 
					
						
							| 
									
										
										
										
											2019-10-22 15:27:52 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	toConstantDependency, | 
					
						
							|  |  |  | 	evaluateToString | 
					
						
							|  |  |  | } = require("./javascript/JavascriptParserHelpers"); | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | const ChunkNameRuntimeModule = require("./runtime/ChunkNameRuntimeModule"); | 
					
						
							|  |  |  | const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2014-06-16 21:18:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | /* eslint-disable camelcase */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | const REPLACEMENTS = { | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	__webpack_require__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.require, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.require], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "function", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_public_path__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.publicPath, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.publicPath], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "string", | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_modules__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.moduleFactories, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.moduleFactories], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "object", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_chunk_load__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.ensureChunk, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.ensureChunk], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "function", | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__non_webpack_require__: { | 
					
						
							|  |  |  | 		expr: "require", | 
					
						
							|  |  |  | 		req: null, | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: undefined, // type is not know, depends on environment
 | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_nonce__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.scriptNonce, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.scriptNonce], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "string", | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 	__webpack_hash__: { | 
					
						
							|  |  |  | 		expr: `${RuntimeGlobals.getFullHash}()`, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:22:36 +08:00
										 |  |  | 		req: [RuntimeGlobals.getFullHash], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "string", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_chunkname__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.chunkName, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:22:36 +08:00
										 |  |  | 		req: [RuntimeGlobals.chunkName], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "string", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2018-12-05 23:00:30 +08:00
										 |  |  | 	__webpack_get_script_filename__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.getChunkScriptFilename, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.getChunkScriptFilename], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: "function", | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											2018-12-05 23:00:30 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2020-02-20 17:52:47 +08:00
										 |  |  | 	__webpack_override__: { | 
					
						
							|  |  |  | 		expr: `Object.assign.bind(Object, ${RuntimeGlobals.overrides})`, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.overrides], | 
					
						
							|  |  |  | 		type: "function", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	"require.onError": { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.uncaughtErrorHandler, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:22:36 +08:00
										 |  |  | 		req: [RuntimeGlobals.uncaughtErrorHandler], | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		type: undefined, // type is not know, could be function or undefined
 | 
					
						
							|  |  |  | 		assign: true // is never a pattern
 | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-11 05:20:07 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-12-13 18:14:00 +08:00
										 |  |  | /* eslint-enable camelcase */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class APIPlugin { | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"APIPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ConstDependency, | 
					
						
							|  |  |  | 					new ConstDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.chunkName) | 
					
						
							|  |  |  | 					.tap("APIPlugin", chunk => { | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							new ChunkNameRuntimeModule(chunk.name) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						return true; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.getFullHash) | 
					
						
							|  |  |  | 					.tap("APIPlugin", chunk => { | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 						compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule()); | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 						return true; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const handler = parser => { | 
					
						
							|  |  |  | 					Object.keys(REPLACEMENTS).forEach(key => { | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 						const info = REPLACEMENTS[key]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						parser.hooks.expression | 
					
						
							|  |  |  | 							.for(key) | 
					
						
							|  |  |  | 							.tap( | 
					
						
							|  |  |  | 								"APIPlugin", | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 								toConstantDependency(parser, info.expr, info.req) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 						if (info.assign === false) { | 
					
						
							|  |  |  | 							parser.hooks.assign.for(key).tap("APIPlugin", expr => { | 
					
						
							|  |  |  | 								const err = new WebpackError(`${key} must not be assigned`); | 
					
						
							|  |  |  | 								err.loc = expr.loc; | 
					
						
							|  |  |  | 								throw err; | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 						if (info.type) { | 
					
						
							|  |  |  | 							parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 								.for(key) | 
					
						
							|  |  |  | 								.tap("APIPlugin", evaluateToString(info.type)); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-12-14 17:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/auto") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/dynamic") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							|  |  |  | 					.for("javascript/esm") | 
					
						
							|  |  |  | 					.tap("APIPlugin", handler); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-03 01:00:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = APIPlugin; |