* @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 })
| 917 | * @returns {string} source code |
| 918 | */ |
| 919 | getAsyncWeakSource(dependencies, id, phase, { chunkGraph, runtimeTemplate }) { |
| 920 | const map = this.getUserRequestMap(dependencies, chunkGraph); |
| 921 | const fakeMap = this.getFakeMap(dependencies, chunkGraph); |
| 922 | const asyncDepsMap = |
| 923 | ImportPhaseUtils.isDefer(phase) && |
| 924 | this.getModuleDeferredAsyncDepsMap(dependencies, chunkGraph); |
| 925 | const returnModuleObject = this.getReturnModuleObjectSource( |
| 926 | fakeMap, |
| 927 | true, |
| 928 | asyncDepsMap ? "asyncDepsMap[id]" : undefined |
| 929 | ); |
| 930 | const cst = runtimeTemplate.renderConst(); |
| 931 | |
| 932 | return `${cst} map = ${JSON.stringify(map, null, "\t")}; |
| 933 | ${this.getFakeMapInitStatement(fakeMap, runtimeTemplate)} |
| 934 | ${this.getModuleDeferredAsyncDepsMapInitStatement(asyncDepsMap, runtimeTemplate)} |
| 935 | |
| 936 | function webpackAsyncContext(req) { |
| 937 | return webpackAsyncContextResolve(req).then(${runtimeTemplate.basicFunction( |
| 938 | "id", |
| 939 | [ |
| 940 | `if(!${RuntimeGlobals.moduleFactories}[id]) {`, |
| 941 | Template.indent([ |
| 942 | `${cst} e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");`, |
| 943 | "e.code = 'MODULE_NOT_FOUND';", |
| 944 | "throw e;" |
| 945 | ]), |
| 946 | "}", |
| 947 | `return ${returnModuleObject};` |
| 948 | ] |
| 949 | )}); |
| 950 | } |
| 951 | function webpackAsyncContextResolve(req) { |
| 952 | // Here Promise.resolve().then() is used instead of new Promise() to prevent |
| 953 | // uncaught exception popping up in devtools |
| 954 | return Promise.resolve().then(${runtimeTemplate.basicFunction("", [ |
| 955 | `if(!${RuntimeGlobals.hasOwnProperty}(map, req)) {`, |
| 956 | Template.indent([ |
| 957 | `${cst} e = new Error("Cannot find module '" + req + "'");`, |
| 958 | "e.code = 'MODULE_NOT_FOUND';", |
| 959 | "throw e;" |
| 960 | ]), |
| 961 | "}", |
| 962 | "return map[req];" |
| 963 | ])}); |
| 964 | } |
| 965 | webpackAsyncContext.keys = ${runtimeTemplate.returningFunction( |
| 966 | "Object.keys(map)" |
| 967 | )}; |
| 968 | webpackAsyncContext.resolve = webpackAsyncContextResolve; |
| 969 | webpackAsyncContext.id = ${JSON.stringify(id)}; |
| 970 | module.exports = webpackAsyncContext;`; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * @param {Dependency[]} dependencies dependencies |
no test coverage detected