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

Method codeGeneration

lib/hmr/LazyCompilationPlugin.js:364–441  ·  view source on GitHub ↗

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

({ runtimeTemplate, chunkGraph, moduleGraph })

Source from the content-addressed store, hash-verified

362 * @returns {CodeGenerationResult} result
363 */
364 codeGeneration({ runtimeTemplate, chunkGraph, moduleGraph }) {
365 /** @type {Sources} */
366 const sources = new Map();
367 /** @type {RuntimeRequirements} */
368 const runtimeRequirements = new Set();
369 runtimeRequirements.add(RuntimeGlobals.module);
370 const clientDep = /** @type {CommonJsRequireDependency} */ (
371 this.dependencies[0]
372 );
373 const clientModule = moduleGraph.getModule(clientDep);
374 const block = this.blocks[0];
375 const cst = runtimeTemplate.renderConst();
376 const lt = runtimeTemplate.renderLet();
377 const client = Template.asString([
378 `${cst} client = ${runtimeTemplate.moduleExports({
379 module: clientModule,
380 chunkGraph,
381 request: clientDep.userRequest,
382 runtimeRequirements
383 })}`,
384 `${cst} data = ${JSON.stringify(this.data)};`
385 ]);
386 const keepActive = Template.asString([
387 `${cst} dispose = client.keepAlive({ data: data, active: ${JSON.stringify(
388 Boolean(block)
389 )}, module: module, onError: onError });`
390 ]);
391 /** @type {string} */
392 let source;
393 if (block) {
394 const dep = block.dependencies[0];
395 const module = /** @type {Module} */ (moduleGraph.getModule(dep));
396 source = Template.asString([
397 client,
398 `module.exports = ${runtimeTemplate.moduleNamespacePromise({
399 chunkGraph,
400 block,
401 module,
402 request: this.request,
403 dependency: dep,
404 strict: false, // TODO this should be inherited from the original module
405 message: "import()",
406 runtimeRequirements
407 })};`,
408 "if (module.hot) {",
409 Template.indent([
410 "module.hot.accept();",
411 `module.hot.accept(${JSON.stringify(
412 chunkGraph.getModuleId(module)
413 )}, function() { module.hot.invalidate(); });`,
414 "module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });",
415 `if (${runtimeTemplate.optionalChaining("module.hot.data", "resolveSelf")}) module.hot.data.resolveSelf(module.exports);`
416 ]),
417 "}",
418 "function onError() { /* ignore */ }",
419 keepActive
420 ]);
421 } else {

Callers

nothing calls this directly

Calls 11

renderConstMethod · 0.80
renderLetMethod · 0.80
moduleExportsMethod · 0.80
indentMethod · 0.80
getModuleIdMethod · 0.80
optionalChainingMethod · 0.80
addMethod · 0.45
getModuleMethod · 0.45
asStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected