MCPcopy
hub / github.com/webpack/webpack / getAsyncWeakSource

Method getAsyncWeakSource

lib/ContextModule.js:919–971  ·  view source on GitHub ↗

* @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 })

Source from the content-addressed store, hash-verified

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
936function 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}
951function 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}
965webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
966 "Object.keys(map)"
967 )};
968webpackAsyncContext.resolve = webpackAsyncContextResolve;
969webpackAsyncContext.id = ${JSON.stringify(id)};
970module.exports = webpackAsyncContext;`;
971 }
972
973 /**
974 * @param {Dependency[]} dependencies dependencies

Callers 1

getSourceStringMethod · 0.95

Calls 10

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

Tested by

no test coverage detected