WXL
3 天以前 9bce51f651aad297ef9eb6df832bfdaf1de05d84
node_modules/webpack/lib/optimize/ConcatenatedModule.js
@@ -16,7 +16,10 @@
const ConcatenationScope = require("../ConcatenationScope");
const { UsageState } = require("../ExportsInfo");
const Module = require("../Module");
const { JS_TYPES } = require("../ModuleSourceTypesConstants");
const {
   JAVASCRIPT_TYPE,
   JAVASCRIPT_TYPES
} = require("../ModuleSourceTypeConstants");
const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../ModuleTypeConstants");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
@@ -42,8 +45,7 @@
const createHash = require("../util/createHash");
const { makePathsRelative } = require("../util/identifier");
const makeSerializable = require("../util/makeSerializable");
const propertyAccess = require("../util/propertyAccess");
const { propertyName } = require("../util/propertyName");
const { propertyAccess, propertyName } = require("../util/property");
const {
   filterRuntime,
   intersectRuntime,
@@ -69,6 +71,7 @@
/** @typedef {import("../Module").FileSystemDependencies} FileSystemDependencies */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
/** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
/** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("../Module").LibIdent} LibIdent */
@@ -87,7 +90,8 @@
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {typeof import("../util/Hash")} HashConstructor */
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
/** @typedef {import("../util/concatenate").UsedNames} UsedNames */
/** @typedef {import("../util/concatenate").ScopeInfo} ScopeInfo */
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
/** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
@@ -132,8 +136,10 @@
 * @property {ExportName} exportName
 */
/** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo } ModuleInfo */
/** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo | ReferenceToModuleInfo } ModuleInfoOrReference */
/** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo} ModuleInfo */
/** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo | ReferenceToModuleInfo} ModuleInfoOrReference */
/** @typedef {Map<string, string>} ExportMap */
/**
 * @typedef {object} ConcatenatedModuleInfo
@@ -148,8 +154,8 @@
 * @property {Scope | undefined} globalScope
 * @property {Scope | undefined} moduleScope
 * @property {Map<string, string>} internalNames
 * @property {Map<string, string> | undefined} exportMap
 * @property {Map<string, string> | undefined} rawExportMap
 * @property {ExportMap | undefined} exportMap
 * @property {ExportMap | undefined} rawExportMap
 * @property {string=} namespaceExportSymbol
 * @property {string | undefined} namespaceObjectName
 * @property {ConcatenationScope | undefined} concatenationScope
@@ -192,7 +198,7 @@
/**
 * @template T
 * @param {string} property property
 * @param {function(T[keyof T], T[keyof T]): 0 | 1 | -1} comparator comparator
 * @param {(a: T[keyof T], b: T[keyof T]) => 0 | 1 | -1} comparator comparator
 * @returns {Comparator<T>} comparator
 */
@@ -222,6 +228,7 @@
   }
   return 0;
};
const bySourceOrder = createComparator("sourceOrder", compareNumbers);
const byRangeStart = createComparator("rangeStart", compareNumbers);
@@ -648,7 +655,9 @@
   );
   {
      const { ids, comment } = binding;
      /** @type {string} */
      let reference;
      /** @type {boolean} */
      let isPropertyAccess;
      if ("rawName" in binding) {
         reference = `${binding.rawName}${comment || ""}${propertyAccess(ids)}`;
@@ -684,7 +693,7 @@
/**
 * @typedef {object} ConcatenateModuleHooks
 * @property {SyncBailHook<[ConcatenatedModule], boolean>} onDemandExportsGeneration
 * @property {SyncBailHook<[ConcatenatedModule, RuntimeSpec[], string, Record<string, string>], boolean>} onDemandExportsGeneration
 * @property {SyncBailHook<[Partial<ConcatenatedModuleInfo>, ConcatenatedModuleInfo], boolean | void>} concatenatedModuleInfo
 */
@@ -700,7 +709,7 @@
    * @param {RuntimeSpec} runtime the runtime
    * @param {Compilation} compilation the compilation
    * @param {AssociatedObjectForCache=} associatedObjectForCache object for caching
    * @param {string | HashConstructor=} hashFunction hash function to use
    * @param {HashFunction=} hashFunction hash function to use
    * @returns {ConcatenatedModule} the module
    */
   static create(
@@ -734,7 +743,12 @@
      let hooks = compilationHooksMap.get(compilation);
      if (hooks === undefined) {
         hooks = {
            onDemandExportsGeneration: new SyncBailHook(["module"]),
            onDemandExportsGeneration: new SyncBailHook([
               "module",
               "runtimes",
               "exportsFinalName",
               "exportsSource"
            ]),
            concatenatedModuleInfo: new SyncBailHook([
               "updatedInfo",
               "concatenatedModuleInfo"
@@ -781,10 +795,11 @@
   }
   /**
    * Returns the source types this module can generate.
    * @returns {SourceTypes} types available (do not mutate)
    */
   getSourceTypes() {
      return JS_TYPES;
      return JAVASCRIPT_TYPES;
   }
   get modules() {
@@ -792,6 +807,7 @@
   }
   /**
    * Returns the unique identifier used to reference this module.
    * @returns {string} a unique identifier of the module
    */
   identifier() {
@@ -799,6 +815,7 @@
   }
   /**
    * Returns a human-readable identifier for this module.
    * @param {RequestShortener} requestShortener the request shortener
    * @returns {string} a user readable identifier of the module
    */
@@ -809,6 +826,7 @@
   }
   /**
    * Gets the library identifier.
    * @param {LibIdentOptions} options options
    * @returns {LibIdent | null} an identifier for library inclusion
    */
@@ -817,6 +835,7 @@
   }
   /**
    * Returns the path used when matching this module against rule conditions.
    * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
    */
   nameForCondition() {
@@ -824,6 +843,7 @@
   }
   /**
    * Gets side effects connection state.
    * @param {ModuleGraph} moduleGraph the module graph
    * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
    */
@@ -832,6 +852,7 @@
   }
   /**
    * Builds the module using the provided compilation context.
    * @param {WebpackOptions} options webpack options
    * @param {Compilation} compilation the compilation
    * @param {ResolverWithOptions} resolver the resolver
@@ -844,6 +865,7 @@
      const { moduleArgument, exportsArgument } =
         /** @type {BuildInfo} */
         (rootModule.buildInfo);
      /** @type {BuildInfo} */
      this.buildInfo = {
         strict: true,
         cacheable: true,
@@ -862,8 +884,7 @@
      for (const m of this._modules) {
         // populate cacheable
         if (!(/** @type {BuildInfo} */ (m.buildInfo).cacheable)) {
            /** @type {BuildInfo} */
            (this.buildInfo).cacheable = false;
            this.buildInfo.cacheable = false;
         }
         // populate dependencies
@@ -901,7 +922,7 @@
         const { assets, assetsInfo, topLevelDeclarations, needCreateRequire } =
            /** @type {BuildInfo} */ (m.buildInfo);
         const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
         const buildInfo = this.buildInfo;
         // populate topLevelDeclarations
         if (topLevelDeclarations) {
@@ -944,6 +965,7 @@
   }
   /**
    * Returns the estimated size for the requested source type.
    * @param {string=} type the source type for which the size should be estimated
    * @returns {number} the estimated size of the module (must be non-zero)
    */
@@ -987,6 +1009,11 @@
         const references = connections
            .filter((connection) => {
               if (!(connection.dependency instanceof HarmonyImportDependency)) {
                  return false;
               }
               if (
                  !Module.getSourceBasicTypes(connection.module).has(JAVASCRIPT_TYPE)
               ) {
                  return false;
               }
               return (
@@ -1199,7 +1226,7 @@
    * @param {Module} rootModule the root module of the concatenation
    * @param {Set<Module>} modules all modules in the concatenation (including the root module)
    * @param {AssociatedObjectForCache=} associatedObjectForCache object for caching
    * @param {string | HashConstructor=} hashFunction hash function to use
    * @param {HashFunction=} hashFunction hash function to use
    * @returns {string} the identifier
    */
   static _createIdentifier(
@@ -1212,6 +1239,7 @@
         /** @type {string} */ (rootModule.context),
         associatedObjectForCache
      );
      /** @type {string[]} */
      const identifiers = [];
      for (const module of modules) {
         identifiers.push(cachedMakePathsRelative(module.identifier()));
@@ -1223,6 +1251,7 @@
   }
   /**
    * Adds the provided file dependencies to the module.
    * @param {FileSystemDependencies} fileDependencies set where file dependencies are added to
    * @param {FileSystemDependencies} contextDependencies set where context dependencies are added to
    * @param {FileSystemDependencies} missingDependencies set where missing dependencies are added to
@@ -1245,6 +1274,7 @@
   }
   /**
    * Generates code and runtime requirements for this module.
    * @param {CodeGenerationContext} context context for code generation
    * @returns {CodeGenerationResult} result
    */
@@ -1254,6 +1284,7 @@
      moduleGraph,
      chunkGraph,
      runtime: generationRuntime,
      runtimes,
      codeGenerationResults
   }) {
      const { concatenatedModuleInfo } = ConcatenatedModule.getCompilationHooks(
@@ -1289,6 +1320,7 @@
            moduleGraph,
            chunkGraph,
            runtime,
            runtimes,
            /** @type {CodeGenerationResults} */
            (codeGenerationResults),
            allUsedNames
@@ -1304,6 +1336,7 @@
      const usedNamesInScopeInfo = new Map();
      // Set of already checked scopes
      /** @type {Set<Scope>} */
      const ignoredScopes = new Set();
      // get all global names
@@ -1317,14 +1350,17 @@
            // The super class expression in class scopes behaves weird
            // We get ranges of all super class expressions to make
            // renaming to work correctly
            /** @typedef {{ range: Range, variables: Variable[] }} ClassInfo */
            /** @type {WeakMap<Scope, ClassInfo[]>} */
            const superClassCache = new WeakMap();
            /**
             * @param {Scope} scope scope
             * @returns {{ range: Range, variables: Variable[] }[]} result
             * @returns {ClassInfo[]} result
             */
            const getSuperClassExpressions = (scope) => {
               const cacheEntry = superClassCache.get(scope);
               if (cacheEntry !== undefined) return cacheEntry;
               /** @type {ClassInfo[]} */
               const superClassExpressions = [];
               for (const childScope of scope.childScopes) {
                  if (childScope.type !== "class") continue;
@@ -1407,7 +1443,7 @@
       * @param {string} name the name to find a new name for
       * @param {ConcatenatedModuleInfo} info the info of the module
       * @param {Reference[]} references the references to the name
       * @returns {string|undefined} the new name or undefined if the name is not found
       * @returns {string | undefined} the new name or undefined if the name is not found
       */
      const _findNewName = (name, info, references) => {
         const { usedNames, alreadyCheckedScopes } = getUsedNamesInScopeInfo(
@@ -1441,7 +1477,7 @@
       * @param {string} name the name to find a new name for
       * @param {ConcatenatedModuleInfo} info the info of the module
       * @param {Reference[]} references the references to the name
       * @returns {string|undefined} the new name or undefined if the name is not found
       * @returns {string | undefined} the new name or undefined if the name is not found
       */
      const _findNewNameForSpecifier = (name, info, references) => {
         const { usedNames: moduleUsedNames, alreadyCheckedScopes } =
@@ -1450,6 +1486,7 @@
               info.module.identifier(),
               name
            );
         /** @type {UsedNames} */
         const referencesUsedNames = new Set();
         for (const ref of references) {
            addScopeSymbols(
@@ -1521,11 +1558,12 @@
                     topLevelDeclarations.add(name);
                  }
               }
               /** @type {string} */
               let namespaceObjectName;
               if (info.namespaceExportSymbol) {
                  namespaceObjectName = info.internalNames.get(
                     info.namespaceExportSymbol
                  );
                  namespaceObjectName =
                     /** @type {string} */
                     (info.internalNames.get(info.namespaceExportSymbol));
               } else {
                  namespaceObjectName = findNewName(
                     "namespaceObject",
@@ -1535,13 +1573,8 @@
                  );
                  allUsedNames.add(namespaceObjectName);
               }
               info.namespaceObjectName =
                  /** @type {string} */
                  (namespaceObjectName);
               topLevelDeclarations.add(
                  /** @type {string} */
                  (namespaceObjectName)
               );
               info.namespaceObjectName = namespaceObjectName;
               topLevelDeclarations.add(namespaceObjectName);
               break;
            }
            case "external": {
@@ -1624,13 +1657,15 @@
         if (info.type === "concatenated") {
            const globalScope = /** @type {Scope} */ (info.globalScope);
            // group references by name
            /** @type {Map<string, Reference[]>} */
            const referencesByName = new Map();
            for (const reference of globalScope.through) {
               const name = reference.identifier.name;
               if (!referencesByName.has(name)) {
                  referencesByName.set(name, []);
               }
               referencesByName.get(name).push(reference);
               /** @type {Reference[]} */
               (referencesByName.get(name)).push(reference);
            }
            for (const [name, references] of referencesByName) {
               const match = ConcatenationScope.matchModuleReference(name);
@@ -1763,6 +1798,7 @@
      // define exports
      if (exportsMap.size > 0) {
         /** @type {string[]} */
         const definitions = [];
         for (const [key, value] of exportsMap) {
            definitions.push(
@@ -1771,9 +1807,6 @@
               )}`
            );
         }
         const { onDemandExportsGeneration } =
            ConcatenatedModule.getCompilationHooks(this.compilation);
         runtimeRequirements.add(RuntimeGlobals.exports);
         runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
@@ -1788,21 +1821,24 @@
            );
         }
         if (onDemandExportsGeneration.call(this)) {
            /** @type {BuildMeta} */ (this.buildMeta).factoryExportsBinding =
               "\n// EXPORTS\n" +
               `${RuntimeGlobals.definePropertyGetters}(${
                  this.exportsArgument
               }, {${definitions.join(",")}\n});\n`;
            /** @type {BuildMeta} */ (this.buildMeta).exportsFinalName =
               exportsFinalName;
         } else {
            result.add("\n// EXPORTS\n");
            result.add(
               `${RuntimeGlobals.definePropertyGetters}(${
                  this.exportsArgument
               }, {${definitions.join(",")}\n});\n`
            );
         const exportsSource =
            "\n// EXPORTS\n" +
            `${RuntimeGlobals.definePropertyGetters}(${this.exportsArgument}, {${definitions.join(
               ","
            )}\n});\n`;
         const { onDemandExportsGeneration } =
            ConcatenatedModule.getCompilationHooks(this.compilation);
         if (
            !onDemandExportsGeneration.call(
               this,
               runtimes,
               exportsSource,
               exportsFinalName
            )
         ) {
            result.add(exportsSource);
         }
      }
@@ -1814,9 +1850,11 @@
      }
      // generate namespace objects
      /** @type {Map<ConcatenatedModuleInfo, string>} */
      const namespaceObjectSources = new Map();
      for (const info of neededNamespaceObjects) {
         if (info.namespaceExportSymbol) continue;
         /** @type {string[]} */
         const nsObj = [];
         const exportsInfo = moduleGraph.getExportsInfo(info.module);
         for (const exportInfo of exportsInfo.orderedExports) {
@@ -1912,6 +1950,7 @@
      // evaluate modules in order
      for (const rawInfo of modulesWithInfo) {
         /** @type {undefined | string} */
         let name;
         let isConditional = false;
         const info = rawInfo.type === "reference" ? rawInfo.target : rawInfo;
@@ -2000,6 +2039,7 @@
         }
      }
      /** @type {CodeGenerationResultData} */
      const data = new Map();
      if (chunkInitFragments.length > 0) {
         data.set("chunkInitFragments", chunkInitFragments);
@@ -2008,7 +2048,7 @@
      /** @type {CodeGenerationResult} */
      const resultEntry = {
         sources: new Map([["javascript", new CachedSource(result)]]),
         sources: new Map([[JAVASCRIPT_TYPE, new CachedSource(result)]]),
         data,
         runtimeRequirements
      };
@@ -2024,8 +2064,9 @@
    * @param {ModuleGraph} moduleGraph moduleGraph
    * @param {ChunkGraph} chunkGraph chunkGraph
    * @param {RuntimeSpec} runtime runtime
    * @param {RuntimeSpec[]} runtimes runtimes
    * @param {CodeGenerationResults} codeGenerationResults codeGenerationResults
    * @param {Set<string>} usedNames used names
    * @param {UsedNames} usedNames used names
    */
   _analyseModule(
      modulesMap,
@@ -2035,6 +2076,7 @@
      moduleGraph,
      chunkGraph,
      runtime,
      runtimes,
      codeGenerationResults,
      usedNames
   ) {
@@ -2055,13 +2097,14 @@
               moduleGraph,
               chunkGraph,
               runtime,
               runtimes,
               concatenationScope,
               codeGenerationResults,
               sourceTypes: JS_TYPES
               sourceTypes: JAVASCRIPT_TYPES
            });
            const source =
               /** @type {Source} */
               (codeGenResult.sources.get("javascript"));
               (codeGenResult.sources.get(JAVASCRIPT_TYPE));
            const data = codeGenResult.data;
            const chunkInitFragments = data && data.get("chunkInitFragments");
            const code = source.source().toString();
@@ -2211,6 +2254,7 @@
   }
   /**
    * Updates the hash with the data contributed by this instance.
    * @param {Hash} hash the hash used to track dependencies
    * @param {UpdateHashContext} context context
    * @returns {void}