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

Method generate

lib/asset/AssetBytesGenerator.js:49–101  ·  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 encodedSource = originalSource.buffer().toString("base64");
63
64 runtimeRequirements.add(RuntimeGlobals.requireScope);
65 runtimeRequirements.add(RuntimeGlobals.toBinary);
66
67 /** @type {string} */
68 let sourceContent;
69 if (concatenationScope) {
70 concatenationScope.registerNamespaceExport(
71 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
72 );
73 sourceContent = `${runtimeTemplate.renderConst()} ${
74 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
75 } = ${RuntimeGlobals.toBinary}(${JSON.stringify(encodedSource)});`;
76 } else {
77 runtimeRequirements.add(RuntimeGlobals.module);
78 sourceContent = `${module.moduleArgument}.exports = ${RuntimeGlobals.toBinary}(${JSON.stringify(
79 encodedSource
80 )});`;
81 }
82 return new RawSource(sourceContent);
83 }
84 case ASSET_URL_TYPE: {
85 if (!originalSource) {
86 return null;
87 }
88
89 const encodedSource = originalSource.buffer().toString("base64");
90
91 if (data) {
92 data.set("url", {
93 [type]: `data:application/octet-stream;base64,${encodedSource}`
94 });
95 }
96 return null;
97 }
98 default:
99 return null;
100 }
101 }
102
103 /**
104 * Generates fallback output for the provided error condition.

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected