Skip to content
14 changes: 14 additions & 0 deletions packages/rspack-test-tools/src/case/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ function createRunner(
await updatePlugin.goNext();
const stats = await compiler.build();
const jsonStats = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
// errorDetails: true
});
await checkStats(jsonStats);
Expand All @@ -294,6 +300,10 @@ function createRunner(
compiler.createCompiler();
const stats = await compiler.build();
const jsonStats = stats.toJson({
assets: true,
chunks: true,
entrypoints: true,
chunkGroups: true,
// errorDetails: true
});
await checkStats(jsonStats);
Expand Down Expand Up @@ -321,6 +331,10 @@ function createRunner(
compiler.createCompiler();
const stats = await compiler.build();
const jsonStats = stats.toJson({
assets: true,
chunks: true,
entrypoints: true,
chunkGroups: true,
// errorDetails: true
});

Expand Down
10 changes: 10 additions & 0 deletions packages/rspack-test-tools/src/case/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export async function check(

if (testConfig.writeStatsJson) {
const jsonStats = stats.toJson({
assets: true,
chunks: true,
entrypoints: true,
chunkGroups: true,
errorDetails: true,
});
fs.writeFileSync(
Expand All @@ -159,6 +163,12 @@ export async function check(
stats.hasWarnings()
) {
const statsJson = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
errorDetails: true,
});
if (statsJson.errors) {
Expand Down
5 changes: 5 additions & 0 deletions packages/rspack-test-tools/src/case/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ function createCompilerProcessor(
expect(typeof stats).toBe('object');
const compilation = stats.compilation;
const statsJson = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
reasons: true,
});
expect(typeof statsJson).toBe('object');
Expand Down
6 changes: 6 additions & 0 deletions packages/rspack-test-tools/src/case/hot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ export function createHotRunner(
throw new Error('Should generate stats during build');
}
const jsonStats = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
// errorDetails: true
});
const compilerOptions = compiler.getOptions();
Expand Down
6 changes: 6 additions & 0 deletions packages/rspack-test-tools/src/case/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export function cachedStats(
return cached;
}
cached = compiler.getStats()!.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
errorDetails: true,
});
return cached;
Expand Down
17 changes: 17 additions & 0 deletions packages/rspack-test-tools/src/case/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ export function createWatchInitialProcessor(
return () => {
if (!cached) {
cached = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
errorDetails: true,
});
}
Expand Down Expand Up @@ -202,6 +208,12 @@ export function createWatchInitialProcessor(
stats.hasWarnings()
) {
const statsJson = stats.toJson({
assets: true,
chunks: true,
chunkModules: true,
modules: true,
entrypoints: true,
chunkGroups: true,
errorDetails: true,
});
if (statsJson.errors) {
Expand Down Expand Up @@ -428,6 +440,11 @@ function cachedWatchStats(
return cached[stepName];
}
cached[stepName] = compiler.getStats()!.toJson({
entrypoints: true,
assets: true,
chunks: true,
chunkModules: true,
modules: true,
errorDetails: true,
});
return cached[stepName];
Expand Down
20 changes: 10 additions & 10 deletions packages/rspack/src/stats/DefaultStatsPresetPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const NAMED_PRESETS: Record<string, StatsOptions> = {
entrypoints: true,
chunkGroups: true,
ids: true,
assets: true,
modules: false,
chunks: true,
chunkRelations: true,
Expand Down Expand Up @@ -60,11 +61,13 @@ const NAMED_PRESETS: Record<string, StatsOptions> = {
detailed: {
hash: true,
builtAt: true,
assets: true,
relatedAssets: true,
entrypoints: true,
chunkGroups: true,
ids: true,
chunks: true,
modules: true,
chunkRelations: true,
chunkModules: false,
chunkOrigins: true,
Expand Down Expand Up @@ -151,6 +154,8 @@ const AUTO_FOR_TO_STRING: StatsFunc = ({ all }, { forToString }) => {
return true;
};

// NOTE: Unlike webpack's defaults, Rspack uses minimal stats defaults to reduce JS-Rust communication overhead.
// The default behavior aligns with the 'errors-warnings' preset for both toString() and toJson() output.
const DEFAULTS: StatsDefault = {
// context: (options, context, compilation) => compilation.compiler.context,
// requestShortener: (options, context, compilation) =>
Expand All @@ -163,14 +168,14 @@ const DEFAULTS: StatsDefault = {
version: NORMAL_ON,
timings: NORMAL_ON,
builtAt: OFF_FOR_TO_STRING,
assets: NORMAL_ON,
entrypoints: AUTO_FOR_TO_STRING,
chunkGroups: OFF_FOR_TO_STRING,
assets: NORMAL_OFF,
entrypoints: NORMAL_OFF,
chunkGroups: NORMAL_OFF,
chunkGroupAuxiliary: OFF_FOR_TO_STRING,
chunkGroupChildren: OFF_FOR_TO_STRING,
chunkGroupMaxAssets: (_, { forToString }) =>
forToString ? 5 : Number.POSITIVE_INFINITY,
chunks: OFF_FOR_TO_STRING,
chunks: NORMAL_OFF,
chunkRelations: OFF_FOR_TO_STRING,
chunkModules: ({ all, modules }) => {
if (all === false) return false;
Expand All @@ -181,12 +186,7 @@ const DEFAULTS: StatsDefault = {
dependentModules: OFF_FOR_TO_STRING,
chunkOrigins: OFF_FOR_TO_STRING,
ids: OFF_FOR_TO_STRING,
modules: ({ all, chunks, chunkModules }, { forToString }) => {
if (all === false) return false;
if (all === true) return true;
if (forToString && chunks && chunkModules) return false;
return true;
},
modules: NORMAL_OFF,
nestedModules: OFF_FOR_TO_STRING,
groupModulesByType: ON_FOR_TO_STRING,
groupModulesByCacheStatus: ON_FOR_TO_STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
{
apply(compiler) {
compiler.hooks.done.tap('TEST', function (stats) {
const { modules } = stats.toJson();
const { modules } = stats.toJson({ modules: true });
compiler.__modules = modules.map((item) => item.identifier);
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
apply(compiler) {
compiler.hooks.done.tap("Test", function (stats) {
let s = stats.toJson({
assets: true
all: true
});
expect(s.assets.some(item => item.name === "a.txt")).toBeTruthy();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/rspack-test/compilerCases/banner-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = [{
});
},
async check() {
const { assets } = lastStats.toJson();
const { assets } = lastStats.toJson({ assets: true });
expect(assets.find(as => as.name === "entry1.js").emitted).toBe(false);
expect(assets.find(as => as.name === "entry2.js").emitted).toBe(true);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/rspack-test/compilerCases/consistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ module.exports = {
if (err) {
throw err
}
stats.push(stat.toJson().assets)
stats.push(stat.toJson({ assets: true }).assets)
compiler.run((_, stat) => {
stats.push(stat.toJson().assets)
stats.push(stat.toJson({ assets: true }).assets)
compiler.run((_, stat) => {
stats.push(stat.toJson().assets)
stats.push(stat.toJson({ assets: true }).assets)
resolve();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
}),
compiler => {
compiler.hooks.afterEmit.tap("PLUGIN", compilation => {
const stats = compilation.getStats().toJson();
const stats = compilation.getStats().toJson({ chunks: true });
const entryChunk = stats.chunks.find(chunk => chunk.entry);
expect(entryChunk.modules[0].name).toBe('external "./external"');
});
Expand Down
2 changes: 1 addition & 1 deletion tests/rspack-test/hashCases/issue-7992/test.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('@rspack/test-tools').THashCaseConfig} */
module.exports = {
validate(stats) {
const assets = stats.stats[0].toJson().assets.map(i => i.name);
const assets = stats.stats[0].toJson({ assets: true }).assets.map(i => i.name);
assets.sort();
expect(assets).toEqual([
"file1.c30068f3cc748ce3.svg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** @type {import('@rspack/test-tools').THashCaseConfig} */
module.exports = {
validate(stats) {
const assets = stats.stats[0].toJson().assets.map(i => i.name);
const assets = stats.stats[0].toJson({ assets: true }).assets.map(i => i.name);
assets.sort();
// `afc10c70ed4ce2b33593` = md4 of src/file.svg
expect(assets).toEqual(["afc10c70ed4ce2b33593.svg", "main.js"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** @type {import("@rspack/test-tools").THashCaseConfig} */
module.exports = {
validate(stats) {
const assets = stats.stats[0].toJson().assets.map(i => i.name);
const assets = stats.stats[0].toJson({ assets: true }).assets.map(i => i.name);
assets.sort();

// `04c785ed39b3beedfad58de4438c4e905d1da406dc6a1b9ed043ebc574819baa` = sha256 of src/file.svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** @type {import("@rspack/test-tools").THashCaseConfig} */
module.exports = {
validate(stats) {
const assets = stats.stats[0].toJson().assets.map(i => i.name);
const assets = stats.stats[0].toJson({ assets: true }).assets.map(i => i.name);
assets.sort();

// `c30068f3cc748ce3` = xxhash64 of src/file.svg
Expand Down
6 changes: 3 additions & 3 deletions tests/rspack-test/multiCompilerCases/lazy-compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ module.exports = {
const [statsA, statsB, statsC] = multiStats.stats;

expect(
statsA.toJson().modules.every(module => {
statsA.toJson({ modules: true }).modules.every(module => {
return !module.identifier.includes("lazy-compilation-proxy");
})
).toBeTruthy();

// second compiler lazy compile entry
expect(
statsB.toJson().modules.find(module => {
statsB.toJson({ modules: true }).modules.find(module => {
return (
module.identifier.includes("lazy-compilation-proxy") &&
module.identifier.replaceAll("\\", "/").includes("/esm/b.js")
Expand All @@ -62,7 +62,7 @@ module.exports = {

// third compiler lazy compile dyn imports
expect(
statsC.toJson().modules.find(module => {
statsC.toJson({ modules: true }).modules.find(module => {
return (
module.identifier.includes("lazy-compilation-proxy") &&
module.identifier
Expand Down
3 changes: 3 additions & 0 deletions tests/rspack-test/statsAPICases/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
},
async check(stats) {
const statsOptions = {
assets: true,
modules: true,
chunks: true,
all: true,
timings: false,
builtAt: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
},
async check(stats) {
const statsOptions = {
modules: true,
runtimeModules: true,
timings: false,
builtAt: false,
Expand Down
1 change: 1 addition & 0 deletions tests/rspack-test/statsAPICases/bundler-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
},
async check(stats) {
const statsOptions = {
modules: true,
runtimeModules: true
};
expect(typeof stats?.hash).toBe("string");
Expand Down
2 changes: 2 additions & 0 deletions tests/rspack-test/statsAPICases/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = {
},
async check(stats) {
const statsChunks = stats?.toJson({
assets: true,
modules: true,
chunks: true,
timings: false,
builtAt: false,
Expand Down
16 changes: 15 additions & 1 deletion tests/rspack-test/statsAPICases/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ module.exports = {
},
async check(stats) {
const statsOptions = {
entrypoints: true,
assets: true,
chunks: true,
chunkModules: true,
chunkGroups: true,
modules: true,
usedExports: true,
providedExports: true,
timings: false,
Expand Down Expand Up @@ -1220,7 +1226,15 @@ module.exports = {
warningsCount: 0,
}
`);
expect(stats?.toString(statsOptions)).toMatchInlineSnapshot(`
expect(stats?.toString({
assets: true,
modules: true,
usedExports: true,
providedExports: true,
timings: false,
builtAt: false,
version: false
})).toMatchInlineSnapshot(`
asset main.js 403 bytes [emitted] (name: main)
orphan modules 192 bytes [orphan] 4 modules
runtime modules 647 bytes 3 modules
Expand Down
1 change: 1 addition & 0 deletions tests/rspack-test/statsAPICases/nested-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
},
async check(stats) {
const statsOptions = {
assets: true,
modules: true,
nestedModules: true,
timings: false,
Expand Down
4 changes: 3 additions & 1 deletion tests/rspack-test/statsAPICases/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class TestPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap("custom", compilation => {
compilation.hooks.optimizeModules.tap("test plugin", () => {
stats = compiler._lastCompilation.getStats().toJson({});
stats = compiler._lastCompilation.getStats().toJson({
entrypoints: true,
});
});
});
}
Expand Down
Loading
Loading