| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | const { fileURLToPath } = require("url"); | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | const CommentCompilationWarning = require("../CommentCompilationWarning"); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning"); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | const WebpackError = require("../WebpackError"); | 
					
						
							|  |  |  | const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression"); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	evaluateToIdentifier, | 
					
						
							|  |  |  | 	evaluateToString, | 
					
						
							|  |  |  | 	expressionIsUnsupported, | 
					
						
							|  |  |  | 	toConstantDependency | 
					
						
							|  |  |  | } = require("../javascript/JavascriptParserHelpers"); | 
					
						
							|  |  |  | const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency"); | 
					
						
							|  |  |  | const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency"); | 
					
						
							|  |  |  | const CommonJsRequireDependency = require("./CommonJsRequireDependency"); | 
					
						
							|  |  |  | const ConstDependency = require("./ConstDependency"); | 
					
						
							|  |  |  | const ContextDependencyHelpers = require("./ContextDependencyHelpers"); | 
					
						
							|  |  |  | const LocalModuleDependency = require("./LocalModuleDependency"); | 
					
						
							|  |  |  | const { getLocalModule } = require("./LocalModulesHelpers"); | 
					
						
							|  |  |  | const RequireHeaderDependency = require("./RequireHeaderDependency"); | 
					
						
							|  |  |  | const RequireResolveContextDependency = require("./RequireResolveContextDependency"); | 
					
						
							|  |  |  | const RequireResolveDependency = require("./RequireResolveDependency"); | 
					
						
							|  |  |  | const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | /** @typedef {import("estree").CallExpression} CallExpression */ | 
					
						
							|  |  |  | /** @typedef {import("estree").Expression} Expression */ | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** @typedef {import("estree").NewExpression} NewExpression */ | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ | 
					
						
							|  |  |  | /** @typedef {import("../javascript/JavascriptParser").ImportSource} ImportSource */ | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser").Range} Range */ | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | const createRequireSpecifierTag = Symbol("createRequire"); | 
					
						
							|  |  |  | const createdRequireIdentifierTag = Symbol("createRequire()"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | class CommonJsImportsParserPlugin { | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {JavascriptParserOptions} options parser options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {JavascriptParser} parser the parser | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 	apply(parser) { | 
					
						
							|  |  |  | 		const options = this.options; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 		const getContext = () => { | 
					
						
							|  |  |  | 			if (parser.currentTagData) { | 
					
						
							|  |  |  | 				const { context } = parser.currentTagData; | 
					
						
							|  |  |  | 				return context; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region metadata
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							| 
									
										
										
										
											2024-01-23 22:42:10 +08:00
										 |  |  | 		 * @param {string} expression expression | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		 * @param {() => string[]} getMembers get members | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const tapRequireExpression = (expression, getMembers) => { | 
					
						
							|  |  |  | 			parser.hooks.typeof | 
					
						
							|  |  |  | 				.for(expression) | 
					
						
							|  |  |  | 				.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 					"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					toConstantDependency(parser, JSON.stringify("function")) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 				.for(expression) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				.tap("CommonJsImportsParserPlugin", evaluateToString("function")); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			parser.hooks.evaluateIdentifier | 
					
						
							|  |  |  | 				.for(expression) | 
					
						
							|  |  |  | 				.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 					"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					evaluateToIdentifier(expression, "require", getMembers, true) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {string | symbol} tag tag | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		const tapRequireExpressionTag = tag => { | 
					
						
							|  |  |  | 			parser.hooks.typeof | 
					
						
							|  |  |  | 				.for(tag) | 
					
						
							|  |  |  | 				.tap( | 
					
						
							|  |  |  | 					"CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 					toConstantDependency(parser, JSON.stringify("function")) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 				.for(tag) | 
					
						
							|  |  |  | 				.tap("CommonJsImportsParserPlugin", evaluateToString("function")); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		tapRequireExpression("require", () => []); | 
					
						
							|  |  |  | 		tapRequireExpression("require.resolve", () => ["resolve"]); | 
					
						
							|  |  |  | 		tapRequireExpression("require.resolveWeak", () => ["resolveWeak"]); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Weird stuff //
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		parser.hooks.assign | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", expr => { | 
					
						
							|  |  |  | 				// to not leak to global "require", we need to define a local require here.
 | 
					
						
							|  |  |  | 				const dep = new ConstDependency("var require;", 0); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Unsupported
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		parser.hooks.expression | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			.for("require.main") | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				expressionIsUnsupported( | 
					
						
							|  |  |  | 					parser, | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 					"require.main is not supported by webpack." | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-12-05 18:43:04 +08:00
										 |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require.main.require") | 
					
						
							|  |  |  | 			.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 18:43:04 +08:00
										 |  |  | 				expressionIsUnsupported( | 
					
						
							|  |  |  | 					parser, | 
					
						
							|  |  |  | 					"require.main.require is not supported by webpack." | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for("module.parent.require") | 
					
						
							|  |  |  | 			.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				expressionIsUnsupported( | 
					
						
							|  |  |  | 					parser, | 
					
						
							|  |  |  | 					"module.parent.require is not supported by webpack." | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-12-05 18:43:04 +08:00
										 |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("module.parent.require") | 
					
						
							|  |  |  | 			.tap( | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							| 
									
										
										
										
											2019-12-05 18:43:04 +08:00
										 |  |  | 				expressionIsUnsupported( | 
					
						
							|  |  |  | 					parser, | 
					
						
							|  |  |  | 					"module.parent.require is not supported by webpack." | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Renaming
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {Expression} expr expression | 
					
						
							|  |  |  | 		 * @returns {boolean} true when set undefined | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		const defineUndefined = expr => { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			// To avoid "not defined" error, replace the value with undefined
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 			const dep = new ConstDependency( | 
					
						
							|  |  |  | 				"undefined", | 
					
						
							|  |  |  | 				/** @type {Range} */ (expr.range) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		parser.hooks.canRename | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", () => true); | 
					
						
							|  |  |  | 		parser.hooks.rename | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", defineUndefined); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Inspection
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		const requireCache = toConstantDependency( | 
					
						
							|  |  |  | 			parser, | 
					
						
							|  |  |  | 			RuntimeGlobals.moduleCache, | 
					
						
							|  |  |  | 			[ | 
					
						
							|  |  |  | 				RuntimeGlobals.moduleCache, | 
					
						
							|  |  |  | 				RuntimeGlobals.moduleId, | 
					
						
							|  |  |  | 				RuntimeGlobals.moduleLoaded | 
					
						
							|  |  |  | 			] | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for("require.cache") | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			.tap("CommonJsImportsParserPlugin", requireCache); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Require as expression
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {Expression} expr expression | 
					
						
							|  |  |  | 		 * @returns {boolean} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 		const requireAsExpressionHandler = expr => { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			const dep = new CommonJsRequireContextDependency( | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					request: options.unknownContextRequest, | 
					
						
							|  |  |  | 					recursive: options.unknownContextRecursive, | 
					
						
							|  |  |  | 					regExp: options.unknownContextRegExp, | 
					
						
							|  |  |  | 					mode: "sync" | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				/** @type {Range} */ (expr.range), | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				undefined, | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				parser.scope.inShorthand, | 
					
						
							|  |  |  | 				getContext() | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			dep.critical = | 
					
						
							|  |  |  | 				options.unknownContextCritical && | 
					
						
							|  |  |  | 				"require function is used in a way in which dependencies cannot be statically extracted"; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 			dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 			dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 			.tap("CommonJsImportsParserPlugin", requireAsExpressionHandler); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Require
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression | NewExpression} expr expression | 
					
						
							|  |  |  | 		 * @param {BasicEvaluatedExpression} param param | 
					
						
							|  |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const processRequireItem = (expr, param) => { | 
					
						
							|  |  |  | 			if (param.isString()) { | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				const dep = new CommonJsRequireDependency( | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					/** @type {string} */ (param.string), | 
					
						
							|  |  |  | 					/** @type {Range} */ (param.range), | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 					getContext() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 				dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression | NewExpression} expr expression | 
					
						
							|  |  |  | 		 * @param {BasicEvaluatedExpression} param param | 
					
						
							|  |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const processRequireContext = (expr, param) => { | 
					
						
							|  |  |  | 			const dep = ContextDependencyHelpers.create( | 
					
						
							|  |  |  | 				CommonJsRequireContextDependency, | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				/** @type {Range} */ (expr.range), | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				param, | 
					
						
							|  |  |  | 				expr, | 
					
						
							|  |  |  | 				options, | 
					
						
							| 
									
										
										
										
											2020-06-18 05:03:02 +08:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					category: "commonjs" | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				parser, | 
					
						
							|  |  |  | 				undefined, | 
					
						
							|  |  |  | 				getContext() | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			if (!dep) return; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 			dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 			dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {boolean} callNew true, when require is called with new | 
					
						
							|  |  |  | 		 * @returns {(expr: CallExpression | NewExpression) => (boolean | void)} handler | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const createRequireHandler = callNew => expr => { | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 			if (options.commonjsMagicComments) { | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 				const { options: requireOptions, errors: commentErrors } = | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					parser.parseCommentOptions(/** @type {Range} */ (expr.range)); | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 				if (commentErrors) { | 
					
						
							|  |  |  | 					for (const e of commentErrors) { | 
					
						
							|  |  |  | 						const { comment } = e; | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | 						parser.state.module.addWarning( | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 							new CommentCompilationWarning( | 
					
						
							|  |  |  | 								`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, | 
					
						
							| 
									
										
										
										
											2024-10-16 22:42:26 +08:00
										 |  |  | 								/** @type {DependencyLocation} */ (comment.loc) | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-01-20 22:08:58 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 				if (requireOptions && requireOptions.webpackIgnore !== undefined) { | 
					
						
							|  |  |  | 					if (typeof requireOptions.webpackIgnore !== "boolean") { | 
					
						
							|  |  |  | 						parser.state.module.addWarning( | 
					
						
							|  |  |  | 							new UnsupportedFeatureWarning( | 
					
						
							|  |  |  | 								`\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`, | 
					
						
							|  |  |  | 								/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} else if (requireOptions.webpackIgnore) { | 
					
						
							|  |  |  | 						// Do not instrument `require()` if `webpackIgnore` is `true`
 | 
					
						
							|  |  |  | 						return true; | 
					
						
							| 
									
										
										
										
											2021-01-20 03:54:29 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			if (expr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 			let localModule; | 
					
						
							|  |  |  | 			const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 			if (param.isConditional()) { | 
					
						
							|  |  |  | 				let isExpression = false; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				for (const p of /** @type {BasicEvaluatedExpression[]} */ ( | 
					
						
							|  |  |  | 					param.options | 
					
						
							|  |  |  | 				)) { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					const result = processRequireItem(expr, p); | 
					
						
							|  |  |  | 					if (result === undefined) { | 
					
						
							|  |  |  | 						isExpression = true; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!isExpression) { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					const dep = new RequireHeaderDependency( | 
					
						
							|  |  |  | 						/** @type {Range} */ (expr.callee.range) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				param.isString() && | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				(localModule = getLocalModule( | 
					
						
							|  |  |  | 					parser.state, | 
					
						
							|  |  |  | 					/** @type {string} */ (param.string) | 
					
						
							|  |  |  | 				)) | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			) { | 
					
						
							|  |  |  | 				localModule.flagUsed(); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				const dep = new LocalModuleDependency( | 
					
						
							|  |  |  | 					localModule, | 
					
						
							|  |  |  | 					/** @type {Range} */ (expr.range), | 
					
						
							|  |  |  | 					callNew | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				const result = processRequireItem(expr, param); | 
					
						
							|  |  |  | 				if (result === undefined) { | 
					
						
							|  |  |  | 					processRequireContext(expr, param); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					const dep = new RequireHeaderDependency( | 
					
						
							|  |  |  | 						/** @type {Range} */ (expr.callee.range) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-02-18 14:00:45 +08:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", createRequireHandler(false)); | 
					
						
							|  |  |  | 		parser.hooks.new | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", createRequireHandler(true)); | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", createRequireHandler(false)); | 
					
						
							|  |  |  | 		parser.hooks.new | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", createRequireHandler(true)); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Require with property access
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {Expression} expr expression | 
					
						
							|  |  |  | 		 * @param {string[]} calleeMembers callee members | 
					
						
							|  |  |  | 		 * @param {CallExpression} callExpr call expression | 
					
						
							|  |  |  | 		 * @param {string[]} members members | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 		 * @param {Range[]} memberRanges member ranges | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 		const chainHandler = ( | 
					
						
							|  |  |  | 			expr, | 
					
						
							|  |  |  | 			calleeMembers, | 
					
						
							|  |  |  | 			callExpr, | 
					
						
							|  |  |  | 			members, | 
					
						
							|  |  |  | 			memberRanges | 
					
						
							|  |  |  | 		) => { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			if (callExpr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 			const param = parser.evaluateExpression(callExpr.arguments[0]); | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 			if ( | 
					
						
							|  |  |  | 				param.isString() && | 
					
						
							|  |  |  | 				!getLocalModule(parser.state, /** @type {string} */ (param.string)) | 
					
						
							|  |  |  | 			) { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				const dep = new CommonJsFullRequireDependency( | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 					/** @type {string} */ (param.string), | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					/** @type {Range} */ (expr.range), | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 					members, | 
					
						
							|  |  |  | 					/** @type {Range[]} */ memberRanges | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.asiSafe = !parser.isAsiPosition( | 
					
						
							|  |  |  | 					/** @type {Range} */ (expr.range)[0] | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 				dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-11-14 01:27:23 +08:00
										 |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression} expr expression | 
					
						
							|  |  |  | 		 * @param {string[]} calleeMembers callee members | 
					
						
							|  |  |  | 		 * @param {CallExpression} callExpr call expression | 
					
						
							|  |  |  | 		 * @param {string[]} members members | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 		 * @param {Range[]} memberRanges member ranges | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 		const callChainHandler = ( | 
					
						
							|  |  |  | 			expr, | 
					
						
							|  |  |  | 			calleeMembers, | 
					
						
							|  |  |  | 			callExpr, | 
					
						
							|  |  |  | 			members, | 
					
						
							|  |  |  | 			memberRanges | 
					
						
							|  |  |  | 		) => { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			if (callExpr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 			const param = parser.evaluateExpression(callExpr.arguments[0]); | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 			if ( | 
					
						
							|  |  |  | 				param.isString() && | 
					
						
							|  |  |  | 				!getLocalModule(parser.state, /** @type {string} */ (param.string)) | 
					
						
							|  |  |  | 			) { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				const dep = new CommonJsFullRequireDependency( | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 					/** @type {string} */ (param.string), | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					/** @type {Range} */ (expr.callee.range), | 
					
						
							| 
									
										
										
										
											2023-10-01 13:13:01 +08:00
										 |  |  | 					members, | 
					
						
							|  |  |  | 					/** @type {Range[]} */ memberRanges | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 				dep.call = true; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.asiSafe = !parser.isAsiPosition( | 
					
						
							|  |  |  | 					/** @type {Range} */ (expr.range)[0] | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 				dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.callee.loc); | 
					
						
							| 
									
										
										
										
											2021-11-14 01:27:23 +08:00
										 |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							| 
									
										
										
										
											2019-12-24 00:43:41 +08:00
										 |  |  | 				parser.walkExpressions(expr.arguments); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		parser.hooks.memberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", chainHandler); | 
					
						
							|  |  |  | 		parser.hooks.memberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", chainHandler); | 
					
						
							|  |  |  | 		parser.hooks.callMemberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for("require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", callChainHandler); | 
					
						
							|  |  |  | 		parser.hooks.callMemberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for("module.require") | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", callChainHandler); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Require.resolve
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression} expr call expression | 
					
						
							|  |  |  | 		 * @param {boolean} weak weak | 
					
						
							|  |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const processResolve = (expr, weak) => { | 
					
						
							|  |  |  | 			if (expr.arguments.length !== 1) return; | 
					
						
							|  |  |  | 			const param = parser.evaluateExpression(expr.arguments[0]); | 
					
						
							|  |  |  | 			if (param.isConditional()) { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				for (const option of /** @type {BasicEvaluatedExpression[]} */ ( | 
					
						
							|  |  |  | 					param.options | 
					
						
							|  |  |  | 				)) { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					const result = processResolveItem(expr, option, weak); | 
					
						
							|  |  |  | 					if (result === undefined) { | 
					
						
							|  |  |  | 						processResolveContext(expr, option, weak); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				const dep = new RequireResolveHeaderDependency( | 
					
						
							|  |  |  | 					/** @type {Range} */ (expr.callee.range) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 			const result = processResolveItem(expr, param, weak); | 
					
						
							|  |  |  | 			if (result === undefined) { | 
					
						
							|  |  |  | 				processResolveContext(expr, param, weak); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			const dep = new RequireResolveHeaderDependency( | 
					
						
							|  |  |  | 				/** @type {Range} */ (expr.callee.range) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							|  |  |  | 			parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression} expr call expression | 
					
						
							|  |  |  | 		 * @param {BasicEvaluatedExpression} param param | 
					
						
							|  |  |  | 		 * @param {boolean} weak weak | 
					
						
							|  |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const processResolveItem = (expr, param, weak) => { | 
					
						
							|  |  |  | 			if (param.isString()) { | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				const dep = new RequireResolveDependency( | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					/** @type {string} */ (param.string), | 
					
						
							|  |  |  | 					/** @type {Range} */ (param.range), | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 					getContext() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 				dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				dep.weak = weak; | 
					
						
							|  |  |  | 				parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {CallExpression} expr call expression | 
					
						
							|  |  |  | 		 * @param {BasicEvaluatedExpression} param param | 
					
						
							|  |  |  | 		 * @param {boolean} weak weak | 
					
						
							|  |  |  | 		 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		const processResolveContext = (expr, param, weak) => { | 
					
						
							|  |  |  | 			const dep = ContextDependencyHelpers.create( | 
					
						
							|  |  |  | 				RequireResolveContextDependency, | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				/** @type {Range} */ (param.range), | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 				param, | 
					
						
							|  |  |  | 				expr, | 
					
						
							|  |  |  | 				options, | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2020-06-18 05:03:02 +08:00
										 |  |  | 					category: "commonjs", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					mode: weak ? "weak" : "sync" | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				parser, | 
					
						
							|  |  |  | 				getContext() | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			if (!dep) return; | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 			dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2024-07-31 11:11:11 +08:00
										 |  |  | 			dep.optional = Boolean(parser.scope.inTry); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			parser.state.current.addDependency(dep); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require.resolve") | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 			.tap("CommonJsImportsParserPlugin", expr => processResolve(expr, false)); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for("require.resolveWeak") | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 			.tap("CommonJsImportsParserPlugin", expr => processResolve(expr, true)); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #region Create require
 | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!options.createRequire) return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		/** @type {ImportSource[]} */ | 
					
						
							| 
									
										
										
										
											2023-04-04 07:42:29 +08:00
										 |  |  | 		let moduleName = []; | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		/** @type {string | undefined} */ | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 		let specifierName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (options.createRequire === true) { | 
					
						
							| 
									
										
										
										
											2023-04-04 07:42:29 +08:00
										 |  |  | 			moduleName = ["module", "node:module"]; | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 			specifierName = "createRequire"; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-04-04 07:42:29 +08:00
										 |  |  | 			let moduleName; | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 			const match = /^(.*) from (.*)$/.exec(options.createRequire); | 
					
						
							|  |  |  | 			if (match) { | 
					
						
							|  |  |  | 				[, specifierName, moduleName] = match; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!specifierName || !moduleName) { | 
					
						
							|  |  |  | 				const err = new WebpackError( | 
					
						
							|  |  |  | 					`Parsing javascript parser option "createRequire" failed, got ${JSON.stringify( | 
					
						
							|  |  |  | 						options.createRequire | 
					
						
							|  |  |  | 					)}`
 | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				err.details = | 
					
						
							|  |  |  | 					'Expected string in format "createRequire from module", where "createRequire" is specifier name and "module" name of the module'; | 
					
						
							|  |  |  | 				throw err; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tapRequireExpressionTag(createdRequireIdentifierTag); | 
					
						
							|  |  |  | 		tapRequireExpressionTag(createRequireSpecifierTag); | 
					
						
							|  |  |  | 		parser.hooks.evaluateCallExpression | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", expr => { | 
					
						
							|  |  |  | 				const context = parseCreateRequireArguments(expr); | 
					
						
							|  |  |  | 				if (context === undefined) return; | 
					
						
							|  |  |  | 				const ident = parser.evaluatedVariable({ | 
					
						
							|  |  |  | 					tag: createdRequireIdentifierTag, | 
					
						
							|  |  |  | 					data: { context }, | 
					
						
							|  |  |  | 					next: undefined | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2024-09-26 00:00:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 				return new BasicEvaluatedExpression() | 
					
						
							| 
									
										
										
										
											2024-09-26 00:00:15 +08:00
										 |  |  | 					.setIdentifier(ident, ident, () => []) | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 					.setSideEffects(false) | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					.setRange(/** @type {Range} */ (expr.range)); | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		parser.hooks.unhandledExpressionMemberChain | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 			.tap("CommonJsImportsParserPlugin", (expr, members) => | 
					
						
							|  |  |  | 				expressionIsUnsupported( | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 					parser, | 
					
						
							|  |  |  | 					`createRequire().${members.join(".")} is not supported by webpack.` | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 				)(expr) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 		parser.hooks.canRename | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", () => true); | 
					
						
							|  |  |  | 		parser.hooks.canRename | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", () => true); | 
					
						
							|  |  |  | 		parser.hooks.rename | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", defineUndefined); | 
					
						
							|  |  |  | 		parser.hooks.expression | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", requireAsExpressionHandler); | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", createRequireHandler(false)); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		/** | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 		 * @param {CallExpression} expr call expression | 
					
						
							|  |  |  | 		 * @returns {string | void} context | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		 */ | 
					
						
							|  |  |  | 		const parseCreateRequireArguments = expr => { | 
					
						
							|  |  |  | 			const args = expr.arguments; | 
					
						
							|  |  |  | 			if (args.length !== 1) { | 
					
						
							|  |  |  | 				const err = new WebpackError( | 
					
						
							|  |  |  | 					"module.createRequire supports only one argument." | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				err.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.state.module.addWarning(err); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 			const arg = args[0]; | 
					
						
							|  |  |  | 			const evaluated = parser.evaluateExpression(arg); | 
					
						
							|  |  |  | 			if (!evaluated.isString()) { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				const err = new WebpackError( | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 					"module.createRequire failed parsing argument." | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				err.loc = /** @type {DependencyLocation} */ (arg.loc); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.state.module.addWarning(err); | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 			const ctx = /** @type {string} */ (evaluated.string).startsWith("file://") | 
					
						
							|  |  |  | 				? fileURLToPath(/** @type {string} */ (evaluated.string)) | 
					
						
							|  |  |  | 				: /** @type {string} */ (evaluated.string); | 
					
						
							| 
									
										
										
										
											2022-03-28 23:51:48 +08:00
										 |  |  | 			// argument always should be a filename
 | 
					
						
							|  |  |  | 			return ctx.slice(0, ctx.lastIndexOf(ctx.startsWith("/") ? "/" : "\\")); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		parser.hooks.import.tap( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 				stage: -10 | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			(statement, source) => { | 
					
						
							|  |  |  | 				if ( | 
					
						
							| 
									
										
										
										
											2023-04-04 07:42:29 +08:00
										 |  |  | 					!moduleName.includes(source) || | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 					statement.specifiers.length !== 1 || | 
					
						
							|  |  |  | 					statement.specifiers[0].type !== "ImportSpecifier" || | 
					
						
							|  |  |  | 					statement.specifiers[0].imported.type !== "Identifier" || | 
					
						
							| 
									
										
										
										
											2022-03-28 22:16:31 +08:00
										 |  |  | 					statement.specifiers[0].imported.name !== specifierName | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				) | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				// clear for 'import { createRequire as x } from "module"'
 | 
					
						
							|  |  |  | 				// if any other specifier was used import module
 | 
					
						
							|  |  |  | 				const clearDep = new ConstDependency( | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					parser.isAsiPosition(/** @type {Range} */ (statement.range)[0]) | 
					
						
							|  |  |  | 						? ";" | 
					
						
							|  |  |  | 						: "", | 
					
						
							|  |  |  | 					/** @type {Range} */ (statement.range) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				clearDep.loc = /** @type {DependencyLocation} */ (statement.loc); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(clearDep); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				parser.unsetAsiPosition(/** @type {Range} */ (statement.range)[1]); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		parser.hooks.importSpecifier.tap( | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 				stage: -10 | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			(statement, source, id, name) => { | 
					
						
							| 
									
										
										
										
											2023-04-04 07:42:29 +08:00
										 |  |  | 				if (!moduleName.includes(source) || id !== specifierName) return; | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.tagVariable(name, createRequireSpecifierTag); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		parser.hooks.preDeclarator.tap( | 
					
						
							|  |  |  | 			"CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 			declarator => { | 
					
						
							|  |  |  | 				if ( | 
					
						
							|  |  |  | 					declarator.id.type !== "Identifier" || | 
					
						
							|  |  |  | 					!declarator.init || | 
					
						
							|  |  |  | 					declarator.init.type !== "CallExpression" || | 
					
						
							|  |  |  | 					declarator.init.callee.type !== "Identifier" | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 				const variableInfo = | 
					
						
							|  |  |  | 					/** @type {TODO} */ | 
					
						
							|  |  |  | 					(parser.getVariableInfo(declarator.init.callee.name)); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				if ( | 
					
						
							|  |  |  | 					variableInfo && | 
					
						
							|  |  |  | 					variableInfo.tagInfo && | 
					
						
							|  |  |  | 					variableInfo.tagInfo.tag === createRequireSpecifierTag | 
					
						
							|  |  |  | 				) { | 
					
						
							|  |  |  | 					const context = parseCreateRequireArguments(declarator.init); | 
					
						
							|  |  |  | 					if (context === undefined) return; | 
					
						
							|  |  |  | 					parser.tagVariable(declarator.id.name, createdRequireIdentifierTag, { | 
					
						
							|  |  |  | 						name: declarator.id.name, | 
					
						
							|  |  |  | 						context | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		parser.hooks.memberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap( | 
					
						
							|  |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 				(expr, calleeMembers, callExpr, members) => { | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						calleeMembers.length !== 0 || | 
					
						
							|  |  |  | 						members.length !== 1 || | 
					
						
							|  |  |  | 						members[0] !== "cache" | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					// createRequire().cache
 | 
					
						
							|  |  |  | 					const context = parseCreateRequireArguments(callExpr); | 
					
						
							|  |  |  | 					if (context === undefined) return; | 
					
						
							|  |  |  | 					return requireCache(expr); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		parser.hooks.callMemberChainOfCallMemberChain | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap( | 
					
						
							|  |  |  | 				"CommonJsImportsParserPlugin", | 
					
						
							|  |  |  | 				(expr, calleeMembers, innerCallExpression, members) => { | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						calleeMembers.length !== 0 || | 
					
						
							|  |  |  | 						members.length !== 1 || | 
					
						
							|  |  |  | 						members[0] !== "resolve" | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					// createRequire().resolve()
 | 
					
						
							|  |  |  | 					return processResolve(expr, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		parser.hooks.expressionMemberChain | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", (expr, members) => { | 
					
						
							|  |  |  | 				// require.cache
 | 
					
						
							|  |  |  | 				if (members.length === 1 && members[0] === "cache") { | 
					
						
							|  |  |  | 					return requireCache(expr); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		parser.hooks.callMemberChain | 
					
						
							|  |  |  | 			.for(createdRequireIdentifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", (expr, members) => { | 
					
						
							|  |  |  | 				// require.resolve()
 | 
					
						
							|  |  |  | 				if (members.length === 1 && members[0] === "resolve") { | 
					
						
							|  |  |  | 					return processResolve(expr, false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		parser.hooks.call | 
					
						
							|  |  |  | 			.for(createRequireSpecifierTag) | 
					
						
							|  |  |  | 			.tap("CommonJsImportsParserPlugin", expr => { | 
					
						
							|  |  |  | 				const clearDep = new ConstDependency( | 
					
						
							|  |  |  | 					"/* createRequire() */ undefined", | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 					/** @type {Range} */ (expr.range) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 				clearDep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2022-03-24 03:02:22 +08:00
										 |  |  | 				parser.state.module.addPresentationalDependency(clearDep); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 		// #endregion
 | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = CommonJsImportsParserPlugin; |