MCPcopy
hub / github.com/webpack/webpack / codeGeneration

Method codeGeneration

lib/container/FallbackModule.js:148–180  ·  view source on GitHub ↗

* Generates code and runtime requirements for this module. * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result

({ runtimeTemplate, moduleGraph, chunkGraph })

Source from the content-addressed store, hash-verified

146 * @returns {CodeGenerationResult} result
147 */
148 codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
149 const ids = this.dependencies.map((dep) =>
150 chunkGraph.getModuleId(/** @type {Module} */ (moduleGraph.getModule(dep)))
151 );
152 const cst = runtimeTemplate.renderConst();
153 const lt = runtimeTemplate.renderLet();
154 const code = Template.asString([
155 `${cst} ids = ${JSON.stringify(ids)};`,
156 `${lt} error, result, i = 0;`,
157 `${cst} loop = ${runtimeTemplate.basicFunction("next", [
158 "while(i < ids.length) {",
159 Template.indent([
160 `try { next = ${RuntimeGlobals.require}(ids[i++]); } catch(e) { return handleError(e); }`,
161 "if(next) return next.then ? next.then(handleResult, handleError) : handleResult(next);"
162 ]),
163 "}",
164 "if(error) throw error;"
165 ])}`,
166 `${cst} handleResult = ${runtimeTemplate.basicFunction("result", [
167 "if(result) return result;",
168 "return loop();"
169 ])};`,
170 `${cst} handleError = ${runtimeTemplate.basicFunction("e", [
171 "error = e;",
172 "return loop();"
173 ])};`,
174 "module.exports = loop();"
175 ]);
176 /** @type {Sources} */
177 const sources = new Map();
178 sources.set(JAVASCRIPT_TYPE, new RawSource(code));
179 return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS };
180 }
181
182 /**
183 * Serializes this instance into the provided serializer context.

Callers

nothing calls this directly

Calls 8

getModuleIdMethod · 0.80
renderConstMethod · 0.80
renderLetMethod · 0.80
basicFunctionMethod · 0.80
indentMethod · 0.80
getModuleMethod · 0.45
asStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected