MCPcopy
hub / github.com/webpack/webpack / getEagerSource

Method getEagerSource

lib/ContextModule.js:982–1025  ·  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

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
1002function webpackAsyncContext(req) {
1003 return webpackAsyncContextResolve(req).then(${thenFunction});
1004}
1005function 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}
1019webpackAsyncContext.keys = ${runtimeTemplate.returningFunction(
1020 "Object.keys(map)"
1021 )};
1022webpackAsyncContext.resolve = webpackAsyncContextResolve;
1023webpackAsyncContext.id = ${JSON.stringify(id)};
1024module.exports = webpackAsyncContext;`;
1025 }
1026
1027 /**
1028 * @param {AsyncDependenciesBlock} block block

Callers 1

getSourceStringMethod · 0.95

Calls 10

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

Tested by

no test coverage detected