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

Method generate

lib/json/JsonGenerator.js:178–230  ·  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,
		{
			moduleGraph,
			runtimeTemplate,
			runtimeRequirements,
			runtime,
			concatenationScope
		}
	)

Source from the content-addressed store, hash-verified

176 * @returns {Source | null} generated code
177 */
178 generate(
179 module,
180 {
181 moduleGraph,
182 runtimeTemplate,
183 runtimeRequirements,
184 runtime,
185 concatenationScope
186 }
187 ) {
188 /** @type {JsonValue | undefined} */
189 const data =
190 module.buildInfo &&
191 module.buildInfo.jsonData &&
192 module.buildInfo.jsonData.get();
193 if (data === undefined) {
194 return new RawSource(
195 runtimeTemplate.missingModuleStatement({
196 request: module.rawRequest
197 })
198 );
199 }
200 const exportsInfo = moduleGraph.getExportsInfo(module);
201 /** @type {JsonValue} */
202 const finalJson =
203 typeof data === "object" &&
204 data &&
205 exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused
206 ? createObjectForExportsInfo(data, exportsInfo, runtime)
207 : data;
208 // Use JSON because JSON.parse() is much faster than JavaScript evaluation
209 const jsonStr = /** @type {string} */ (stringifySafe(finalJson));
210 const jsonExpr =
211 this.options.JSONParse &&
212 jsonStr.length > 20 &&
213 typeof finalJson === "object"
214 ? `/*#__PURE__*/JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
215 : jsonStr.replace(/"__proto__":/g, '["__proto__"]:');
216 /** @type {string} */
217 let content;
218 if (concatenationScope) {
219 content = `${runtimeTemplate.renderConst()} ${
220 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
221 } = ${jsonExpr};`;
222 concatenationScope.registerNamespaceExport(
223 ConcatenationScope.NAMESPACE_OBJECT_EXPORT
224 );
225 } else {
226 runtimeRequirements.add(RuntimeGlobals.module);
227 content = `${module.moduleArgument}.exports = ${jsonExpr};`;
228 }
229 return new RawSource(content);
230 }
231
232 /**
233 * Generates fallback output for the provided error condition.

Callers

nothing calls this directly

Calls 10

stringifySafeFunction · 0.85
getExportsInfoMethod · 0.80
renderConstMethod · 0.80
getMethod · 0.45
getUsedMethod · 0.45
replaceMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected