* @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 }
)
| 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 | |
| 1070 | function webpackAsyncContext(req) { |
| 1071 | return webpackAsyncContextResolve(req).then(${thenFunction}); |
| 1072 | } |
| 1073 | function 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 | } |
| 1085 | webpackAsyncContext.keys = ${runtimeTemplate.returningFunction( |
| 1086 | "Object.keys(map)" |
| 1087 | )}; |
| 1088 | webpackAsyncContext.resolve = webpackAsyncContextResolve; |
| 1089 | webpackAsyncContext.id = ${JSON.stringify(id)}; |
| 1090 | module.exports = webpackAsyncContext;`; |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * @param {AsyncDependenciesBlock[]} blocks blocks |
no test coverage detected