Skip to content

Commit

Permalink
Merge pull request #17301 from webpack/refactor-handle-json-errors
Browse files Browse the repository at this point in the history
refactor: errors and lazy loading
  • Loading branch information
TheLarkInn committed Jun 5, 2023
2 parents c9f32bc + e0c7b6c commit d5397d8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
5 changes: 5 additions & 0 deletions declarations.d.ts
Expand Up @@ -381,6 +381,11 @@ declare module "browserslist" {
export = browserslist;
}

declare module "json-parse-even-better-errors" {
function parseJson(text: string, reviver?: (this: any, key: string, value: any) => any, context?: number): any;
export = parseJson;
}

// TODO remove that when @types/estree is updated
interface ImportAttributeNode {
type: "ImportAttribute";
Expand Down
3 changes: 1 addition & 2 deletions lib/Compiler.js
Expand Up @@ -1011,8 +1011,7 @@ ${other}`);
try {
this.records = parseJson(content.toString("utf-8"));
} catch (e) {
e.message = "Cannot parse records: " + e.message;
return callback(e);
return callback(new Error(`Cannot parse records: ${e.message}`));
}

return callback();
Expand Down
8 changes: 7 additions & 1 deletion lib/Module.js
Expand Up @@ -107,6 +107,7 @@ const makeSerializable = require("./util/makeSerializable");
*/

/** @typedef {KnownBuildMeta & Record<string, any>} BuildMeta */
/** @typedef {Record<string, any>} BuildInfo */

const EMPTY_RESOLVE_OPTIONS = {};

Expand All @@ -116,6 +117,11 @@ const DEFAULT_TYPES_UNKNOWN = new Set(["unknown"]);
const DEFAULT_TYPES_JS = new Set(["javascript"]);

const deprecatedNeedRebuild = util.deprecate(
/**
* @param {Module} module the module
* @param {NeedBuildContext} context context info
* @returns {boolean} true, when rebuild is needed
*/
(module, context) => {
return module.needRebuild(
context.fileSystemInfo.getDeprecatedFileTimestamps(),
Expand Down Expand Up @@ -169,7 +175,7 @@ class Module extends DependenciesBlock {
this._errors = undefined;
/** @type {BuildMeta | undefined} */
this.buildMeta = undefined;
/** @type {Record<string, any> | undefined} */
/** @type {BuildInfo | undefined} */
this.buildInfo = undefined;
/** @type {Dependency[] | undefined} */
this.presentationalDependencies = undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/JsonExportsDependency.js
Expand Up @@ -47,7 +47,7 @@ const getExportsFromData = data => {

class JsonExportsDependency extends NullDependency {
/**
* @param {JsonData=} data json data
* @param {JsonData} data json data
*/
constructor(data) {
super();
Expand Down
4 changes: 2 additions & 2 deletions lib/json/JsonData.js
Expand Up @@ -40,14 +40,14 @@ class JsonData {

/**
* @param {Hash} hash hash to be updated
* @returns {Hash} the updated hash
* @returns {void} the updated hash
*/
updateHash(hash) {
if (this._buffer === undefined && this._data !== undefined) {
this._buffer = Buffer.from(JSON.stringify(this._data));
}

if (this._buffer) return hash.update(this._buffer);
if (this._buffer) hash.update(this._buffer);
}
}

Expand Down
37 changes: 25 additions & 12 deletions lib/json/JsonParser.js
Expand Up @@ -5,16 +5,20 @@

"use strict";

const parseJson = require("json-parse-even-better-errors");
const Parser = require("../Parser");
const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
const memoize = require("../util/memoize");
const JsonData = require("./JsonData");

/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
/** @typedef {import("../Module").BuildInfo} BuildInfo */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */

const getParseJson = memoize(() => require("json-parse-even-better-errors"));

class JsonParser extends Parser {
/**
* @param {JsonModulesPluginParserOptions} options parser options
Expand All @@ -36,17 +40,26 @@ class JsonParser extends Parser {

/** @type {NonNullable<JsonModulesPluginParserOptions["parse"]>} */
const parseFn =
typeof this.options.parse === "function" ? this.options.parse : parseJson;
/** @type {Buffer | RawJsonData} */
const data =
typeof source === "object"
? source
: parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
const jsonData = new JsonData(data);
state.module.buildInfo.jsonData = jsonData;
state.module.buildInfo.strict = true;
state.module.buildMeta.exportsType = "default";
state.module.buildMeta.defaultObject =
typeof this.options.parse === "function"
? this.options.parse
: getParseJson();
/** @type {Buffer | RawJsonData | undefined} */
let data;
try {
data =
typeof source === "object"
? source
: parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
} catch (e) {
throw new Error(`Cannot parse JSON: ${/** @type {Error} */ (e).message}`);
}
const jsonData = new JsonData(/** @type {Buffer | RawJsonData} */ (data));
const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo);
buildInfo.jsonData = jsonData;
buildInfo.strict = true;
const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
buildMeta.exportsType = "default";
buildMeta.defaultObject =
typeof data === "object" ? "redirect-warn" : false;
state.module.addDependency(new JsonExportsDependency(jsonData));
return state;
Expand Down
5 changes: 4 additions & 1 deletion types.d.ts
Expand Up @@ -658,6 +658,9 @@ declare abstract class BasicEvaluatedExpression {
*/
setExpression(expression: NodeEstreeIndex): BasicEvaluatedExpression;
}
declare interface BuildInfo {
[index: string]: any;
}
type BuildMeta = KnownBuildMeta & Record<string, any>;
declare abstract class ByTypeGenerator extends Generator {
map: any;
Expand Down Expand Up @@ -7267,7 +7270,7 @@ declare class Module extends DependenciesBlock {
useSourceMap: boolean;
useSimpleSourceMap: boolean;
buildMeta?: BuildMeta;
buildInfo?: Record<string, any>;
buildInfo?: BuildInfo;
presentationalDependencies?: Dependency[];
codeGenerationDependencies?: Dependency[];
id: string | number;
Expand Down

0 comments on commit d5397d8

Please sign in to comment.