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