| | |
| | | |
| | | "use strict"; |
| | | |
| | | const { SyncBailHook } = require("tapable"); |
| | | const { OriginalSource, RawSource } = require("webpack-sources"); |
| | | const ConcatenationScope = require("./ConcatenationScope"); |
| | | const EnvironmentNotSupportAsyncWarning = require("./EnvironmentNotSupportAsyncWarning"); |
| | |
| | | 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"); |
| | |
| | | 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 */ |
| | |
| | | /** @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 */ |
| | |
| | | /** @typedef {ImportDependencyMeta | CssImportDependencyMeta | AssetDependencyMeta} DependencyMeta */ |
| | | |
| | | /** |
| | | * Defines the source data type used by this module. |
| | | * @typedef {object} SourceData |
| | | * @property {boolean=} iife |
| | | * @property {string=} init |
| | |
| | | |
| | | /** @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 |
| | |
| | | /** @typedef {string | string[]} ModuleAndSpecifiers */ |
| | | |
| | | /** |
| | | * Gets source for common js external. |
| | | * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request |
| | | * @returns {SourceData} the generated source |
| | | */ |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Gets external module node commonjs init fragment. |
| | | * @param {RuntimeTemplate} runtimeTemplate the runtime template |
| | | * @returns {InitFragment<ChunkRenderContext>} code |
| | | */ |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Gets source for import external. |
| | | * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request |
| | | * @param {RuntimeTemplate} runtimeTemplate the runtime template |
| | | * @param {ImportDependencyMeta=} dependencyMeta the dependency meta |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Import assertion replacer. |
| | | * @param {string} key key |
| | | * @param {ImportAttributes | string | boolean | undefined} value value |
| | | * @returns {ImportAttributes | string | boolean | undefined} replaced value |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns imported. |
| | | * @returns {Imported} imported |
| | | */ |
| | | getImported() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Updates imported using the provided imported. |
| | | * @param {Imported} imported imported |
| | | */ |
| | | setImported(imported) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns identifier. |
| | | * @param {string} ident ident |
| | | * @returns {string} identifier |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns normalized imported. |
| | | * @param {Imported} imported imported |
| | | * @returns {Imported} normalized imported |
| | | */ |
| | |
| | | ); |
| | | |
| | | /** |
| | | * Generates module remapping. |
| | | * @param {string} input input |
| | | * @param {ExportsInfo} exportsInfo the exports info |
| | | * @param {RuntimeSpec=} runtime the runtime |
| | |
| | | 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); |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Gets source for module external. |
| | | * @param {ModuleAndSpecifiers} moduleAndSpecifiers the module request |
| | | * @param {ExportsInfo} exportsInfo exports info of this module |
| | | * @param {RuntimeSpec} runtime the runtime |
| | |
| | | let expression = baseAccess; |
| | | |
| | | const useNamespace = imported === true; |
| | | /** @type {undefined | string} */ |
| | | let moduleRemapping; |
| | | if (useNamespace) { |
| | | moduleRemapping = generateModuleRemapping( |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Gets source for script external. |
| | | * @param {string | string[]} urlAndGlobal the script request |
| | | * @param {RuntimeTemplate} runtimeTemplate the runtime template |
| | | * @returns {SourceData} the generated source |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Checks external variable. |
| | | * @param {string} variableName the variable name to check |
| | | * @param {string} request the request path |
| | | * @param {RuntimeTemplate} runtimeTemplate the runtime template |
| | |
| | | )} }\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 |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | /** @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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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() { |
| | |
| | | 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 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns a human-readable identifier for this module. |
| | | * @param {RequestShortener} requestShortener the request shortener |
| | | * @returns {string} a user readable identifier of the module |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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} |
| | |
| | | } |
| | | |
| | | /** |
| | | * Builds the module using the provided compilation context. |
| | | * @param {WebpackOptions} options webpack options |
| | | * @param {Compilation} compilation the compilation |
| | | * @param {ResolverWithOptions} resolver the resolver |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Get request and external type. |
| | | * @private |
| | | * @returns {{ request: string | string[], externalType: ExternalsType }} the request and external type |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the source data. |
| | | * @private |
| | | * @param {string | string[]} request request |
| | | * @param {ExternalsType} externalType the external type |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates code and runtime requirements for this module. |
| | | * @param {CodeGenerationContext} context context for code generation |
| | | * @returns {CodeGenerationResult} result |
| | | */ |
| | |
| | | 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 |
| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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) |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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} |
| | |
| | | } |
| | | |
| | | /** |
| | | * Serializes this instance into the provided serializer context. |
| | | * @param {ObjectSerializerContext} context context |
| | | */ |
| | | serialize(context) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Restores this instance from the provided deserializer context. |
| | | * @param {ObjectDeserializerContext} context context |
| | | */ |
| | | deserialize(context) { |