WXL
3 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
node_modules/webpack/lib/ExternalModule.js
@@ -5,6 +5,7 @@
"use strict";
const { SyncBailHook } = require("tapable");
const { OriginalSource, RawSource } = require("webpack-sources");
const ConcatenationScope = require("./ConcatenationScope");
const EnvironmentNotSupportAsyncWarning = require("./EnvironmentNotSupportAsyncWarning");
@@ -14,8 +15,9 @@
const {
   CSS_IMPORT_TYPES,
   CSS_URL_TYPES,
   JS_TYPES
} = require("./ModuleSourceTypesConstants");
   JAVASCRIPT_TYPE,
   JAVASCRIPT_TYPES
} = require("./ModuleSourceTypeConstants");
const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
const RuntimeGlobals = require("./RuntimeGlobals");
const Template = require("./Template");
@@ -24,7 +26,7 @@
const createHash = require("./util/createHash");
const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
const makeSerializable = require("./util/makeSerializable");
const propertyAccess = require("./util/propertyAccess");
const { propertyAccess } = require("./util/property");
const { register } = require("./util/serialization");
/** @typedef {import("webpack-sources").Source} Source */
@@ -44,12 +46,15 @@
/** @typedef {import("./Module").BuildInfo} BuildInfo */
/** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("./Module").CodeGenerationResultData} CodeGenerationResultData */
/** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("./Module").LibIdent} LibIdent */
/** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
/** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
/** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
/** @typedef {import("./Module").Sources} Sources */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
/** @typedef {import("./RequestShortener")} RequestShortener */
@@ -70,6 +75,7 @@
/** @typedef {ImportDependencyMeta | CssImportDependencyMeta | AssetDependencyMeta} DependencyMeta */
/**
 * Defines the source data type used by this module.
 * @typedef {object} SourceData
 * @property {boolean=} iife
 * @property {string=} init
@@ -81,14 +87,19 @@
/** @typedef {true | [string, string][]} Imported */
/** @type {RuntimeRequirements} */
const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
/** @type {RuntimeRequirements} */
const RUNTIME_REQUIREMENTS_FOR_SCRIPT = new Set([RuntimeGlobals.loadScript]);
/** @type {RuntimeRequirements} */
const RUNTIME_REQUIREMENTS_FOR_MODULE = new Set([
   RuntimeGlobals.definePropertyGetters
]);
const EMPTY_RUNTIME_REQUIREMENTS = new Set([]);
/** @type {RuntimeRequirements} */
const EMPTY_RUNTIME_REQUIREMENTS = new Set();
/**
 * Gets source for global variable external.
 * @param {string | string[]} variableName the variable name or path
 * @param {string} type the module system
 * @returns {SourceData} the generated source
@@ -112,6 +123,7 @@
/** @typedef {string | string[]} ModuleAndSpecifiers */
/**
 * Gets source for common js external.
 * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request
 * @returns {SourceData} the generated source
 */
@@ -131,6 +143,7 @@
};
/**
 * Gets external module node commonjs init fragment.
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
 * @returns {InitFragment<ChunkRenderContext>} code
 */
@@ -148,6 +161,7 @@
};
/**
 * Gets source for common js external in node module.
 * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
 * @returns {SourceData} the generated source
@@ -177,6 +191,7 @@
};
/**
 * Gets source for import external.
 * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
 * @param {ImportDependencyMeta=} dependencyMeta the dependency meta
@@ -231,6 +246,7 @@
};
/**
 * Import assertion replacer.
 * @param {string} key key
 * @param {ImportAttributes | string | boolean | undefined} value value
 * @returns {ImportAttributes | string | boolean | undefined} replaced value
@@ -244,10 +260,12 @@
};
/**
 * Represents ModuleExternalInitFragment.
 * @extends {InitFragment<GenerateContext>}
 */
class ModuleExternalInitFragment extends InitFragment {
   /**
    * Creates an instance of ModuleExternalInitFragment.
    * @param {string} request import source
    * @param {Imported} imported the imported specifiers
    * @param {string=} ident recomputed ident
@@ -285,6 +303,7 @@
   }
   /**
    * Returns imported.
    * @returns {Imported} imported
    */
   getImported() {
@@ -292,6 +311,7 @@
   }
   /**
    * Updates imported using the provided imported.
    * @param {Imported} imported imported
    */
   setImported(imported) {
@@ -299,6 +319,7 @@
   }
   /**
    * Returns the source code that will be included as initialization code.
    * @param {GenerateContext} context context
    * @returns {string | Source | undefined} the source code that will be included as initialization code
    */
@@ -346,6 +367,7 @@
   }
   /**
    * Returns identifier.
    * @param {string} ident ident
    * @returns {string} identifier
    */
@@ -354,6 +376,7 @@
   }
   /**
    * Returns normalized imported.
    * @param {Imported} imported imported
    * @returns {Imported} normalized imported
    */
@@ -386,6 +409,7 @@
);
/**
 * Generates module remapping.
 * @param {string} input input
 * @param {ExportsInfo} exportsInfo the exports info
 * @param {RuntimeSpec=} runtime the runtime
@@ -399,6 +423,7 @@
   runtimeTemplate
) => {
   if (exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused) {
      /** @type {string[]} */
      const properties = [];
      for (const exportInfo of exportsInfo.orderedExports) {
         const used = exportInfo.getUsedName(exportInfo.name, runtime);
@@ -427,6 +452,7 @@
};
/**
 * Gets source for module external.
 * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request
 * @param {ExportsInfo} exportsInfo exports info of this module
 * @param {RuntimeSpec} runtime the runtime
@@ -487,6 +513,7 @@
   let expression = baseAccess;
   const useNamespace = imported === true;
   /** @type {undefined | string} */
   let moduleRemapping;
   if (useNamespace) {
      moduleRemapping = generateModuleRemapping(
@@ -519,6 +546,7 @@
};
/**
 * Gets source for script external.
 * @param {string | string[]} urlAndGlobal the script request
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
 * @returns {SourceData} the generated source
@@ -556,6 +584,7 @@
};
/**
 * Checks external variable.
 * @param {string} variableName the variable name to check
 * @param {string} request the request path
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
@@ -567,6 +596,7 @@
   )} }\n`;
/**
 * Gets source for amd or umd external.
 * @param {ModuleId | string} id the module id
 * @param {boolean} optional true, if the module is optional
 * @param {string | string[]} request the request path
@@ -595,6 +625,7 @@
};
/**
 * Gets source for default case.
 * @param {boolean} optional true, if the module is optional
 * @param {string | string[]} request the request path
 * @param {RuntimeTemplate} runtimeTemplate the runtime template
@@ -619,8 +650,18 @@
/** @typedef {Record<string, string | string[]>} RequestRecord */
/** @typedef {string | string[] | RequestRecord} ExternalModuleRequest */
/**
 * Defines the external module hooks type used by this module.
 * @typedef {object} ExternalModuleHooks
 * @property {SyncBailHook<[Chunk, Compilation], boolean>} chunkCondition
 */
/** @type {WeakMap<Compilation, ExternalModuleHooks>} */
const compilationHooksMap = new WeakMap();
class ExternalModule extends Module {
   /**
    * Creates an instance of ExternalModule.
    * @param {ExternalModuleRequest} request request
    * @param {ExternalsType} type type
    * @param {string} userRequest user request
@@ -641,6 +682,23 @@
   }
   /**
    * Returns the attached hooks.
    * @param {Compilation} compilation the compilation
    * @returns {ExternalModuleHooks} the attached hooks
    */
   static getCompilationHooks(compilation) {
      let hooks = compilationHooksMap.get(compilation);
      if (hooks === undefined) {
         hooks = {
            chunkCondition: new SyncBailHook(["chunk", "compilation"])
         };
         compilationHooksMap.set(compilation, hooks);
      }
      return hooks;
   }
   /**
    * Returns the source types this module can generate.
    * @returns {SourceTypes} types available (do not mutate)
    */
   getSourceTypes() {
@@ -655,10 +713,11 @@
         return CSS_IMPORT_TYPES;
      }
      return JS_TYPES;
      return JAVASCRIPT_TYPES;
   }
   /**
    * Gets the library identifier.
    * @param {LibIdentOptions} options options
    * @returns {LibIdent | null} an identifier for library inclusion
    */
@@ -667,17 +726,28 @@
   }
   /**
    * Returns true if the module can be placed in the chunk.
    * @param {Chunk} chunk the chunk which condition should be checked
    * @param {Compilation} compilation the compilation
    * @returns {boolean} true, if the chunk is ok for the module
    * @returns {boolean} true if the module can be placed in the chunk
    */
   chunkCondition(chunk, { chunkGraph }) {
      return this.externalType === "css-import"
         ? true
         : chunkGraph.getNumberOfEntryModules(chunk) > 0;
   chunkCondition(chunk, compilation) {
      const { chunkCondition } = ExternalModule.getCompilationHooks(compilation);
      const condition = chunkCondition.call(chunk, compilation);
      if (condition !== undefined) return condition;
      const type = this._resolveExternalType(this.externalType);
      // For `import()` externals, keep them in the initial chunk to avoid loading
      // them asynchronously twice and to improve runtime performance.
      if (["css-import", "module"].includes(type)) {
         return true;
      }
      return compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0;
   }
   /**
    * Returns the unique identifier used to reference this module.
    * @returns {string} a unique identifier of the module
    */
   identifier() {
@@ -685,6 +755,7 @@
   }
   /**
    * Returns a human-readable identifier for this module.
    * @param {RequestShortener} requestShortener the request shortener
    * @returns {string} a user readable identifier of the module
    */
@@ -693,6 +764,7 @@
   }
   /**
    * Checks whether the module needs to be rebuilt for the current build state.
    * @param {NeedBuildContext} context context info
    * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
    * @returns {void}
@@ -702,6 +774,7 @@
   }
   /**
    * Builds the module using the provided compilation context.
    * @param {WebpackOptions} options webpack options
    * @param {Compilation} compilation the compilation
    * @param {ResolverWithOptions} resolver the resolver
@@ -795,6 +868,7 @@
   }
   /**
    * Returns the reason this module cannot be concatenated, when one exists.
    * @param {ConcatenationBailoutReasonContext} context context
    * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
    */
@@ -812,6 +886,7 @@
   }
   /**
    * Get request and external type.
    * @private
    * @returns {{ request: string | string[], externalType: ExternalsType }} the request and external type
    */
@@ -858,6 +933,7 @@
   }
   /**
    * Returns the source data.
    * @private
    * @param {string | string[]} request request
    * @param {ExternalsType} externalType the external type
@@ -964,6 +1040,7 @@
   }
   /**
    * Generates code and runtime requirements for this module.
    * @param {CodeGenerationContext} context context for code generation
    * @returns {CodeGenerationResult} result
    */
@@ -977,22 +1054,27 @@
      const { request, externalType } = this._getRequestAndExternalType();
      switch (externalType) {
         case "asset": {
            /** @type {Sources} */
            const sources = new Map();
            sources.set(
               "javascript",
               JAVASCRIPT_TYPE,
               new RawSource(`module.exports = ${JSON.stringify(request)};`)
            );
            /** @type {CodeGenerationResultData} */
            const data = new Map();
            data.set("url", { javascript: request });
            data.set("url", { javascript: /** @type {string} */ (request) });
            return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS, data };
         }
         case "css-url": {
            /** @type {Sources} */
            const sources = new Map();
            /** @type {CodeGenerationResultData} */
            const data = new Map();
            data.set("url", { "css-url": request });
            data.set("url", { "css-url": /** @type {string} */ (request) });
            return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS, data };
         }
         case "css-import": {
            /** @type {Sources} */
            const sources = new Map();
            const dependencyMeta = /** @type {CssImportDependencyMeta} */ (
               this.dependencyMeta
@@ -1055,20 +1137,22 @@
               sourceString = `${sourceData.init}\n${sourceString}`;
            }
            /** @type {undefined | CodeGenerationResultData} */
            let data;
            if (sourceData.chunkInitFragments) {
               data = new Map();
               data.set("chunkInitFragments", sourceData.chunkInitFragments);
            }
            /** @type {Sources} */
            const sources = new Map();
            if (this.useSourceMap || this.useSimpleSourceMap) {
               sources.set(
                  "javascript",
                  JAVASCRIPT_TYPE,
                  new OriginalSource(sourceString, this.identifier())
               );
            } else {
               sources.set("javascript", new RawSource(sourceString));
               sources.set(JAVASCRIPT_TYPE, new RawSource(sourceString));
            }
            let runtimeRequirements = sourceData.runtimeRequirements;
@@ -1093,6 +1177,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)
    */
@@ -1101,6 +1186,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}
@@ -1116,6 +1202,7 @@
   }
   /**
    * Serializes this instance into the provided serializer context.
    * @param {ObjectSerializerContext} context context
    */
   serialize(context) {
@@ -1130,6 +1217,7 @@
   }
   /**
    * Restores this instance from the provided deserializer context.
    * @param {ObjectDeserializerContext} context context
    */
   deserialize(context) {