WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
node_modules/webpack/lib/APIPlugin.js
@@ -16,6 +16,8 @@
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 {
@@ -32,11 +34,18 @@
/** @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",
@@ -132,7 +141,7 @@
class APIPlugin {
   /**
    * Apply the plugin
    * Applies the plugin by registering its hooks on the compiler.
    * @param {Compiler} compiler the compiler instance
    * @returns {void}
    */
@@ -153,6 +162,10 @@
            compilation.dependencyTemplates.set(
               ConstDependency,
               new ConstDependency.Template()
            );
            compilation.dependencyTemplates.set(
               ModuleInitFragmentDependency,
               new ModuleInitFragmentDependency.Template()
            );
            compilation.hooks.runtimeRequirementInTree
@@ -192,9 +205,44 @@
            );
            /**
             * 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) => {
@@ -269,13 +317,28 @@
                     /** @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;
                  });
@@ -285,13 +348,28 @@
                     /** @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