* @param {Dependency[]} dependencies dependencies * @param {ModuleId} id module id * @param {ImportPhaseType} phase import phase * @param {object} context context * @param {ChunkGraph} context.chunkGraph the chunk graph * @param {RuntimeTemplate} context.runtimeTemplate the chunk graph *
(dependencies, id, phase, { chunkGraph, runtimeTemplate })
| 980 | * @returns {string} source code |
| 981 | */ |
| 982 | getEagerSource(dependencies, id, phase, { chunkGraph, runtimeTemplate }) { |
| 983 | const map = this.getUserRequestMap(dependencies, chunkGraph); |
| 984 | const fakeMap = this.getFakeMap(dependencies, chunkGraph); |
| 985 | const asyncDepsMap = |
| 986 | ImportPhaseUtils.isDefer(phase) && |
| 987 | this.getModuleDeferredAsyncDepsMap(dependencies, chunkGraph); |
| 988 | const thenFunction = runtimeTemplate.returningFunction( |
| 989 | this.getReturnModuleObjectSource( |
| 990 | fakeMap, |
| 991 | true, |
| 992 | asyncDepsMap ? "asyncDepsMap[id]" : undefined |
| 993 | ), |
| 994 | "id" |
| 995 | ); |
| 996 | |
| 997 | const cst = runtimeTemplate.renderConst(); |
| 998 | return `${cst} map = ${JSON.stringify(map, null, "\t")}; |
| 999 | ${this.getFakeMapInitStatement(fakeMap, runtimeTemplate)} |
| 1000 | ${this.getModuleDeferredAsyncDepsMapInitStatement(asyncDepsMap, runtimeTemplate)} |
| 1001 | |
| 1002 | function webpackAsyncContext(req) { |
| 1003 | return webpackAsyncContextResolve(req).then(${thenFunction}); |
| 1004 | } |
| 1005 | function webpackAsyncContextResolve(req) { |
| 1006 | // Here Promise.resolve().then() is used instead of new Promise() to prevent |
| 1007 | // uncaught exception popping up in devtools |
| 1008 | return Promise.resolve().then(${runtimeTemplate.basicFunction("", [ |
| 1009 | `if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {`, |
| 1010 | Template.indent([ |
| 1011 | `${cst} e = new Error("Cannot find module '" + req + "'");`, |
| 1012 | "e.code = 'MODULE_NOT_FOUND';", |
| 1013 | "throw e;" |
| 1014 | ]), |
| 1015 | "}", |
| 1016 | "return map[req];" |
| 1017 | ])}); |
| 1018 | } |
| 1019 | webpackAsyncContext.keys = ${runtimeTemplate.returningFunction( |
| 1020 | "Object.keys(map)" |
| 1021 | )}; |
| 1022 | webpackAsyncContext.resolve = webpackAsyncContextResolve; |
| 1023 | webpackAsyncContext.id = ${JSON.stringify(id)}; |
| 1024 | module.exports = webpackAsyncContext;`; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * @param {AsyncDependenciesBlock} block block |
no test coverage detected