2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-18 17:12:09 +08:00
|
|
|
"use strict";
|
2018-06-26 17:23:22 +08:00
|
|
|
|
2018-03-22 19:05:58 +08:00
|
|
|
const { OriginalSource, RawSource } = require("webpack-sources");
|
2017-02-18 17:12:09 +08:00
|
|
|
const AsyncDependenciesBlock = require("./AsyncDependenciesBlock");
|
2020-05-12 21:58:14 +08:00
|
|
|
const { makeWebpackError } = require("./HookWebpackError");
|
2018-07-30 23:08:51 +08:00
|
|
|
const Module = require("./Module");
|
2024-11-01 04:19:07 +08:00
|
|
|
const { JS_TYPES } = require("./ModuleSourceTypesConstants");
|
2023-04-01 01:56:32 +08:00
|
|
|
const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
|
2018-11-05 21:36:15 +08:00
|
|
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
2017-05-05 00:37:25 +08:00
|
|
|
const Template = require("./Template");
|
2018-09-12 00:47:55 +08:00
|
|
|
const WebpackError = require("./WebpackError");
|
2018-10-18 15:20:59 +08:00
|
|
|
const {
|
2018-12-20 15:51:54 +08:00
|
|
|
compareLocations,
|
2025-07-03 17:06:45 +08:00
|
|
|
compareModulesById,
|
2018-10-18 15:20:59 +08:00
|
|
|
compareSelect,
|
2025-07-03 17:06:45 +08:00
|
|
|
concatComparators,
|
|
|
|
keepOriginalOrder
|
2018-10-18 15:20:59 +08:00
|
|
|
} = require("./util/comparators");
|
2022-02-18 01:08:27 +08:00
|
|
|
const {
|
|
|
|
contextify,
|
2025-07-03 17:06:45 +08:00
|
|
|
makePathsRelative,
|
|
|
|
parseResource
|
2022-02-18 01:08:27 +08:00
|
|
|
} = require("./util/identifier");
|
2019-09-04 19:34:34 +08:00
|
|
|
const makeSerializable = require("./util/makeSerializable");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2025-04-16 23:04:35 +08:00
|
|
|
/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
|
2025-08-20 18:50:12 +08:00
|
|
|
/** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @typedef {import("./Chunk")} Chunk */
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @typedef {import("./Chunk").ChunkId} ChunkId */
|
2025-09-11 08:10:10 +08:00
|
|
|
/** @typedef {import("./Chunk").ChunkName} ChunkName */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("./ChunkGraph")} ChunkGraph */
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {import("./ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
2018-07-25 18:12:17 +08:00
|
|
|
/** @typedef {import("./Compilation")} Compilation */
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @typedef {import("./Dependency")} Dependency */
|
2025-09-11 08:10:10 +08:00
|
|
|
/** @typedef {import("./Dependency").RawReferencedExports} RawReferencedExports */
|
2024-11-01 04:19:07 +08:00
|
|
|
/** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
2025-03-12 09:56:14 +08:00
|
|
|
/** @typedef {import("./Module").BuildCallback} BuildCallback */
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @typedef {import("./Module").BuildInfo} BuildInfo */
|
2025-08-28 18:34:30 +08:00
|
|
|
/** @typedef {import("./Module").FileSystemDependencies} FileSystemDependencies */
|
2020-07-08 16:58:20 +08:00
|
|
|
/** @typedef {import("./Module").BuildMeta} BuildMeta */
|
2019-10-09 04:29:46 +08:00
|
|
|
/** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
|
|
|
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
2018-07-20 22:24:35 +08:00
|
|
|
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
|
2025-09-11 08:10:10 +08:00
|
|
|
/** @typedef {import("./Module").LibIdent} LibIdent */
|
2025-03-12 09:56:14 +08:00
|
|
|
/** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
|
2018-09-26 15:14:44 +08:00
|
|
|
/** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./RequestShortener")} RequestShortener */
|
2019-11-11 22:25:03 +08:00
|
|
|
/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
2018-07-25 18:12:17 +08:00
|
|
|
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("./dependencies/ContextElementDependency")} ContextElementDependency */
|
2024-06-11 00:21:03 +08:00
|
|
|
/** @typedef {import("./javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
2023-04-12 02:57:43 +08:00
|
|
|
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
2019-11-11 22:25:03 +08:00
|
|
|
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
2018-07-25 18:12:17 +08:00
|
|
|
|
|
|
|
/** @typedef {"sync" | "eager" | "weak" | "async-weak" | "lazy" | "lazy-once"} ContextMode Context mode */
|
2018-06-08 19:20:57 +08:00
|
|
|
|
2018-11-07 21:03:25 +08:00
|
|
|
/**
|
2024-06-11 21:09:50 +08:00
|
|
|
* @typedef {object} ContextOptions
|
2018-11-07 21:03:25 +08:00
|
|
|
* @property {ContextMode} mode
|
|
|
|
* @property {boolean} recursive
|
2025-08-22 00:14:29 +08:00
|
|
|
* @property {RegExp | false | null} regExp
|
|
|
|
* @property {"strict" | boolean=} namespaceObject
|
2018-11-07 21:03:25 +08:00
|
|
|
* @property {string=} addon
|
2025-09-11 08:10:10 +08:00
|
|
|
* @property {ChunkName=} chunkName
|
2025-08-22 00:14:29 +08:00
|
|
|
* @property {RegExp | null=} include
|
|
|
|
* @property {RegExp | null=} exclude
|
2019-06-14 17:44:54 +08:00
|
|
|
* @property {RawChunkGroupOptions=} groupOptions
|
2021-05-31 19:44:09 +08:00
|
|
|
* @property {string=} typePrefix
|
2020-06-18 06:21:22 +08:00
|
|
|
* @property {string=} category
|
2025-09-11 08:10:10 +08:00
|
|
|
* @property {RawReferencedExports | null=} referencedExports exports referenced from modules (won't be mangled)
|
2025-08-22 00:14:29 +08:00
|
|
|
* @property {string | null=} layer
|
2024-06-11 00:21:03 +08:00
|
|
|
* @property {ImportAttributes=} attributes
|
2019-06-14 17:44:54 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2024-06-11 21:09:50 +08:00
|
|
|
* @typedef {object} ContextModuleOptionsExtras
|
2025-04-26 01:43:01 +08:00
|
|
|
* @property {false | string | string[]} resource
|
2019-06-14 17:44:54 +08:00
|
|
|
* @property {string=} resourceQuery
|
2020-07-03 23:03:15 +08:00
|
|
|
* @property {string=} resourceFragment
|
2025-04-16 23:04:35 +08:00
|
|
|
* @property {ResolveOptions=} resolveOptions
|
2018-11-07 21:03:25 +08:00
|
|
|
*/
|
|
|
|
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {ContextOptions & ContextModuleOptionsExtras} ContextModuleOptions */
|
|
|
|
|
2018-07-10 16:23:28 +08:00
|
|
|
/**
|
|
|
|
* @callback ResolveDependenciesCallback
|
2024-08-07 23:22:25 +08:00
|
|
|
* @param {Error | null} err
|
2020-06-10 19:31:01 +08:00
|
|
|
* @param {ContextElementDependency[]=} dependencies
|
2025-09-09 23:41:52 +08:00
|
|
|
* @returns {void}
|
2018-07-10 16:23:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback ResolveDependencies
|
2020-06-10 19:31:01 +08:00
|
|
|
* @param {InputFileSystem} fs
|
|
|
|
* @param {ContextModuleOptions} options
|
2018-07-10 16:23:28 +08:00
|
|
|
* @param {ResolveDependenciesCallback} callback
|
|
|
|
*/
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @typedef {1 | 3 | 7 | 9} FakeMapType */
|
|
|
|
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @typedef {Record<ModuleId, FakeMapType>} FakeMap */
|
2024-03-26 00:05:56 +08:00
|
|
|
|
2017-02-18 17:12:09 +08:00
|
|
|
class ContextModule extends Module {
|
2018-07-10 16:23:28 +08:00
|
|
|
/**
|
|
|
|
* @param {ResolveDependencies} resolveDependencies function to get dependencies in this context
|
2018-11-07 21:03:25 +08:00
|
|
|
* @param {ContextModuleOptions} options options object
|
2018-07-10 16:23:28 +08:00
|
|
|
*/
|
2017-10-14 05:31:15 +08:00
|
|
|
constructor(resolveDependencies, options) {
|
2022-02-21 16:58:44 +08:00
|
|
|
if (!options || typeof options.resource === "string") {
|
|
|
|
const parsed = parseResource(
|
|
|
|
options ? /** @type {string} */ (options.resource) : ""
|
|
|
|
);
|
|
|
|
const resource = parsed.path;
|
|
|
|
const resourceQuery = (options && options.resourceQuery) || parsed.query;
|
|
|
|
const resourceFragment =
|
|
|
|
(options && options.resourceFragment) || parsed.fragment;
|
2023-06-03 21:38:37 +08:00
|
|
|
const layer = options && options.layer;
|
2022-02-21 16:58:44 +08:00
|
|
|
|
2023-06-03 21:38:37 +08:00
|
|
|
super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, resource, layer);
|
2022-02-21 16:58:44 +08:00
|
|
|
/** @type {ContextModuleOptions} */
|
|
|
|
this.options = {
|
|
|
|
...options,
|
|
|
|
resource,
|
|
|
|
resourceQuery,
|
|
|
|
resourceFragment
|
|
|
|
};
|
|
|
|
} else {
|
2023-06-03 21:38:37 +08:00
|
|
|
super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, undefined, options.layer);
|
2022-02-21 16:58:44 +08:00
|
|
|
/** @type {ContextModuleOptions} */
|
|
|
|
this.options = {
|
|
|
|
...options,
|
|
|
|
resource: options.resource,
|
|
|
|
resourceQuery: options.resourceQuery || "",
|
|
|
|
resourceFragment: options.resourceFragment || ""
|
|
|
|
};
|
|
|
|
}
|
2018-01-31 04:40:44 +08:00
|
|
|
|
|
|
|
// Info from Factory
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @type {ResolveDependencies | undefined} */
|
2018-01-31 04:40:44 +08:00
|
|
|
this.resolveDependencies = resolveDependencies;
|
2020-07-17 16:27:48 +08:00
|
|
|
if (options && options.resolveOptions !== undefined) {
|
2017-11-17 21:26:23 +08:00
|
|
|
this.resolveOptions = options.resolveOptions;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2017-11-06 20:02:35 +08:00
|
|
|
|
2020-07-17 16:27:48 +08:00
|
|
|
if (options && typeof options.mode !== "string") {
|
2017-10-16 20:49:51 +08:00
|
|
|
throw new Error("options.mode is a required option");
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-03-13 08:17:49 +08:00
|
|
|
|
2018-03-25 02:02:30 +08:00
|
|
|
this._identifier = this._createIdentifier();
|
2018-09-12 00:47:55 +08:00
|
|
|
this._forceBuild = true;
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2019-09-26 21:51:40 +08:00
|
|
|
/**
|
2024-02-17 01:39:12 +08:00
|
|
|
* @returns {SourceTypes} types available (do not mutate)
|
2019-09-26 21:51:40 +08:00
|
|
|
*/
|
|
|
|
getSourceTypes() {
|
2024-11-01 04:19:07 +08:00
|
|
|
return JS_TYPES;
|
2019-09-26 21:51:40 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* Assuming this module is in the cache. Update the (cached) module with
|
|
|
|
* the fresh module from the factory. Usually updates internal references
|
|
|
|
* and properties.
|
|
|
|
* @param {Module} module fresh module
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-03-28 22:19:15 +08:00
|
|
|
updateCacheModule(module) {
|
2018-07-25 18:12:17 +08:00
|
|
|
const m = /** @type {ContextModule} */ (module);
|
|
|
|
this.resolveDependencies = m.resolveDependencies;
|
|
|
|
this.options = m.options;
|
2021-03-15 17:29:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assuming this module is in the cache. Remove internal references to allow freeing some memory.
|
|
|
|
*/
|
|
|
|
cleanupForCache() {
|
|
|
|
super.cleanupForCache();
|
|
|
|
this.resolveDependencies = undefined;
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
|
|
|
* @private
|
2024-07-10 12:45:14 +08:00
|
|
|
* @param {RegExp} regexString RegExp as a string
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {boolean=} stripSlash do we need to strip a slsh
|
|
|
|
* @returns {string} pretty RegExp
|
|
|
|
*/
|
2024-07-10 12:45:14 +08:00
|
|
|
_prettyRegExp(regexString, stripSlash = true) {
|
|
|
|
const str = stripSlash
|
|
|
|
? regexString.source + regexString.flags
|
2024-07-31 10:39:30 +08:00
|
|
|
: `${regexString}`;
|
2024-07-10 12:45:14 +08:00
|
|
|
return str.replace(/!/g, "%21").replace(/\|/g, "%7C");
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-03-25 02:02:30 +08:00
|
|
|
_createIdentifier() {
|
2022-02-21 16:58:44 +08:00
|
|
|
let identifier =
|
|
|
|
this.context ||
|
2022-02-21 18:25:30 +08:00
|
|
|
(typeof this.options.resource === "string" ||
|
|
|
|
this.options.resource === false
|
|
|
|
? `${this.options.resource}`
|
2022-02-21 16:58:44 +08:00
|
|
|
: this.options.resource.join("|"));
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.resourceQuery) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += `|${this.options.resourceQuery}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-07-03 23:03:15 +08:00
|
|
|
if (this.options.resourceFragment) {
|
|
|
|
identifier += `|${this.options.resourceFragment}`;
|
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.mode) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += `|${this.options.mode}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (!this.options.recursive) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += "|nonrecursive";
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.addon) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += `|${this.options.addon}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.regExp) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += `|${this._prettyRegExp(this.options.regExp, false)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.include) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += `|include: ${this._prettyRegExp(
|
2022-01-11 07:15:21 +08:00
|
|
|
this.options.include,
|
|
|
|
false
|
|
|
|
)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.exclude) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += `|exclude: ${this._prettyRegExp(
|
2022-01-11 07:15:21 +08:00
|
|
|
this.options.exclude,
|
|
|
|
false
|
|
|
|
)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-06-10 19:31:01 +08:00
|
|
|
if (this.options.referencedExports) {
|
|
|
|
identifier += `|referencedExports: ${JSON.stringify(
|
|
|
|
this.options.referencedExports
|
|
|
|
)}`;
|
|
|
|
}
|
2019-09-04 19:34:34 +08:00
|
|
|
if (this.options.chunkName) {
|
|
|
|
identifier += `|chunkName: ${this.options.chunkName}`;
|
|
|
|
}
|
2018-04-16 16:27:22 +08:00
|
|
|
if (this.options.groupOptions) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += `|groupOptions: ${JSON.stringify(
|
2018-04-16 16:27:22 +08:00
|
|
|
this.options.groupOptions
|
|
|
|
)}`;
|
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.namespaceObject === "strict") {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += "|strict namespace object";
|
2018-05-29 20:50:40 +08:00
|
|
|
} else if (this.options.namespaceObject) {
|
2018-12-08 01:12:04 +08:00
|
|
|
identifier += "|namespace object";
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2023-06-16 01:56:55 +08:00
|
|
|
if (this.layer) {
|
|
|
|
identifier += `|layer: ${this.layer}`;
|
2023-06-16 00:48:55 +08:00
|
|
|
}
|
2017-02-18 17:29:21 +08:00
|
|
|
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* @returns {string} a unique identifier of the module
|
|
|
|
*/
|
2018-03-25 02:02:30 +08:00
|
|
|
identifier() {
|
|
|
|
return this._identifier;
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
|
|
|
* @param {RequestShortener} requestShortener the request shortener
|
|
|
|
* @returns {string} a user readable identifier of the module
|
|
|
|
*/
|
2017-02-18 17:29:21 +08:00
|
|
|
readableIdentifier(requestShortener) {
|
2022-02-21 16:58:44 +08:00
|
|
|
let identifier;
|
|
|
|
if (this.context) {
|
2024-07-31 10:39:30 +08:00
|
|
|
identifier = `${requestShortener.shorten(this.context)}/`;
|
2022-02-21 18:25:30 +08:00
|
|
|
} else if (
|
|
|
|
typeof this.options.resource === "string" ||
|
|
|
|
this.options.resource === false
|
|
|
|
) {
|
2024-07-31 10:39:30 +08:00
|
|
|
identifier = `${requestShortener.shorten(`${this.options.resource}`)}/`;
|
2022-02-21 16:58:44 +08:00
|
|
|
} else {
|
|
|
|
identifier = this.options.resource
|
2025-07-17 00:13:14 +08:00
|
|
|
.map((r) => `${requestShortener.shorten(r)}/`)
|
2022-02-21 16:58:44 +08:00
|
|
|
.join(" ");
|
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.resourceQuery) {
|
2017-12-01 19:47:14 +08:00
|
|
|
identifier += ` ${this.options.resourceQuery}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.mode) {
|
|
|
|
identifier += ` ${this.options.mode}`;
|
|
|
|
}
|
|
|
|
if (!this.options.recursive) {
|
|
|
|
identifier += " nonrecursive";
|
|
|
|
}
|
|
|
|
if (this.options.addon) {
|
2017-10-14 05:31:15 +08:00
|
|
|
identifier += ` ${requestShortener.shorten(this.options.addon)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.regExp) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.include) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.exclude) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-06-10 19:31:01 +08:00
|
|
|
if (this.options.referencedExports) {
|
|
|
|
identifier += ` referencedExports: ${this.options.referencedExports
|
2025-07-17 00:13:14 +08:00
|
|
|
.map((e) => e.join("."))
|
2020-06-10 19:31:01 +08:00
|
|
|
.join(", ")}`;
|
|
|
|
}
|
2019-09-04 19:34:34 +08:00
|
|
|
if (this.options.chunkName) {
|
|
|
|
identifier += ` chunkName: ${this.options.chunkName}`;
|
|
|
|
}
|
2018-04-16 16:27:22 +08:00
|
|
|
if (this.options.groupOptions) {
|
|
|
|
const groupOptions = this.options.groupOptions;
|
2018-05-29 20:50:40 +08:00
|
|
|
for (const key of Object.keys(groupOptions)) {
|
2024-03-26 00:05:56 +08:00
|
|
|
identifier += ` ${key}: ${
|
|
|
|
groupOptions[/** @type {keyof RawChunkGroupOptions} */ (key)]
|
|
|
|
}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-04-16 16:27:22 +08:00
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.namespaceObject === "strict") {
|
2017-12-04 19:07:43 +08:00
|
|
|
identifier += " strict namespace object";
|
2018-05-29 20:50:40 +08:00
|
|
|
} else if (this.options.namespaceObject) {
|
|
|
|
identifier += " namespace object";
|
|
|
|
}
|
2017-02-18 17:29:21 +08:00
|
|
|
|
|
|
|
return identifier;
|
|
|
|
}
|
|
|
|
|
2018-07-20 22:24:35 +08:00
|
|
|
/**
|
|
|
|
* @param {LibIdentOptions} options options
|
2025-09-11 08:10:10 +08:00
|
|
|
* @returns {LibIdent | null} an identifier for library inclusion
|
2018-07-20 22:24:35 +08:00
|
|
|
*/
|
2017-02-18 17:12:09 +08:00
|
|
|
libIdent(options) {
|
2022-02-21 16:58:44 +08:00
|
|
|
let identifier;
|
|
|
|
|
|
|
|
if (this.context) {
|
|
|
|
identifier = contextify(
|
|
|
|
options.context,
|
|
|
|
this.context,
|
|
|
|
options.associatedObjectForCache
|
|
|
|
);
|
|
|
|
} else if (typeof this.options.resource === "string") {
|
|
|
|
identifier = contextify(
|
|
|
|
options.context,
|
|
|
|
this.options.resource,
|
|
|
|
options.associatedObjectForCache
|
|
|
|
);
|
2022-02-21 18:25:30 +08:00
|
|
|
} else if (this.options.resource === false) {
|
|
|
|
identifier = "false";
|
2022-02-21 16:58:44 +08:00
|
|
|
} else {
|
2022-02-28 20:22:22 +08:00
|
|
|
identifier = this.options.resource
|
2025-07-17 00:13:14 +08:00
|
|
|
.map((res) =>
|
2022-02-21 16:58:44 +08:00
|
|
|
contextify(options.context, res, options.associatedObjectForCache)
|
2022-02-28 20:22:22 +08:00
|
|
|
)
|
|
|
|
.join(" ");
|
2022-02-21 16:58:44 +08:00
|
|
|
}
|
|
|
|
|
2022-01-14 19:05:28 +08:00
|
|
|
if (this.layer) identifier = `(${this.layer})/${identifier}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
if (this.options.mode) {
|
|
|
|
identifier += ` ${this.options.mode}`;
|
|
|
|
}
|
|
|
|
if (this.options.recursive) {
|
|
|
|
identifier += " recursive";
|
|
|
|
}
|
|
|
|
if (this.options.addon) {
|
2019-01-19 19:40:00 +08:00
|
|
|
identifier += ` ${contextify(
|
|
|
|
options.context,
|
|
|
|
this.options.addon,
|
|
|
|
options.associatedObjectForCache
|
|
|
|
)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.regExp) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.include) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (this.options.exclude) {
|
2022-01-11 07:27:42 +08:00
|
|
|
identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-06-10 19:31:01 +08:00
|
|
|
if (this.options.referencedExports) {
|
|
|
|
identifier += ` referencedExports: ${this.options.referencedExports
|
2025-07-17 00:13:14 +08:00
|
|
|
.map((e) => e.join("."))
|
2020-06-10 19:31:01 +08:00
|
|
|
.join(", ")}`;
|
|
|
|
}
|
2017-02-18 17:29:21 +08:00
|
|
|
|
|
|
|
return identifier;
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2018-09-12 00:47:55 +08:00
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
invalidateBuild() {
|
|
|
|
this._forceBuild = true;
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:38:34 +08:00
|
|
|
/**
|
2018-09-26 15:14:44 +08:00
|
|
|
* @param {NeedBuildContext} context context info
|
2025-03-12 09:56:14 +08:00
|
|
|
* @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
2018-09-26 15:14:44 +08:00
|
|
|
* @returns {void}
|
2018-07-25 18:38:34 +08:00
|
|
|
*/
|
2018-09-27 13:22:19 +08:00
|
|
|
needBuild({ fileSystemInfo }, callback) {
|
2019-09-04 19:34:34 +08:00
|
|
|
// build if enforced
|
2018-09-27 13:22:19 +08:00
|
|
|
if (this._forceBuild) return callback(null, true);
|
2017-02-18 18:09:18 +08:00
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
|
|
|
|
|
2022-02-25 17:22:00 +08:00
|
|
|
// always build when we have no snapshot and context
|
2025-07-02 20:10:54 +08:00
|
|
|
if (!buildInfo.snapshot) {
|
2022-02-25 17:22:00 +08:00
|
|
|
return callback(null, Boolean(this.context || this.options.resource));
|
2025-07-02 20:10:54 +08:00
|
|
|
}
|
2019-09-04 19:34:34 +08:00
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
fileSystemInfo.checkSnapshotValid(buildInfo.snapshot, (err, valid) => {
|
2019-09-04 19:34:34 +08:00
|
|
|
callback(err, !valid);
|
2018-09-27 13:22:19 +08:00
|
|
|
});
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {WebpackOptions} options webpack options
|
2018-07-25 18:12:17 +08:00
|
|
|
* @param {Compilation} compilation the compilation
|
2019-11-11 22:25:03 +08:00
|
|
|
* @param {ResolverWithOptions} resolver the resolver
|
|
|
|
* @param {InputFileSystem} fs the file system
|
2025-03-12 09:56:14 +08:00
|
|
|
* @param {BuildCallback} callback callback function
|
2018-07-25 18:12:17 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-18 17:12:09 +08:00
|
|
|
build(options, compilation, resolver, fs, callback) {
|
2018-09-12 00:47:55 +08:00
|
|
|
this._forceBuild = false;
|
2020-07-08 16:58:20 +08:00
|
|
|
/** @type {BuildMeta} */
|
|
|
|
this.buildMeta = {
|
|
|
|
exportsType: "default",
|
|
|
|
defaultObject: "redirect-warn"
|
|
|
|
};
|
2017-12-06 19:09:17 +08:00
|
|
|
this.buildInfo = {
|
2020-08-23 03:54:34 +08:00
|
|
|
snapshot: undefined
|
2017-12-06 19:09:17 +08:00
|
|
|
};
|
2018-09-12 00:47:55 +08:00
|
|
|
this.dependencies.length = 0;
|
|
|
|
this.blocks.length = 0;
|
2019-09-04 19:34:34 +08:00
|
|
|
const startTime = Date.now();
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @type {ResolveDependencies} */
|
|
|
|
(this.resolveDependencies)(fs, this.options, (err, dependencies) => {
|
2020-05-12 21:58:14 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(
|
|
|
|
makeWebpackError(err, "ContextModule.resolveDependencies")
|
|
|
|
);
|
|
|
|
}
|
2013-02-01 01:00:22 +08:00
|
|
|
|
2017-05-05 00:37:25 +08:00
|
|
|
// abort if something failed
|
|
|
|
// this will create an empty context
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!dependencies) {
|
2017-02-18 18:09:18 +08:00
|
|
|
callback();
|
|
|
|
return;
|
2016-06-23 00:24:10 +08:00
|
|
|
}
|
2017-02-18 18:09:18 +08:00
|
|
|
|
2017-05-05 00:37:25 +08:00
|
|
|
// enhance dependencies with meta info
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const dep of dependencies) {
|
2018-07-10 16:23:28 +08:00
|
|
|
dep.loc = {
|
|
|
|
name: dep.userRequest
|
|
|
|
};
|
2017-10-14 05:31:15 +08:00
|
|
|
dep.request = this.options.addon + dep.request;
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2018-10-18 15:20:59 +08:00
|
|
|
dependencies.sort(
|
|
|
|
concatComparators(
|
2025-07-17 00:13:14 +08:00
|
|
|
compareSelect((a) => a.loc, compareLocations),
|
2018-10-18 15:20:59 +08:00
|
|
|
keepOriginalOrder(this.dependencies)
|
|
|
|
)
|
|
|
|
);
|
2017-02-18 18:09:18 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.options.mode === "sync" || this.options.mode === "eager") {
|
2017-05-05 00:37:25 +08:00
|
|
|
// if we have an sync or eager context
|
|
|
|
// just add all dependencies and continue
|
2017-02-18 17:12:09 +08:00
|
|
|
this.dependencies = dependencies;
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (this.options.mode === "lazy-once") {
|
2017-05-05 00:37:25 +08:00
|
|
|
// for the lazy-once mode create a new async dependency block
|
|
|
|
// and add that block to this context
|
2018-02-25 09:00:20 +08:00
|
|
|
if (dependencies.length > 0) {
|
2019-06-19 19:16:05 +08:00
|
|
|
const block = new AsyncDependenciesBlock({
|
|
|
|
...this.options.groupOptions,
|
|
|
|
name: this.options.chunkName
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const dep of dependencies) {
|
2017-05-05 00:37:25 +08:00
|
|
|
block.addDependency(dep);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
this.addBlock(block);
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (
|
|
|
|
this.options.mode === "weak" ||
|
|
|
|
this.options.mode === "async-weak"
|
|
|
|
) {
|
2017-07-26 20:49:37 +08:00
|
|
|
// we mark all dependencies as weak
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const dep of dependencies) {
|
2018-01-22 20:52:43 +08:00
|
|
|
dep.weak = true;
|
|
|
|
}
|
2017-07-26 20:49:37 +08:00
|
|
|
this.dependencies = dependencies;
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (this.options.mode === "lazy") {
|
2017-07-26 20:49:37 +08:00
|
|
|
// if we are lazy create a new async dependency block per dependency
|
2017-05-05 00:37:25 +08:00
|
|
|
// and add all blocks to this context
|
2018-01-22 20:52:43 +08:00
|
|
|
let index = 0;
|
2018-02-25 09:00:20 +08:00
|
|
|
for (const dep of dependencies) {
|
2017-10-14 05:31:15 +08:00
|
|
|
let chunkName = this.options.chunkName;
|
2018-02-25 09:00:20 +08:00
|
|
|
if (chunkName) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!/\[(index|request)\]/.test(chunkName)) {
|
|
|
|
chunkName += "[index]";
|
|
|
|
}
|
2019-06-14 17:44:54 +08:00
|
|
|
chunkName = chunkName.replace(/\[index\]/g, `${index++}`);
|
2018-02-25 09:00:20 +08:00
|
|
|
chunkName = chunkName.replace(
|
|
|
|
/\[request\]/g,
|
|
|
|
Template.toPath(dep.userRequest)
|
|
|
|
);
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
const block = new AsyncDependenciesBlock(
|
2019-06-19 19:16:05 +08:00
|
|
|
{
|
|
|
|
...this.options.groupOptions,
|
2018-04-16 16:27:22 +08:00
|
|
|
name: chunkName
|
2019-06-19 19:16:05 +08:00
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.loc,
|
|
|
|
dep.userRequest
|
|
|
|
);
|
2017-05-05 00:37:25 +08:00
|
|
|
block.addDependency(dep);
|
|
|
|
this.addBlock(block);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-10-16 20:49:51 +08:00
|
|
|
} else {
|
2018-02-25 09:00:20 +08:00
|
|
|
callback(
|
2018-09-12 00:47:55 +08:00
|
|
|
new WebpackError(`Unsupported mode "${this.options.mode}" in context`)
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-10-16 20:49:51 +08:00
|
|
|
return;
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2022-02-25 17:22:00 +08:00
|
|
|
if (!this.context && !this.options.resource) return callback();
|
2022-02-21 18:25:30 +08:00
|
|
|
|
2025-08-26 03:16:48 +08:00
|
|
|
const snapshotOptions = compilation.options.snapshot.contextModule;
|
|
|
|
|
2019-09-04 19:34:34 +08:00
|
|
|
compilation.fileSystemInfo.createSnapshot(
|
|
|
|
startTime,
|
|
|
|
null,
|
2022-02-21 16:58:44 +08:00
|
|
|
this.context
|
|
|
|
? [this.context]
|
|
|
|
: typeof this.options.resource === "string"
|
2024-07-31 04:54:55 +08:00
|
|
|
? [this.options.resource]
|
|
|
|
: /** @type {string[]} */ (this.options.resource),
|
2019-09-04 19:34:34 +08:00
|
|
|
null,
|
2025-08-26 03:16:48 +08:00
|
|
|
snapshotOptions,
|
2019-09-04 19:34:34 +08:00
|
|
|
(err, snapshot) => {
|
|
|
|
if (err) return callback(err);
|
2024-03-26 00:05:56 +08:00
|
|
|
/** @type {BuildInfo} */
|
|
|
|
(this.buildInfo).snapshot = snapshot;
|
2019-09-04 19:34:34 +08:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 06:19:52 +08:00
|
|
|
});
|
2014-07-26 20:48:42 +08:00
|
|
|
}
|
2017-02-18 17:12:09 +08:00
|
|
|
|
2020-08-23 03:54:34 +08:00
|
|
|
/**
|
2025-08-28 18:34:30 +08:00
|
|
|
* @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
|
|
|
|
* @param {FileSystemDependencies} buildDependencies set where build dependencies are added to
|
2020-08-23 03:54:34 +08:00
|
|
|
*/
|
|
|
|
addCacheDependencies(
|
|
|
|
fileDependencies,
|
|
|
|
contextDependencies,
|
|
|
|
missingDependencies,
|
|
|
|
buildDependencies
|
|
|
|
) {
|
2022-02-21 16:58:44 +08:00
|
|
|
if (this.context) {
|
|
|
|
contextDependencies.add(this.context);
|
|
|
|
} else if (typeof this.options.resource === "string") {
|
|
|
|
contextDependencies.add(this.options.resource);
|
2022-02-21 18:25:30 +08:00
|
|
|
} else if (this.options.resource === false) {
|
2024-07-31 09:37:24 +08:00
|
|
|
// Do nothing
|
2022-02-21 16:58:44 +08:00
|
|
|
} else {
|
|
|
|
for (const res of this.options.resource) contextDependencies.add(res);
|
|
|
|
}
|
2020-08-23 03:54:34 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies all dependencies
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2025-08-28 18:34:30 +08:00
|
|
|
* @returns {Map<string, ModuleId>} map with user requests
|
2018-07-24 23:35:36 +08:00
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
getUserRequestMap(dependencies, chunkGraph) {
|
|
|
|
const moduleGraph = chunkGraph.moduleGraph;
|
2017-02-18 19:41:39 +08:00
|
|
|
// if we filter first we get a new array
|
2020-03-13 00:51:26 +08:00
|
|
|
// therefore we don't need to create a clone of dependencies explicitly
|
2017-02-18 19:41:39 +08:00
|
|
|
// therefore the order of this is !important!
|
2024-03-26 00:05:56 +08:00
|
|
|
const sortedDependencies =
|
|
|
|
/** @type {ContextElementDependency[]} */
|
|
|
|
(dependencies)
|
2025-07-17 00:13:14 +08:00
|
|
|
.filter((dependency) => moduleGraph.getModule(dependency))
|
2024-03-26 00:05:56 +08:00
|
|
|
.sort((a, b) => {
|
|
|
|
if (a.userRequest === b.userRequest) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return a.userRequest < b.userRequest ? -1 : 1;
|
|
|
|
});
|
2020-01-17 04:22:05 +08:00
|
|
|
const map = Object.create(null);
|
|
|
|
for (const dep of sortedDependencies) {
|
2024-03-26 00:05:56 +08:00
|
|
|
const module = /** @type {Module} */ (moduleGraph.getModule(dep));
|
2020-01-17 04:22:05 +08:00
|
|
|
map[dep.userRequest] = chunkGraph.getModuleId(module);
|
|
|
|
}
|
|
|
|
return map;
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies all dependencies
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2024-08-07 23:22:25 +08:00
|
|
|
* @returns {FakeMap | FakeMapType} fake map
|
2018-07-24 23:35:36 +08:00
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
getFakeMap(dependencies, chunkGraph) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!this.options.namespaceObject) {
|
|
|
|
return 9;
|
|
|
|
}
|
2018-08-28 17:50:33 +08:00
|
|
|
const moduleGraph = chunkGraph.moduleGraph;
|
2019-12-05 05:54:26 +08:00
|
|
|
// bitfield
|
|
|
|
let hasType = 0;
|
|
|
|
const comparator = compareModulesById(chunkGraph);
|
2017-12-04 19:07:43 +08:00
|
|
|
// if we filter first we get a new array
|
2020-03-13 00:51:26 +08:00
|
|
|
// therefore we don't need to create a clone of dependencies explicitly
|
2017-12-04 19:07:43 +08:00
|
|
|
// therefore the order of this is !important!
|
2020-01-17 04:22:05 +08:00
|
|
|
const sortedModules = dependencies
|
2024-03-26 00:05:56 +08:00
|
|
|
.map(
|
2025-07-17 00:13:14 +08:00
|
|
|
(dependency) =>
|
|
|
|
/** @type {Module} */ (moduleGraph.getModule(dependency))
|
2024-03-26 00:05:56 +08:00
|
|
|
)
|
2020-01-17 04:22:05 +08:00
|
|
|
.filter(Boolean)
|
|
|
|
.sort(comparator);
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @type {FakeMap} */
|
2020-01-17 04:22:05 +08:00
|
|
|
const fakeMap = Object.create(null);
|
|
|
|
for (const module of sortedModules) {
|
|
|
|
const exportsType = module.getExportsType(
|
2020-08-18 03:32:47 +08:00
|
|
|
moduleGraph,
|
2020-01-17 04:22:05 +08:00
|
|
|
this.options.namespaceObject === "strict"
|
|
|
|
);
|
2024-08-07 23:22:25 +08:00
|
|
|
const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
|
2020-01-17 04:22:05 +08:00
|
|
|
switch (exportsType) {
|
|
|
|
case "namespace":
|
|
|
|
fakeMap[id] = 9;
|
|
|
|
hasType |= 1;
|
|
|
|
break;
|
|
|
|
case "dynamic":
|
|
|
|
fakeMap[id] = 7;
|
|
|
|
hasType |= 2;
|
|
|
|
break;
|
|
|
|
case "default-only":
|
|
|
|
fakeMap[id] = 1;
|
|
|
|
hasType |= 4;
|
|
|
|
break;
|
|
|
|
case "default-with-named":
|
|
|
|
fakeMap[id] = 3;
|
|
|
|
hasType |= 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected exports type ${exportsType}`);
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 05:54:26 +08:00
|
|
|
if (hasType === 1) {
|
2018-05-29 20:50:40 +08:00
|
|
|
return 9;
|
|
|
|
}
|
2019-12-05 05:54:26 +08:00
|
|
|
if (hasType === 2) {
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
if (hasType === 4) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (hasType === 8) {
|
|
|
|
return 3;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2019-12-05 05:54:26 +08:00
|
|
|
if (hasType === 0) {
|
2018-05-29 20:50:40 +08:00
|
|
|
return 9;
|
|
|
|
}
|
2017-12-04 19:07:43 +08:00
|
|
|
return fakeMap;
|
|
|
|
}
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
2024-08-07 23:22:25 +08:00
|
|
|
* @param {FakeMap | FakeMapType} fakeMap fake map
|
2024-03-26 00:05:56 +08:00
|
|
|
* @returns {string} fake map init statement
|
|
|
|
*/
|
2017-12-23 01:23:33 +08:00
|
|
|
getFakeMapInitStatement(fakeMap) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return typeof fakeMap === "object"
|
|
|
|
? `var fakeMap = ${JSON.stringify(fakeMap, null, "\t")};`
|
|
|
|
: "";
|
2017-12-23 01:23:33 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
|
|
|
* @param {FakeMapType} type type
|
|
|
|
* @param {boolean=} asyncModule is async module
|
|
|
|
* @returns {string} return result
|
|
|
|
*/
|
2020-11-17 03:24:54 +08:00
|
|
|
getReturn(type, asyncModule) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (type === 9) {
|
2023-05-20 00:00:54 +08:00
|
|
|
return `${RuntimeGlobals.require}(id)`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-11-17 03:24:54 +08:00
|
|
|
return `${RuntimeGlobals.createFakeNamespaceObject}(id, ${type}${
|
|
|
|
asyncModule ? " | 16" : ""
|
|
|
|
})`;
|
2018-01-06 10:18:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
2024-08-07 23:22:25 +08:00
|
|
|
* @param {FakeMap | FakeMapType} fakeMap fake map
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {boolean=} asyncModule us async module
|
|
|
|
* @param {string=} fakeMapDataExpression fake map data expression
|
|
|
|
* @returns {string} module object source
|
|
|
|
*/
|
2020-11-17 03:24:54 +08:00
|
|
|
getReturnModuleObjectSource(
|
|
|
|
fakeMap,
|
|
|
|
asyncModule,
|
|
|
|
fakeMapDataExpression = "fakeMap[id]"
|
|
|
|
) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (typeof fakeMap === "number") {
|
2021-06-01 00:16:21 +08:00
|
|
|
return `return ${this.getReturn(fakeMap, asyncModule)};`;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-11-17 03:24:54 +08:00
|
|
|
return `return ${
|
|
|
|
RuntimeGlobals.createFakeNamespaceObject
|
|
|
|
}(id, ${fakeMapDataExpression}${asyncModule ? " | 16" : ""})`;
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies dependencies
|
|
|
|
* @param {ModuleId} id module id
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph the chunk graph
|
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
|
|
|
getSyncSource(dependencies, id, chunkGraph) {
|
|
|
|
const map = this.getUserRequestMap(dependencies, chunkGraph);
|
|
|
|
const fakeMap = this.getFakeMap(dependencies, chunkGraph);
|
2018-01-06 10:18:40 +08:00
|
|
|
const returnModuleObject = this.getReturnModuleObjectSource(fakeMap);
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-02-18 19:41:39 +08:00
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
2017-12-23 01:23:33 +08:00
|
|
|
${this.getFakeMapInitStatement(fakeMap)}
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-02-18 19:41:39 +08:00
|
|
|
function webpackContext(req) {
|
2017-12-04 19:07:43 +08:00
|
|
|
var id = webpackContextResolve(req);
|
|
|
|
${returnModuleObject}
|
|
|
|
}
|
2017-02-18 19:41:39 +08:00
|
|
|
function webpackContextResolve(req) {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
return map[req];
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2017-02-18 19:41:39 +08:00
|
|
|
webpackContext.keys = function webpackContextKeys() {
|
|
|
|
return Object.keys(map);
|
|
|
|
};
|
|
|
|
webpackContext.resolve = webpackContextResolve;
|
|
|
|
module.exports = webpackContext;
|
|
|
|
webpackContext.id = ${JSON.stringify(id)};`;
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies dependencies
|
|
|
|
* @param {ModuleId} id module id
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph the chunk graph
|
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
|
|
|
getWeakSyncSource(dependencies, id, chunkGraph) {
|
|
|
|
const map = this.getUserRequestMap(dependencies, chunkGraph);
|
|
|
|
const fakeMap = this.getFakeMap(dependencies, chunkGraph);
|
2018-01-06 10:18:40 +08:00
|
|
|
const returnModuleObject = this.getReturnModuleObjectSource(fakeMap);
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-07-26 01:21:02 +08:00
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
2017-12-23 01:23:33 +08:00
|
|
|
${this.getFakeMapInitStatement(fakeMap)}
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-07-26 01:21:02 +08:00
|
|
|
function webpackContext(req) {
|
2017-07-26 20:49:37 +08:00
|
|
|
var id = webpackContextResolve(req);
|
2018-11-05 21:36:15 +08:00
|
|
|
if(!${RuntimeGlobals.moduleFactories}[id]) {
|
2017-12-19 22:50:09 +08:00
|
|
|
var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
|
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2017-12-04 19:07:43 +08:00
|
|
|
${returnModuleObject}
|
|
|
|
}
|
2017-07-26 01:21:02 +08:00
|
|
|
function webpackContextResolve(req) {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
return map[req];
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2017-07-26 01:21:02 +08:00
|
|
|
webpackContext.keys = function webpackContextKeys() {
|
|
|
|
return Object.keys(map);
|
|
|
|
};
|
|
|
|
webpackContext.resolve = webpackContextResolve;
|
2017-07-26 21:13:39 +08:00
|
|
|
webpackContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackContext;`;
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies dependencies
|
|
|
|
* @param {ModuleId} id module id
|
2024-06-11 21:09:50 +08:00
|
|
|
* @param {object} context context
|
2019-08-27 02:21:07 +08:00
|
|
|
* @param {ChunkGraph} context.chunkGraph the chunk graph
|
|
|
|
* @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
|
2018-08-28 17:50:33 +08:00
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
2019-08-27 02:21:07 +08:00
|
|
|
getAsyncWeakSource(dependencies, id, { chunkGraph, runtimeTemplate }) {
|
|
|
|
const arrow = runtimeTemplate.supportsArrowFunction();
|
2018-08-28 17:50:33 +08:00
|
|
|
const map = this.getUserRequestMap(dependencies, chunkGraph);
|
|
|
|
const fakeMap = this.getFakeMap(dependencies, chunkGraph);
|
2020-11-17 03:24:54 +08:00
|
|
|
const returnModuleObject = this.getReturnModuleObjectSource(fakeMap, true);
|
2017-07-26 01:21:02 +08:00
|
|
|
|
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
2017-12-23 01:23:33 +08:00
|
|
|
${this.getFakeMapInitStatement(fakeMap)}
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-07-26 01:21:02 +08:00
|
|
|
function webpackAsyncContext(req) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return webpackAsyncContextResolve(req).then(${
|
|
|
|
arrow ? "id =>" : "function(id)"
|
|
|
|
} {
|
2018-11-05 21:36:15 +08:00
|
|
|
if(!${RuntimeGlobals.moduleFactories}[id]) {
|
2017-12-19 22:50:09 +08:00
|
|
|
var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
|
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2017-12-04 19:07:43 +08:00
|
|
|
${returnModuleObject}
|
2017-07-26 20:49:37 +08:00
|
|
|
});
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2017-07-26 20:49:37 +08:00
|
|
|
function webpackAsyncContextResolve(req) {
|
|
|
|
// Here Promise.resolve().then() is used instead of new Promise() to prevent
|
2018-02-26 10:50:05 +08:00
|
|
|
// uncaught exception popping up in devtools
|
2019-08-27 02:21:07 +08:00
|
|
|
return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
return map[req];
|
2017-07-26 01:21:02 +08:00
|
|
|
});
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2019-12-03 22:05:22 +08:00
|
|
|
webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
|
|
|
|
"Object.keys(map)"
|
|
|
|
)};
|
2017-07-26 20:49:37 +08:00
|
|
|
webpackAsyncContext.resolve = webpackAsyncContextResolve;
|
|
|
|
webpackAsyncContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackAsyncContext;`;
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {Dependency[]} dependencies dependencies
|
|
|
|
* @param {ModuleId} id module id
|
2024-06-11 21:09:50 +08:00
|
|
|
* @param {object} context context
|
2019-08-27 02:21:07 +08:00
|
|
|
* @param {ChunkGraph} context.chunkGraph the chunk graph
|
|
|
|
* @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
|
2018-08-28 17:50:33 +08:00
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
2019-08-27 02:21:07 +08:00
|
|
|
getEagerSource(dependencies, id, { chunkGraph, runtimeTemplate }) {
|
|
|
|
const arrow = runtimeTemplate.supportsArrowFunction();
|
2018-08-28 17:50:33 +08:00
|
|
|
const map = this.getUserRequestMap(dependencies, chunkGraph);
|
|
|
|
const fakeMap = this.getFakeMap(dependencies, chunkGraph);
|
2018-02-25 09:00:20 +08:00
|
|
|
const thenFunction =
|
2018-05-03 00:09:24 +08:00
|
|
|
fakeMap !== 9
|
2019-08-27 02:21:07 +08:00
|
|
|
? `${arrow ? "id =>" : "function(id)"} {
|
2024-04-30 17:42:04 +08:00
|
|
|
${this.getReturnModuleObjectSource(fakeMap, true)}
|
2018-05-03 00:09:24 +08:00
|
|
|
}`
|
2023-05-20 00:00:54 +08:00
|
|
|
: RuntimeGlobals.require;
|
2017-05-05 00:37:25 +08:00
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
2017-12-23 01:23:33 +08:00
|
|
|
${this.getFakeMapInitStatement(fakeMap)}
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-05-05 00:37:25 +08:00
|
|
|
function webpackAsyncContext(req) {
|
2017-12-04 19:07:43 +08:00
|
|
|
return webpackAsyncContextResolve(req).then(${thenFunction});
|
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
function webpackAsyncContextResolve(req) {
|
2017-07-26 20:49:37 +08:00
|
|
|
// Here Promise.resolve().then() is used instead of new Promise() to prevent
|
2018-02-26 10:50:05 +08:00
|
|
|
// uncaught exception popping up in devtools
|
2019-08-27 02:21:07 +08:00
|
|
|
return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
return map[req];
|
2017-05-05 00:37:25 +08:00
|
|
|
});
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2019-12-03 22:05:22 +08:00
|
|
|
webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
|
|
|
|
"Object.keys(map)"
|
|
|
|
)};
|
2017-05-05 00:37:25 +08:00
|
|
|
webpackAsyncContext.resolve = webpackAsyncContextResolve;
|
2017-07-26 20:49:37 +08:00
|
|
|
webpackAsyncContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackAsyncContext;`;
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {AsyncDependenciesBlock} block block
|
|
|
|
* @param {Dependency[]} dependencies dependencies
|
|
|
|
* @param {ModuleId} id module id
|
2024-06-11 21:09:50 +08:00
|
|
|
* @param {object} options options object
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {RuntimeTemplate} options.runtimeTemplate the runtime template
|
|
|
|
* @param {ChunkGraph} options.chunkGraph the chunk graph
|
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
2018-11-20 16:31:36 +08:00
|
|
|
getLazyOnceSource(block, dependencies, id, { runtimeTemplate, chunkGraph }) {
|
2017-12-19 22:50:09 +08:00
|
|
|
const promise = runtimeTemplate.blockPromise({
|
2018-08-23 02:17:49 +08:00
|
|
|
chunkGraph,
|
2017-12-19 22:50:09 +08:00
|
|
|
block,
|
2018-11-17 01:18:44 +08:00
|
|
|
message: "lazy-once context",
|
2018-11-20 16:31:36 +08:00
|
|
|
runtimeRequirements: new Set()
|
2017-12-19 22:50:09 +08:00
|
|
|
});
|
2019-08-27 02:21:07 +08:00
|
|
|
const arrow = runtimeTemplate.supportsArrowFunction();
|
2018-08-28 17:50:33 +08:00
|
|
|
const map = this.getUserRequestMap(dependencies, chunkGraph);
|
|
|
|
const fakeMap = this.getFakeMap(dependencies, chunkGraph);
|
2018-02-25 09:00:20 +08:00
|
|
|
const thenFunction =
|
2018-05-03 00:09:24 +08:00
|
|
|
fakeMap !== 9
|
2019-08-27 02:21:07 +08:00
|
|
|
? `${arrow ? "id =>" : "function(id)"} {
|
2020-11-17 03:24:54 +08:00
|
|
|
${this.getReturnModuleObjectSource(fakeMap, true)};
|
2018-02-25 09:00:20 +08:00
|
|
|
}`
|
2023-05-20 00:00:54 +08:00
|
|
|
: RuntimeGlobals.require;
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-05-05 00:37:25 +08:00
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
2017-12-23 01:23:33 +08:00
|
|
|
${this.getFakeMapInitStatement(fakeMap)}
|
2017-12-04 19:07:43 +08:00
|
|
|
|
2017-05-05 00:37:25 +08:00
|
|
|
function webpackAsyncContext(req) {
|
2017-12-04 19:07:43 +08:00
|
|
|
return webpackAsyncContextResolve(req).then(${thenFunction});
|
|
|
|
}
|
2017-05-05 00:37:25 +08:00
|
|
|
function webpackAsyncContextResolve(req) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return ${promise}.then(${arrow ? "() =>" : "function()"} {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
return map[req];
|
2017-05-05 00:37:25 +08:00
|
|
|
});
|
2017-12-04 19:07:43 +08:00
|
|
|
}
|
2019-12-03 22:05:22 +08:00
|
|
|
webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
|
|
|
|
"Object.keys(map)"
|
|
|
|
)};
|
2017-05-05 00:37:25 +08:00
|
|
|
webpackAsyncContext.resolve = webpackAsyncContextResolve;
|
2017-07-26 20:49:37 +08:00
|
|
|
webpackAsyncContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackAsyncContext;`;
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 17:50:33 +08:00
|
|
|
/**
|
2024-03-26 00:05:56 +08:00
|
|
|
* @param {AsyncDependenciesBlock[]} blocks blocks
|
|
|
|
* @param {ModuleId} id module id
|
2024-06-11 21:09:50 +08:00
|
|
|
* @param {object} context context
|
2019-08-27 02:21:07 +08:00
|
|
|
* @param {ChunkGraph} context.chunkGraph the chunk graph
|
|
|
|
* @param {RuntimeTemplate} context.runtimeTemplate the chunk graph
|
2018-08-28 17:50:33 +08:00
|
|
|
* @returns {string} source code
|
|
|
|
*/
|
2019-08-27 02:21:07 +08:00
|
|
|
getLazySource(blocks, id, { chunkGraph, runtimeTemplate }) {
|
2018-08-28 17:50:33 +08:00
|
|
|
const moduleGraph = chunkGraph.moduleGraph;
|
2019-08-27 02:21:07 +08:00
|
|
|
const arrow = runtimeTemplate.supportsArrowFunction();
|
2017-02-18 20:01:29 +08:00
|
|
|
let hasMultipleOrNoChunks = false;
|
2019-02-06 00:29:27 +08:00
|
|
|
let hasNoChunk = true;
|
2018-07-24 23:35:36 +08:00
|
|
|
const fakeMap = this.getFakeMap(
|
2025-07-17 00:13:14 +08:00
|
|
|
blocks.map((b) => b.dependencies[0]),
|
2018-08-28 17:50:33 +08:00
|
|
|
chunkGraph
|
2018-07-24 23:35:36 +08:00
|
|
|
);
|
2019-02-06 00:29:27 +08:00
|
|
|
const hasFakeMap = typeof fakeMap === "object";
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @typedef {{userRequest: string, dependency: ContextElementDependency, chunks: undefined | Chunk[], module: Module, block: AsyncDependenciesBlock}} Item */
|
|
|
|
/**
|
|
|
|
* @type {Item[]}
|
|
|
|
*/
|
2019-02-07 16:41:34 +08:00
|
|
|
const items = blocks
|
2025-07-17 00:13:14 +08:00
|
|
|
.map((block) => {
|
2024-03-26 00:05:56 +08:00
|
|
|
const dependency =
|
|
|
|
/** @type {ContextElementDependency} */
|
|
|
|
(block.dependencies[0]);
|
2018-07-24 23:35:36 +08:00
|
|
|
return {
|
2024-07-31 04:09:42 +08:00
|
|
|
dependency,
|
2024-03-26 00:05:56 +08:00
|
|
|
module: /** @type {Module} */ (moduleGraph.getModule(dependency)),
|
2024-07-31 04:09:42 +08:00
|
|
|
block,
|
2019-02-07 16:41:34 +08:00
|
|
|
userRequest: dependency.userRequest,
|
|
|
|
chunks: undefined
|
2018-07-24 23:35:36 +08:00
|
|
|
};
|
|
|
|
})
|
2025-07-17 00:13:14 +08:00
|
|
|
.filter((item) => item.module);
|
2019-02-07 16:41:34 +08:00
|
|
|
for (const item of items) {
|
|
|
|
const chunkGroup = chunkGraph.getBlockChunkGroup(item.block);
|
|
|
|
const chunks = (chunkGroup && chunkGroup.chunks) || [];
|
|
|
|
item.chunks = chunks;
|
|
|
|
if (chunks.length > 0) {
|
|
|
|
hasNoChunk = false;
|
|
|
|
}
|
|
|
|
if (chunks.length !== 1) {
|
|
|
|
hasMultipleOrNoChunks = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const shortMode = hasNoChunk && !hasFakeMap;
|
2020-01-17 04:22:05 +08:00
|
|
|
const sortedItems = items.sort((a, b) => {
|
|
|
|
if (a.userRequest === b.userRequest) return 0;
|
|
|
|
return a.userRequest < b.userRequest ? -1 : 1;
|
|
|
|
});
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @type {Record<string, ModuleId | (ModuleId[] | ChunkId[])>} */
|
2020-01-17 04:22:05 +08:00
|
|
|
const map = Object.create(null);
|
|
|
|
for (const item of sortedItems) {
|
2024-08-07 23:22:25 +08:00
|
|
|
const moduleId =
|
|
|
|
/** @type {ModuleId} */
|
|
|
|
(chunkGraph.getModuleId(item.module));
|
2020-01-17 04:22:05 +08:00
|
|
|
if (shortMode) {
|
|
|
|
map[item.userRequest] = moduleId;
|
|
|
|
} else {
|
2024-08-07 23:22:25 +08:00
|
|
|
/** @type {(ModuleId | ChunkId)[]} */
|
2020-01-17 04:22:05 +08:00
|
|
|
const arrayStart = [moduleId];
|
|
|
|
if (hasFakeMap) {
|
|
|
|
arrayStart.push(fakeMap[moduleId]);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2025-07-03 17:06:45 +08:00
|
|
|
map[item.userRequest] = [
|
|
|
|
...arrayStart,
|
|
|
|
.../** @type {Chunk[]} */
|
2025-07-17 00:13:14 +08:00
|
|
|
(item.chunks).map((chunk) => /** @type {ChunkId} */ (chunk.id))
|
2025-07-03 17:06:45 +08:00
|
|
|
];
|
2020-01-17 04:22:05 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 19:41:39 +08:00
|
|
|
|
2019-02-06 00:29:27 +08:00
|
|
|
const chunksStartPosition = hasFakeMap ? 2 : 1;
|
|
|
|
const requestPrefix = hasNoChunk
|
|
|
|
? "Promise.resolve()"
|
|
|
|
: hasMultipleOrNoChunks
|
2024-07-31 04:54:55 +08:00
|
|
|
? `Promise.all(ids.slice(${chunksStartPosition}).map(${RuntimeGlobals.ensureChunk}))`
|
|
|
|
: `${RuntimeGlobals.ensureChunk}(ids[${chunksStartPosition}])`;
|
2018-02-25 09:00:20 +08:00
|
|
|
const returnModuleObject = this.getReturnModuleObjectSource(
|
|
|
|
fakeMap,
|
2020-11-17 03:24:54 +08:00
|
|
|
true,
|
2019-02-06 00:29:27 +08:00
|
|
|
shortMode ? "invalid" : "ids[1]"
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-18 19:41:39 +08:00
|
|
|
|
2019-02-06 00:29:27 +08:00
|
|
|
const webpackAsyncContext =
|
|
|
|
requestPrefix === "Promise.resolve()"
|
2020-11-17 03:24:54 +08:00
|
|
|
? `
|
2017-02-18 19:41:39 +08:00
|
|
|
function webpackAsyncContext(req) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2019-02-06 00:29:27 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
${shortMode ? "var id = map[req];" : "var ids = map[req], id = ids[0];"}
|
|
|
|
${returnModuleObject}
|
|
|
|
});
|
|
|
|
}`
|
|
|
|
: `function webpackAsyncContext(req) {
|
2019-12-02 22:59:37 +08:00
|
|
|
if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
2019-02-06 00:29:27 +08:00
|
|
|
|
|
|
|
var ids = map[req], id = ids[0];
|
2019-08-27 02:21:07 +08:00
|
|
|
return ${requestPrefix}.then(${arrow ? "() =>" : "function()"} {
|
2017-12-04 19:07:43 +08:00
|
|
|
${returnModuleObject}
|
2017-02-18 19:41:39 +08:00
|
|
|
});
|
2019-02-06 00:29:27 +08:00
|
|
|
}`;
|
|
|
|
|
|
|
|
return `var map = ${JSON.stringify(map, null, "\t")};
|
|
|
|
${webpackAsyncContext}
|
2019-12-03 22:05:22 +08:00
|
|
|
webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
|
|
|
|
"Object.keys(map)"
|
|
|
|
)};
|
2017-07-26 20:49:37 +08:00
|
|
|
webpackAsyncContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackAsyncContext;`;
|
2017-02-18 19:41:39 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleId} id module id
|
|
|
|
* @param {RuntimeTemplate} runtimeTemplate runtime template
|
|
|
|
* @returns {string} source for empty async context
|
|
|
|
*/
|
2019-08-27 02:21:07 +08:00
|
|
|
getSourceForEmptyContext(id, runtimeTemplate) {
|
2017-02-18 19:41:39 +08:00
|
|
|
return `function webpackEmptyContext(req) {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
2017-02-18 19:41:39 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
webpackEmptyContext.keys = ${runtimeTemplate.returningFunction("[]")};
|
2017-02-18 19:41:39 +08:00
|
|
|
webpackEmptyContext.resolve = webpackEmptyContext;
|
2019-08-27 02:21:07 +08:00
|
|
|
webpackEmptyContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackEmptyContext;`;
|
2017-02-18 19:41:39 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:05:56 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleId} id module id
|
|
|
|
* @param {RuntimeTemplate} runtimeTemplate runtime template
|
|
|
|
* @returns {string} source for empty async context
|
|
|
|
*/
|
2019-08-27 02:21:07 +08:00
|
|
|
getSourceForEmptyAsyncContext(id, runtimeTemplate) {
|
|
|
|
const arrow = runtimeTemplate.supportsArrowFunction();
|
2017-05-05 00:37:25 +08:00
|
|
|
return `function webpackEmptyAsyncContext(req) {
|
2017-07-26 20:49:37 +08:00
|
|
|
// Here Promise.resolve().then() is used instead of new Promise() to prevent
|
2018-02-26 10:50:05 +08:00
|
|
|
// uncaught exception popping up in devtools
|
2019-08-27 02:21:07 +08:00
|
|
|
return Promise.resolve().then(${arrow ? "() =>" : "function()"} {
|
2018-05-15 14:16:39 +08:00
|
|
|
var e = new Error("Cannot find module '" + req + "'");
|
2017-12-19 22:50:09 +08:00
|
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw e;
|
2017-07-26 20:49:37 +08:00
|
|
|
});
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
webpackEmptyAsyncContext.keys = ${runtimeTemplate.returningFunction("[]")};
|
2017-05-05 00:37:25 +08:00
|
|
|
webpackEmptyAsyncContext.resolve = webpackEmptyAsyncContext;
|
2019-08-27 02:21:07 +08:00
|
|
|
webpackEmptyAsyncContext.id = ${JSON.stringify(id)};
|
|
|
|
module.exports = webpackEmptyAsyncContext;`;
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2017-02-18 19:41:39 +08:00
|
|
|
|
2018-08-23 02:17:49 +08:00
|
|
|
/**
|
|
|
|
* @param {string} asyncMode module mode
|
2019-10-09 04:29:46 +08:00
|
|
|
* @param {CodeGenerationContext} context context info
|
2018-08-23 02:17:49 +08:00
|
|
|
* @returns {string} the source code
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
getSourceString(asyncMode, { runtimeTemplate, chunkGraph }) {
|
2024-08-07 23:22:25 +08:00
|
|
|
const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(this));
|
2018-02-25 09:00:20 +08:00
|
|
|
if (asyncMode === "lazy") {
|
|
|
|
if (this.blocks && this.blocks.length > 0) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getLazySource(this.blocks, id, {
|
|
|
|
runtimeTemplate,
|
|
|
|
chunkGraph
|
|
|
|
});
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (asyncMode === "eager") {
|
|
|
|
if (this.dependencies && this.dependencies.length > 0) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getEagerSource(this.dependencies, id, {
|
|
|
|
chunkGraph,
|
|
|
|
runtimeTemplate
|
|
|
|
});
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (asyncMode === "lazy-once") {
|
2017-05-05 00:37:25 +08:00
|
|
|
const block = this.blocks[0];
|
2018-02-25 09:00:20 +08:00
|
|
|
if (block) {
|
2018-08-28 17:50:33 +08:00
|
|
|
return this.getLazyOnceSource(block, block.dependencies, id, {
|
2018-07-24 23:35:36 +08:00
|
|
|
runtimeTemplate,
|
2018-11-20 16:31:36 +08:00
|
|
|
chunkGraph
|
2018-07-24 23:35:36 +08:00
|
|
|
});
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
|
2017-05-05 00:37:25 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (asyncMode === "async-weak") {
|
|
|
|
if (this.dependencies && this.dependencies.length > 0) {
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getAsyncWeakSource(this.dependencies, id, {
|
|
|
|
chunkGraph,
|
|
|
|
runtimeTemplate
|
|
|
|
});
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getSourceForEmptyAsyncContext(id, runtimeTemplate);
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
2024-08-02 02:36:27 +08:00
|
|
|
if (
|
|
|
|
asyncMode === "weak" &&
|
|
|
|
this.dependencies &&
|
|
|
|
this.dependencies.length > 0
|
|
|
|
) {
|
|
|
|
return this.getWeakSyncSource(this.dependencies, id, chunkGraph);
|
2017-07-26 01:21:02 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.dependencies && this.dependencies.length > 0) {
|
2018-08-28 17:50:33 +08:00
|
|
|
return this.getSyncSource(this.dependencies, id, chunkGraph);
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2019-08-27 02:21:07 +08:00
|
|
|
return this.getSourceForEmptyContext(id, runtimeTemplate);
|
2017-02-18 19:41:39 +08:00
|
|
|
}
|
|
|
|
|
2022-02-18 01:08:27 +08:00
|
|
|
/**
|
|
|
|
* @param {string} sourceString source content
|
|
|
|
* @param {Compilation=} compilation the compilation
|
|
|
|
* @returns {Source} generated source
|
|
|
|
*/
|
|
|
|
getSource(sourceString, compilation) {
|
2020-10-26 21:41:46 +08:00
|
|
|
if (this.useSourceMap || this.useSimpleSourceMap) {
|
2022-02-18 01:08:27 +08:00
|
|
|
return new OriginalSource(
|
|
|
|
sourceString,
|
|
|
|
`webpack://${makePathsRelative(
|
2022-03-07 22:08:08 +08:00
|
|
|
(compilation && compilation.compiler.context) || "",
|
2022-02-18 01:08:27 +08:00
|
|
|
this.identifier(),
|
|
|
|
compilation && compilation.compiler.root
|
|
|
|
)}`
|
|
|
|
);
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2017-02-18 19:41:39 +08:00
|
|
|
return new RawSource(sourceString);
|
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
2019-10-09 04:29:46 +08:00
|
|
|
* @param {CodeGenerationContext} context context for code generation
|
|
|
|
* @returns {CodeGenerationResult} result
|
2018-07-25 18:12:17 +08:00
|
|
|
*/
|
2019-10-09 04:29:46 +08:00
|
|
|
codeGeneration(context) {
|
2022-02-18 01:08:27 +08:00
|
|
|
const { chunkGraph, compilation } = context;
|
2019-10-09 04:29:46 +08:00
|
|
|
const sources = new Map();
|
|
|
|
sources.set(
|
|
|
|
"javascript",
|
2022-02-18 01:08:27 +08:00
|
|
|
this.getSource(
|
|
|
|
this.getSourceString(this.options.mode, context),
|
|
|
|
compilation
|
|
|
|
)
|
2017-02-18 19:41:39 +08:00
|
|
|
);
|
2020-01-29 17:28:33 +08:00
|
|
|
const set = new Set();
|
2022-03-16 20:25:44 +08:00
|
|
|
const allDeps =
|
|
|
|
this.dependencies.length > 0
|
2025-07-03 17:06:45 +08:00
|
|
|
? /** @type {ContextElementDependency[]} */ [...this.dependencies]
|
2022-03-16 20:25:44 +08:00
|
|
|
: [];
|
2025-07-02 20:10:54 +08:00
|
|
|
for (const block of this.blocks) {
|
|
|
|
for (const dep of block.dependencies) {
|
2022-03-16 20:25:44 +08:00
|
|
|
allDeps.push(/** @type {ContextElementDependency} */ (dep));
|
2025-07-02 20:10:54 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.module);
|
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
2018-11-06 02:03:12 +08:00
|
|
|
if (allDeps.length > 0) {
|
|
|
|
const asyncMode = this.options.mode;
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.require);
|
2018-11-06 02:03:12 +08:00
|
|
|
if (asyncMode === "weak") {
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
2018-11-06 02:03:12 +08:00
|
|
|
} else if (asyncMode === "async-weak") {
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
|
|
|
set.add(RuntimeGlobals.ensureChunk);
|
2018-11-06 02:03:12 +08:00
|
|
|
} else if (asyncMode === "lazy" || asyncMode === "lazy-once") {
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.ensureChunk);
|
2018-11-06 02:03:12 +08:00
|
|
|
}
|
|
|
|
if (this.getFakeMap(allDeps, chunkGraph) !== 9) {
|
2020-01-29 17:28:33 +08:00
|
|
|
set.add(RuntimeGlobals.createFakeNamespaceObject);
|
2018-11-06 02:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 04:29:46 +08:00
|
|
|
return {
|
|
|
|
sources,
|
|
|
|
runtimeRequirements: set
|
|
|
|
};
|
2018-11-06 02:03:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 18:12:17 +08:00
|
|
|
/**
|
2018-12-04 18:23:40 +08:00
|
|
|
* @param {string=} type the source type for which the size should be estimated
|
2019-05-13 18:29:29 +08:00
|
|
|
* @returns {number} the estimated size of the module (must be non-zero)
|
2018-07-25 18:12:17 +08:00
|
|
|
*/
|
2018-12-04 18:23:40 +08:00
|
|
|
size(type) {
|
2017-02-18 18:13:39 +08:00
|
|
|
// base penalty
|
2020-01-17 04:22:05 +08:00
|
|
|
let size = 160;
|
2017-02-18 18:13:39 +08:00
|
|
|
|
2020-03-13 00:51:26 +08:00
|
|
|
// if we don't have dependencies we stop here.
|
2020-01-17 04:22:05 +08:00
|
|
|
for (const dependency of this.dependencies) {
|
2018-06-08 19:20:57 +08:00
|
|
|
const element = /** @type {ContextElementDependency} */ (dependency);
|
2020-01-17 04:22:05 +08:00
|
|
|
size += 5 + element.userRequest.length;
|
|
|
|
}
|
|
|
|
return size;
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
2019-09-04 19:34:34 +08:00
|
|
|
|
2023-04-12 02:57:43 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
2019-09-04 19:34:34 +08:00
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
2020-07-17 16:27:48 +08:00
|
|
|
write(this._identifier);
|
2019-09-04 19:34:34 +08:00
|
|
|
write(this._forceBuild);
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
2023-04-12 02:57:43 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
*/
|
2019-09-04 19:34:34 +08:00
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
2020-07-17 16:27:48 +08:00
|
|
|
this._identifier = read();
|
2019-09-04 19:34:34 +08:00
|
|
|
this._forceBuild = read();
|
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2017-02-18 17:12:09 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 19:34:34 +08:00
|
|
|
makeSerializable(ContextModule, "webpack/lib/ContextModule");
|
2018-10-18 21:52:22 +08:00
|
|
|
|
2017-02-18 17:12:09 +08:00
|
|
|
module.exports = ContextModule;
|