| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2020-12-22 21:51:09 +08:00
										 |  |  | const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression"); | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2020-12-22 21:51:09 +08:00
										 |  |  | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */ | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2020-02-19 12:58:03 +08:00
										 |  |  | 		type: undefined, // type is not known, depends on environment
 | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		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
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2018-11-06 02:03:12 +08:00
										 |  |  | 	"require.onError": { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.uncaughtErrorHandler, | 
					
						
							| 
									
										
										
										
											2018-11-30 19:22:36 +08:00
										 |  |  | 		req: [RuntimeGlobals.uncaughtErrorHandler], | 
					
						
							| 
									
										
										
										
											2020-02-19 12:58:03 +08:00
										 |  |  | 		type: undefined, // type is not known, could be function or undefined
 | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 		assign: true // is never a pattern
 | 
					
						
							| 
									
										
										
										
											2019-05-13 04:29:50 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__system_context__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.systemContext, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.systemContext], | 
					
						
							|  |  |  | 		type: "object", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							| 
									
										
										
										
											2020-05-23 22:08:51 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_share_scopes__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.shareScopeMap, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.shareScopeMap], | 
					
						
							|  |  |  | 		type: "object", | 
					
						
							|  |  |  | 		assign: false | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	__webpack_init_sharing__: { | 
					
						
							|  |  |  | 		expr: RuntimeGlobals.initializeSharing, | 
					
						
							|  |  |  | 		req: [RuntimeGlobals.initializeSharing], | 
					
						
							|  |  |  | 		type: "function", | 
					
						
							|  |  |  | 		assign: true | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-04-30 23:52:27 +08:00
										 |  |  | 					.tap("APIPlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-09-02 00:09:28 +08:00
										 |  |  | 						compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule()); | 
					
						
							| 
									
										
										
										
											2018-11-06 01:54:05 +08:00
										 |  |  | 						return true; | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 21:51:09 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {JavascriptParser} parser the parser | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2020-12-22 21:51:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					parser.hooks.expression | 
					
						
							|  |  |  | 						.for("__webpack_layer__") | 
					
						
							|  |  |  | 						.tap("APIPlugin", expr => { | 
					
						
							|  |  |  | 							const dep = new ConstDependency( | 
					
						
							|  |  |  | 								JSON.stringify(parser.state.module.layer), | 
					
						
							|  |  |  | 								expr.range | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							dep.loc = expr.loc; | 
					
						
							|  |  |  | 							parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 							return true; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					parser.hooks.evaluateIdentifier | 
					
						
							|  |  |  | 						.for("__webpack_layer__") | 
					
						
							|  |  |  | 						.tap("APIPlugin", expr => | 
					
						
							|  |  |  | 							(parser.state.module.layer === null | 
					
						
							|  |  |  | 								? new BasicEvaluatedExpression().setNull() | 
					
						
							|  |  |  | 								: new BasicEvaluatedExpression().setString( | 
					
						
							|  |  |  | 										parser.state.module.layer | 
					
						
							|  |  |  | 								  ) | 
					
						
							|  |  |  | 							).setRange(expr.range) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 						.for("__webpack_layer__") | 
					
						
							|  |  |  | 						.tap("APIPlugin", expr => | 
					
						
							|  |  |  | 							new BasicEvaluatedExpression() | 
					
						
							|  |  |  | 								.setString( | 
					
						
							|  |  |  | 									parser.state.module.layer === null ? "object" : "string" | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 								.setRange(expr.range) | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											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; |