MCPcopy
hub / github.com/webpack/webpack / getLazyOnceSource

Method getLazyOnceSource

lib/ContextModule.js:1037–1091  ·  view source on GitHub ↗

* @param {AsyncDependenciesBlock} block block * @param {Dependency[]} dependencies dependencies * @param {ModuleId} id module id * @param {ImportPhaseType} phase import phase * @param {object} options options object * @param {RuntimeTemplate} options.runtimeTemplate the runtime template

(
		block,
		dependencies,
		id,
		phase,
		{ runtimeTemplate, chunkGraph }
	)

Source from the content-addressed store, hash-verified

1035 * @returns {string} source code
1036 */
1037 getLazyOnceSource(
1038 block,
1039 dependencies,
1040 id,
1041 phase,
1042 { runtimeTemplate, chunkGraph }
1043 ) {
1044 const promise = runtimeTemplate.blockPromise({
1045 chunkGraph,
1046 block,
1047 message: "lazy-once context",
1048 /** @type {RuntimeRequirements} */
1049 runtimeRequirements: new Set()
1050 });
1051 const map = this.getUserRequestMap(dependencies, chunkGraph);
1052 const fakeMap = this.getFakeMap(dependencies, chunkGraph);
1053 const asyncDepsMap =
1054 ImportPhaseUtils.isDefer(phase) &&
1055 this.getModuleDeferredAsyncDepsMap(dependencies, chunkGraph);
1056 const thenFunction = runtimeTemplate.returningFunction(
1057 this.getReturnModuleObjectSource(
1058 fakeMap,
1059 true,
1060 asyncDepsMap ? "asyncDepsMap[id]" : undefined
1061 ),
1062 "id"
1063 );
1064
1065 const cst = runtimeTemplate.renderConst();
1066 return `${cst} map = ${JSON.stringify(map, null, "\t")};
1067${this.getFakeMapInitStatement(fakeMap, runtimeTemplate)}
1068${this.getModuleDeferredAsyncDepsMapInitStatement(asyncDepsMap, runtimeTemplate)}
1069
1070function webpackAsyncContext(req) {
1071 return webpackAsyncContextResolve(req).then(${thenFunction});
1072}
1073function webpackAsyncContextResolve(req) {
1074 return ${promise}.then(${runtimeTemplate.basicFunction("", [
1075 `if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {`,
1076 Template.indent([
1077 `${cst} e = new Error("Cannot find module '" + req + "'");`,
1078 "e.code = 'MODULE_NOT_FOUND';",
1079 "throw e;"
1080 ]),
1081 "}",
1082 "return map[req];"
1083 ])});
1084}
1085webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
1086 "Object.keys(map)"
1087 )};
1088webpackAsyncContext.resolve = webpackAsyncContextResolve;
1089webpackAsyncContext.id = ${JSON.stringify(id)};
1090module.exports = webpackAsyncContext;`;
1091 }
1092
1093 /**
1094 * @param {AsyncDependenciesBlock[]} blocks blocks

Callers 1

getSourceStringMethod · 0.95

Calls 11

getUserRequestMapMethod · 0.95
getFakeMapMethod · 0.95
blockPromiseMethod · 0.80
returningFunctionMethod · 0.80
renderConstMethod · 0.80
basicFunctionMethod · 0.80
indentMethod · 0.80

Tested by

no test coverage detected