Skip to content

Commit

Permalink
refactor: use module.parser.json.exportsDepth
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-x committed Dec 13, 2024
1 parent 38df65d commit 3de7b0d
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 33 deletions.
4 changes: 4 additions & 0 deletions declarations/plugins/JsonModulesPluginParser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/

export interface JsonModulesPluginParserOptions {
/**
* The depth of json dependency flagged as `exportInfo`.
*/
exportsDepth?: number;
/**
* Function that executes for a module source string and should return json-compatible data.
*/
Expand Down
6 changes: 1 addition & 5 deletions lib/WebpackOptionsApply.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class WebpackOptionsApply extends OptionsApply {
compiler.recordsOutputPath = options.recordsOutputPath || null;
compiler.name = options.name;

const development = options.mode === "development";

if (options.externals) {
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const ExternalsPlugin = require("./ExternalsPlugin");
Expand Down Expand Up @@ -292,9 +290,7 @@ class WebpackOptionsApply extends OptionsApply {
}

new JavascriptModulesPlugin().apply(compiler);
new JsonModulesPlugin({
depth: development ? 1 : Infinity
}).apply(compiler);
new JsonModulesPlugin().apply(compiler);
new AssetModulesPlugin().apply(compiler);

if (!options.experiments.outputModule) {
Expand Down
13 changes: 11 additions & 2 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
futureDefaults,
isNode: targetProperties && targetProperties.node === true,
uniqueName: options.output.uniqueName,
targetProperties
targetProperties,
mode: options.mode
});

applyExternalsPresetsDefaults(options.externalsPresets, {
Expand Down Expand Up @@ -609,6 +610,7 @@ const applyCssGeneratorOptionsDefaults = (
* @param {string} options.uniqueName the unique name
* @param {boolean} options.isNode is node target platform
* @param {TargetProperties | false} options.targetProperties target properties
* @param {Mode} options.mode mode
* @returns {void}
*/
const applyModuleDefaults = (
Expand All @@ -621,7 +623,8 @@ const applyModuleDefaults = (
futureDefaults,
isNode,
uniqueName,
targetProperties
targetProperties,
mode
}
) => {
if (cache) {
Expand Down Expand Up @@ -663,6 +666,12 @@ const applyModuleDefaults = (
}

F(module.parser, "javascript", () => ({}));
F(module.parser, JSON_MODULE_TYPE, () => ({}));
D(
module.parser[JSON_MODULE_TYPE],
"exportsDepth",
mode === "development" ? 1 : Infinity
);

applyJavascriptParserOptionsDefaults(
/** @type {NonNullable<ParserOptionsByModuleTypeKnown["javascript"]>} */
Expand Down
16 changes: 8 additions & 8 deletions lib/dependencies/JsonExportsDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const NullDependency = require("./NullDependency");
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/Hash")} Hash */

/** @typedef {{depth:number}} JsonDependencyOptions */
/** @typedef {{exportsDepth:number}} JsonDependencyOptions */

/**
* @param {number} depth depth
* @param {number} exportsDepth exportsDepth
* @returns {((data: RawJsonData, curDepth?: number) => ExportSpec[] | undefined)} value
*/
const getExportsWithDepth = depth =>
const getExportsWithDepth = exportsDepth =>
function getExportsFromData(data, curDepth = 1) {
if (curDepth > depth) return undefined;
if (curDepth > exportsDepth) return undefined;
if (data && typeof data === "object") {
if (Array.isArray(data)) {
return data.length < 100
Expand All @@ -54,12 +54,12 @@ const getExportsWithDepth = depth =>
class JsonExportsDependency extends NullDependency {
/**
* @param {JsonData} data json data
* @param {JsonDependencyOptions} dependencyOptions dependency options
* @param {JsonDependencyOptions} options options
*/
constructor(data, dependencyOptions) {
constructor(data, options) {
super();
this.data = data;
this.options = dependencyOptions;
this.options = options;
}

get type() {
Expand All @@ -73,7 +73,7 @@ class JsonExportsDependency extends NullDependency {
*/
getExports(moduleGraph) {
return {
exports: getExportsWithDepth(this.options.depth)(
exports: getExportsWithDepth(this.options.exportsDepth)(
this.data && /** @type {RawJsonData} */ (this.data.get())
),
dependencies: undefined
Expand Down
11 changes: 1 addition & 10 deletions lib/json/JsonModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const JsonGenerator = require("./JsonGenerator");
const JsonParser = require("./JsonParser");

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

const validate = createSchemaValidation(
Expand All @@ -30,13 +29,6 @@ const PLUGIN_NAME = "JsonModulesPlugin";
* It adds the json module type to the compiler and registers the json parser and generator.
*/
class JsonModulesPlugin {
/**
* @param {JsonDependencyOptions} dependencyOptions dependency options
*/
constructor(dependencyOptions) {
this.dependencyOptions = dependencyOptions;
}

/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
Expand All @@ -50,8 +42,7 @@ class JsonModulesPlugin {
.for(JSON_MODULE_TYPE)
.tap(PLUGIN_NAME, parserOptions => {
validate(parserOptions);

return new JsonParser(parserOptions, this.dependencyOptions);
return new JsonParser(parserOptions);
});
normalModuleFactory.hooks.createGenerator
.for(JSON_MODULE_TYPE)
Expand Down
13 changes: 6 additions & 7 deletions lib/json/JsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ const JsonData = require("./JsonData");
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
/** @typedef {import("../dependencies/JsonExportsDependency").JsonDependencyOptions} JsonDependencyOptions */
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */

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

class JsonParser extends Parser {
/**
* @param {JsonModulesPluginParserOptions} parserOptions parser options
* @param {JsonDependencyOptions} dependencyOptions dependency options
* @param {JsonModulesPluginParserOptions} options parser options
*/
constructor(parserOptions, dependencyOptions) {
constructor(options) {
super();
this.options = parserOptions || {};
this.dependencyOptions = dependencyOptions;
this.options = options || {};
}

/**
Expand Down Expand Up @@ -67,7 +64,9 @@ class JsonParser extends Parser {
buildMeta.defaultObject =
typeof data === "object" ? "redirect-warn" : false;
state.module.addDependency(
new JsonExportsDependency(jsonData, this.dependencyOptions)
new JsonExportsDependency(jsonData, {
exportsDepth: this.options.exportsDepth
})
);
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/JsonModulesPluginParser.check.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions schemas/plugins/JsonModulesPluginParser.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"type": "object",
"additionalProperties": false,
"properties": {
"exportsDepth": {
"description": "The depth of json dependency flagged as `exportInfo`.",
"type": "number"
},
"parse": {
"description": "Function that executes for a module source string and should return json-compatible data.",
"instanceof": "Function",
Expand Down
9 changes: 9 additions & 0 deletions test/Defaults.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ describe("snapshots", () => {
"wrappedContextRecursive": true,
"wrappedContextRegExp": /\\.\\*/,
},
"json": Object {
"exportsDepth": Infinity,
},
},
"rules": Array [],
"unsafeCache": false,
Expand Down Expand Up @@ -845,6 +848,9 @@ describe("snapshots", () => {
- "mode": "none",
+ "mode": "development",
@@ ... @@
- "exportsDepth": Infinity,
+ "exportsDepth": 1,
@@ ... @@
- "unsafeCache": false,
+ "unsafeCache": [Function anonymous],
@@ ... @@
Expand Down Expand Up @@ -1905,6 +1911,9 @@ describe("snapshots", () => {
- "mode": "none",
+ "mode": "development",
@@ ... @@
- "exportsDepth": Infinity,
+ "exportsDepth": 1,
@@ ... @@
- "unsafeCache": false,
+ "unsafeCache": [Function anonymous],
@@ ... @@
Expand Down
27 changes: 27 additions & 0 deletions test/configCases/json/bailout-flag-dep-export-perf/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"depth_1": {
"depth_2": {
"depth_3": {
"depth_4": {
"depth_5": {
"depth_6": "depth_6"
}
}
}
}
},
"_depth_1": {
"_depth_2": {
"_depth_3": {
"_depth_4": {
"_depth_5": {
"_depth_6": "_depth_6"
}
}
}
}
},
"__depth_1": [
{ "__depth_3": [{ "__depth_5": [{ "__depth_7": ["__depth_8"] }] }] }
]
}
11 changes: 11 additions & 0 deletions test/configCases/json/bailout-flag-dep-export-perf/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './data.json';

it("should compile and run", () => {
expect(__webpack_exports_info__.depth_1.provideInfo).toBe(true)
expect(__webpack_exports_info__._depth_1.provideInfo).toBe(true)
expect(__webpack_exports_info__.__depth_1.provideInfo).toBe(true)

expect(__webpack_exports_info__.depth_1.depth_2.provideInfo).toBe(true)
expect(__webpack_exports_info__._depth_1._depth_2._depth_3._depth_4.provideInfo).toBe(true)
expect(__webpack_exports_info__.__depth_1[0].__depth_3[0].__depth_5.provideInfo).toBe(true)
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
module: {
parser: {
json: {
exportsDepth: Infinity
}
}
}
};

0 comments on commit 3de7b0d

Please sign in to comment.