webpack/lib/json/JsonGenerator.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const { ConcatSource, RawSource } = require("webpack-sources");
2019-10-11 21:46:57 +08:00
const Generator = require("../Generator");
const { UsageState } = require("../ModuleGraph");
const RuntimeGlobals = require("../RuntimeGlobals");
/** @typedef {import("webpack-sources").Source} Source */
2019-10-11 21:46:57 +08:00
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../NormalModule")} NormalModule */
2018-04-25 06:35:58 +08:00
const stringifySafe = data => {
const stringified = JSON.stringify(data);
if (!stringified) {
return undefined; // Invalid JSON
}
2019-02-05 17:06:32 +08:00
return stringified.replace(/\u2028|\u2029/g, str =>
str === "\u2029" ? "\\u2029" : "\\u2028"
2018-02-25 09:00:20 +08:00
); // invalid in JavaScript but valid JSON
2018-04-25 06:35:58 +08:00
};
const TYPES = new Set(["javascript"]);
class JsonGenerator extends Generator {
/**
* @returns {Set<string>} available types (do not mutate)
*/
getTypes() {
return TYPES;
}
/**
* @param {NormalModule} module the module
* @param {string=} type source type
* @returns {number} estimate size of the module
*/
getSize(module, type) {
let data = module.buildInfo.jsonData;
if (!data) return 0;
return stringifySafe(data).length + 10;
}
/**
* @param {NormalModule} module module for which the code should be generated
* @param {GenerateContext} generateContext context for generate
* @returns {Source} generated code
*/
2018-11-17 01:18:44 +08:00
generate(module, { moduleGraph, runtimeTemplate, runtimeRequirements }) {
const source = new ConcatSource();
const data = module.buildInfo.jsonData;
if (data === undefined) {
return new RawSource(
runtimeTemplate.missingModuleStatement({
request: module.rawRequest
})
);
}
2018-11-17 01:18:44 +08:00
runtimeRequirements.add(RuntimeGlobals.module);
const providedExports = moduleGraph.getProvidedExports(module);
2019-07-02 17:17:52 +08:00
let finalJson;
2018-02-25 09:00:20 +08:00
if (
Array.isArray(providedExports) &&
module.isExportUsed(moduleGraph, "default") === UsageState.Unused
2018-02-25 09:00:20 +08:00
) {
// Only some exports are used: We can optimize here, by only generating a part of the JSON
const reducedJson = {};
for (const exportName of providedExports) {
2018-02-25 09:00:20 +08:00
if (exportName === "default") continue;
const used = module.getUsedName(moduleGraph, exportName);
if (typeof used === "string") {
reducedJson[used] = data[exportName];
}
}
2019-07-02 17:17:52 +08:00
finalJson = reducedJson;
} else {
2019-07-02 17:17:52 +08:00
finalJson = data;
}
2019-07-02 17:17:52 +08:00
// Use JSON because JSON.parse() is much faster than JavaScript evaluation
const jsonSource = JSON.stringify(stringifySafe(finalJson));
const jsonExpr = `JSON.parse(${jsonSource})`;
source.add(`${module.moduleArgument}.exports = ${jsonExpr};`);
return source;
}
}
module.exports = JsonGenerator;