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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: errors and lazy loading #17301

Merged
merged 1 commit into from
Jun 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const getExportsFromData = data => {

class JsonExportsDependency extends NullDependency {
/**
* @param {JsonData=} data json data
* @param {JsonData} data json data
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(data) {
super();
Expand Down
4 changes: 2 additions & 2 deletions lib/json/JsonData.js
Original file line number Diff line number Diff line change
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);
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
if (this._buffer) hash.update(this._buffer);
}
}

Expand Down
37 changes: 25 additions & 12 deletions lib/json/JsonParser.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -7264,7 +7267,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