Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): more types for JSON type #17095

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/json/JsonData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@

const { register } = require("../util/serialization");

/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */

class JsonData {
/**
* @param {Buffer | RawJsonData} data JSON data
*/
constructor(data) {
/** @type {Buffer | undefined} */
this._buffer = undefined;
/** @type {RawJsonData | undefined} */
this._data = undefined;
if (Buffer.isBuffer(data)) {
this._buffer = data;
Expand All @@ -18,13 +28,20 @@ class JsonData {
}
}

/**
* @returns {RawJsonData|undefined} Raw JSON data
*/
get() {
if (this._data === undefined && this._buffer !== undefined) {
this._data = JSON.parse(this._buffer.toString());
}
return this._data;
}

/**
* @param {Hash} hash hash to be updated
* @returns {Hash} the updated hash
*/
updateHash(hash) {
if (this._buffer === undefined && this._data !== undefined) {
this._buffer = Buffer.from(JSON.stringify(this._data));
Expand All @@ -35,12 +52,20 @@ class JsonData {
}

register(JsonData, "webpack/lib/json/JsonData", null, {
/**
* @param {JsonData} obj JSONData object
* @param {ObjectSerializerContext} context context
*/
serialize(obj, { write }) {
if (obj._buffer === undefined && obj._data !== undefined) {
obj._buffer = Buffer.from(JSON.stringify(obj._data));
}
write(obj._buffer);
},
/**
* @param {ObjectDeserializerContext} context context
* @returns {JsonData} deserialized JSON data
*/
deserialize({ read }) {
return new JsonData(read());
}
Expand Down
18 changes: 15 additions & 3 deletions lib/json/JsonGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ const RuntimeGlobals = require("../RuntimeGlobals");
/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
/** @typedef {import("./JsonData")} JsonData */
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */

/**
* @param {RawJsonData} data Raw JSON data
* @returns {undefined|string} stringified data
*/
const stringifySafe = data => {
const stringified = JSON.stringify(data);
if (!stringified) {
Expand All @@ -30,10 +36,10 @@ const stringifySafe = data => {
};

/**
* @param {Object} data data (always an object or array)
* @param {RawJsonData} data Raw JSON data (always an object or array)
* @param {ExportsInfo} exportsInfo exports info
* @param {RuntimeSpec} runtime the runtime
* @returns {Object} reduced data
* @returns {RawJsonData} reduced data
*/
const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused)
Expand All @@ -45,6 +51,7 @@ const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
const used = exportInfo.getUsed(runtime);
if (used === UsageState.Unused) continue;

/** @type {any} */
let value;
if (used === UsageState.OnlyPropertiesUsed && exportInfo.exportsInfo) {
value = createObjectForExportsInfo(
Expand Down Expand Up @@ -86,6 +93,7 @@ const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
: { length: arrayLengthWhenUsed },
reducedData
);
/** @type {number} */
const generatedLength =
arrayLengthWhenUsed !== undefined
? Math.max(arrayLengthWhenUsed, reducedData.length)
Expand Down Expand Up @@ -116,7 +124,8 @@ class JsonGenerator extends Generator {
* @returns {number} estimate size of the module
*/
getSize(module, type) {
let data =
/** @type {RawJsonData | undefined} */
const data =
module.buildInfo &&
module.buildInfo.jsonData &&
module.buildInfo.jsonData.get();
Expand Down Expand Up @@ -148,6 +157,7 @@ class JsonGenerator extends Generator {
concatenationScope
}
) {
/** @type {RawJsonData | undefined} */
const data =
module.buildInfo &&
module.buildInfo.jsonData &&
Expand All @@ -160,6 +170,7 @@ class JsonGenerator extends Generator {
);
}
const exportsInfo = moduleGraph.getExportsInfo(module);
/** @type {RawJsonData} */
let finalJson =
typeof data === "object" &&
data &&
Expand All @@ -172,6 +183,7 @@ class JsonGenerator extends Generator {
jsonStr.length > 20 && typeof finalJson === "object"
? `JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
: jsonStr;
/** @type {string} */
let content;
if (concatenationScope) {
content = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
Expand Down
1 change: 1 addition & 0 deletions lib/json/JsonModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const JsonGenerator = require("./JsonGenerator");
const JsonParser = require("./JsonParser");

/** @typedef {import("../Compiler")} Compiler */
/** @typedef {Record<string, any>} RawJsonData */

const validate = createSchemaValidation(
require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
Expand Down
3 changes: 2 additions & 1 deletion lib/json/JsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const JsonData = require("./JsonData");
/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */

class JsonParser extends Parser {
/**
Expand All @@ -36,7 +37,7 @@ class JsonParser extends Parser {
/** @type {JsonModulesPluginParserOptions["parse"]} */
const parseFn =
typeof this.options.parse === "function" ? this.options.parse : parseJson;

/** @type {Buffer | RawJsonData} */
const data =
typeof source === "object"
? source
Expand Down