refactor: errors and lazy loading

This commit is contained in:
alexander.akait 2023-06-01 22:44:37 +03:00
parent 6104d28ddd
commit e0c7b6c48f
7 changed files with 45 additions and 19 deletions

5
declarations.d.ts vendored
View File

@ -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";

View File

@ -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();

View File

@ -107,6 +107,7 @@ const makeSerializable = require("./util/makeSerializable");
*/
/** @typedef {KnownBuildMeta & Record<string, any>} BuildMeta */
/** @typedef {Record<string, any>} BuildInfo */
const EMPTY_RESOLVE_OPTIONS = {};
@ -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(),
@ -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;

View File

@ -47,7 +47,7 @@ const getExportsFromData = data => {
class JsonExportsDependency extends NullDependency {
/**
* @param {JsonData=} data json data
* @param {JsonData} data json data
*/
constructor(data) {
super();

View File

@ -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);
}
}

View File

@ -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
@ -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;

5
types.d.ts vendored
View File

@ -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;
@ -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;