MCPcopy
hub / github.com/webpack/webpack / generate

Method generate

lib/asset/AssetSourceGenerator.js:49–100  ·  view source on GitHub ↗

* Generates generated code for this runtime module. * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate * @returns {Source | null} generated code

(
		module,
		{ type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
	)

Source from the content-addressed store, hash-verified

47 * @returns {Source | null} generated code
48 */
49 generate(
50 module,
51 { type, concatenationScope, getData, runtimeTemplate, runtimeRequirements }
52 ) {
53 const originalSource = module.originalSource();
54 const data = getData ? getData() : undefined;
55
56 switch (type) {
57 case JAVASCRIPT_TYPE: {
58 if (!originalSource) {
59 return new RawSource("");
60 }
61
62 const content = originalSource.source();
63 const encodedSource =
64 typeof content === "string" ? content : content.toString("utf8");
65
66 /** @type {string} */
67 let sourceContent;
68 if (concatenationScope) {
69 concatenationScope.registerNamespaceExport(
70 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
71 );
72 sourceContent = `${runtimeTemplate.renderConst()} ${
73 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
74 } = ${JSON.stringify(encodedSource)};`;
75 } else {
76 runtimeRequirements.add(RuntimeGlobals.module);
77 sourceContent = `${module.moduleArgument}.exports = ${JSON.stringify(
78 encodedSource
79 )};`;
80 }
81 return new RawSource(sourceContent);
82 }
83 case ASSET_URL_TYPE: {
84 if (!originalSource) {
85 return null;
86 }
87
88 const content = originalSource.source();
89 const encodedSource =
90 typeof content === "string" ? content : content.toString("utf8");
91
92 if (data) {
93 data.set("url", { [type]: encodedSource });
94 }
95 return null;
96 }
97 default:
98 return null;
99 }
100 }
101
102 /**
103 * Generates fallback output for the provided error condition.

Callers

nothing calls this directly

Calls 7

sourceMethod · 0.80
renderConstMethod · 0.80
originalSourceMethod · 0.45
toStringMethod · 0.45
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected