| | |
| | | const RuntimeGlobals = require("./RuntimeGlobals"); |
| | | const WebpackError = require("./WebpackError"); |
| | | const ConstDependency = require("./dependencies/ConstDependency"); |
| | | const ModuleInitFragmentDependency = require("./dependencies/ModuleInitFragmentDependency"); |
| | | const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency"); |
| | | const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression"); |
| | | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin"); |
| | | const { |
| | |
| | | /** @typedef {import("./javascript/JavascriptParser").Range} Range */ |
| | | |
| | | /** |
| | | * @returns {Record<string, {expr: string, req: string[] | null, type?: string, assign: boolean}>} replacements |
| | | * Returns the replacement definitions used for webpack API identifiers. |
| | | * @returns {Record<string, { expr: string, req: string[] | null, type?: string, assign: boolean }>} replacements |
| | | */ |
| | | function getReplacements() { |
| | | return { |
| | | __webpack_require__: { |
| | | expr: RuntimeGlobals.require, |
| | | req: [RuntimeGlobals.require], |
| | | type: "function", |
| | | assign: false |
| | | }, |
| | | __webpack_global__: { |
| | | expr: RuntimeGlobals.require, |
| | | req: [RuntimeGlobals.require], |
| | | type: "function", |
| | |
| | | |
| | | class APIPlugin { |
| | | /** |
| | | * Apply the plugin |
| | | * Applies the plugin by registering its hooks on the compiler. |
| | | * @param {Compiler} compiler the compiler instance |
| | | * @returns {void} |
| | | */ |
| | |
| | | compilation.dependencyTemplates.set( |
| | | ConstDependency, |
| | | new ConstDependency.Template() |
| | | ); |
| | | compilation.dependencyTemplates.set( |
| | | ModuleInitFragmentDependency, |
| | | new ModuleInitFragmentDependency.Template() |
| | | ); |
| | | |
| | | compilation.hooks.runtimeRequirementInTree |
| | |
| | | ); |
| | | |
| | | /** |
| | | * Handles the hook callback for this code path. |
| | | * @param {JavascriptParser} parser the parser |
| | | */ |
| | | const handler = (parser) => { |
| | | parser.hooks.preDeclarator.tap(PLUGIN_NAME, (declarator) => { |
| | | if ( |
| | | parser.scope.topLevelScope === true && |
| | | declarator.id.type === "Identifier" && |
| | | declarator.id.name === "module" |
| | | ) { |
| | | /** @type {BuildInfo} */ |
| | | (parser.state.module.buildInfo).moduleArgument = |
| | | "__webpack_module__"; |
| | | } |
| | | }); |
| | | |
| | | parser.hooks.preStatement.tap(PLUGIN_NAME, (statement) => { |
| | | if (parser.scope.topLevelScope === true) { |
| | | if ( |
| | | statement.type === "FunctionDeclaration" && |
| | | statement.id && |
| | | statement.id.name === "module" |
| | | ) { |
| | | /** @type {BuildInfo} */ |
| | | (parser.state.module.buildInfo).moduleArgument = |
| | | "__webpack_module__"; |
| | | } else if ( |
| | | statement.type === "ClassDeclaration" && |
| | | statement.id && |
| | | statement.id.name === "module" |
| | | ) { |
| | | /** @type {BuildInfo} */ |
| | | (parser.state.module.buildInfo).moduleArgument = |
| | | "__webpack_module__"; |
| | | } |
| | | } |
| | | }); |
| | | |
| | | for (const key of Object.keys(REPLACEMENTS)) { |
| | | const info = REPLACEMENTS[key]; |
| | | parser.hooks.expression.for(key).tap(PLUGIN_NAME, (expression) => { |
| | |
| | | /** @type {BuildInfo} */ |
| | | (parser.state.module.buildInfo).moduleConcatenationBailout = |
| | | "__webpack_module__.id"; |
| | | const dep = new ConstDependency( |
| | | `${parser.state.module.moduleArgument}.id`, |
| | | /** @type {Range} */ (expr.range), |
| | | [RuntimeGlobals.moduleId] |
| | | ); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | const moduleArgument = parser.state.module.moduleArgument; |
| | | if (moduleArgument === "__webpack_module__") { |
| | | const dep = new RuntimeRequirementsDependency([ |
| | | RuntimeGlobals.moduleId |
| | | ]); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | } else { |
| | | const initDep = new ModuleInitFragmentDependency( |
| | | `var __webpack_internal_module_id__ = ${moduleArgument}.id;\n`, |
| | | [RuntimeGlobals.moduleId], |
| | | "__webpack_internal_module_id__" |
| | | ); |
| | | parser.state.module.addPresentationalDependency(initDep); |
| | | const dep = new ConstDependency( |
| | | "__webpack_internal_module_id__", |
| | | /** @type {Range} */ (expr.range), |
| | | [] |
| | | ); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | } |
| | | return true; |
| | | }); |
| | | |
| | |
| | | /** @type {BuildInfo} */ |
| | | (parser.state.module.buildInfo).moduleConcatenationBailout = |
| | | "__webpack_module__"; |
| | | const dep = new ConstDependency( |
| | | parser.state.module.moduleArgument, |
| | | /** @type {Range} */ (expr.range), |
| | | [RuntimeGlobals.module] |
| | | ); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | const moduleArgument = parser.state.module.moduleArgument; |
| | | if (moduleArgument === "__webpack_module__") { |
| | | const dep = new RuntimeRequirementsDependency([ |
| | | RuntimeGlobals.module |
| | | ]); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | } else { |
| | | const initDep = new ModuleInitFragmentDependency( |
| | | `var __webpack_internal_module__ = ${moduleArgument};\n`, |
| | | [RuntimeGlobals.module], |
| | | "__webpack_internal_module__" |
| | | ); |
| | | parser.state.module.addPresentationalDependency(initDep); |
| | | const dep = new ConstDependency( |
| | | "__webpack_internal_module__", |
| | | /** @type {Range} */ (expr.range), |
| | | [] |
| | | ); |
| | | dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| | | parser.state.module.addPresentationalDependency(dep); |
| | | } |
| | | return true; |
| | | }); |
| | | parser.hooks.evaluateTypeof |