Skip to content

Commit 2371ca7

Browse files
authored
perf(bundler): fine-tuning of Webpack/Rspack optimizations (#11176)
1 parent dee76f8 commit 2371ca7

8 files changed

Lines changed: 185 additions & 117 deletions

File tree

packages/docusaurus-bundler/src/currentBundler.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,46 @@ export async function getProgressBarPlugin({
104104

105105
return WebpackBar;
106106
}
107+
108+
export async function registerBundlerTracing({
109+
currentBundler,
110+
}: {
111+
currentBundler: CurrentBundler;
112+
}): Promise<() => Promise<void>> {
113+
if (currentBundler.name === 'rspack') {
114+
const Rspack = await importRspack();
115+
116+
// See https://rspack.dev/contribute/development/profiling
117+
// File can be opened with https://ui.perfetto.dev/
118+
if (process.env.DOCUSAURUS_RSPACK_TRACE) {
119+
// We use the env variable as the "filter" attribute
120+
// See values here: https://rspack.dev/contribute/development/tracing#tracing-filter
121+
let filter = process.env.DOCUSAURUS_RSPACK_TRACE;
122+
123+
if (filter === 'true' || filter === '1') {
124+
// Default value recommended by the Rspack team
125+
// It's also what the CLI uses for the "overview" preset:
126+
// https://github.com/web-infra-dev/rspack/blob/v1.3.10/packages/rspack-cli/src/utils/profile.ts
127+
filter = 'info';
128+
}
129+
130+
await Rspack.experiments.globalTrace.register(
131+
filter,
132+
'chrome',
133+
'./rspack-tracing.json',
134+
);
135+
136+
console.info(`Rspack tracing registered, filter=${filter}`);
137+
138+
return async () => {
139+
await Rspack.experiments.globalTrace.cleanup();
140+
console.log(`Rspack tracing cleaned up, filter=${filter}`);
141+
};
142+
}
143+
}
144+
145+
// We don't support Webpack tracing at the moment
146+
return async () => {
147+
// noop
148+
};
149+
}

packages/docusaurus-bundler/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
getCSSExtractPlugin,
1313
getCopyPlugin,
1414
getProgressBarPlugin,
15+
registerBundlerTracing,
1516
} from './currentBundler';
1617

1718
export {getMinimizers} from './minification';

packages/docusaurus-faster/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"license": "MIT",
2020
"dependencies": {
2121
"@docusaurus/types": "3.7.0",
22-
"@rspack/core": "^1.3.3",
22+
"@rspack/core": "^1.3.10",
2323
"@swc/core": "^1.7.39",
2424
"@swc/html": "^1.7.39",
2525
"browserslist": "^4.24.2",

packages/docusaurus-faster/src/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import browserslist from 'browserslist';
1111
import {minify as swcHtmlMinifier} from '@swc/html';
1212
import type {JsMinifyOptions, Options as SwcOptions} from '@swc/core';
1313

14-
// See https://rspack.dev/contribute/development/profiling
15-
// File can be opened with https://ui.perfetto.dev/
16-
if (process.env.DOCUSAURUS_RSPACK_TRACE) {
17-
Rspack.experiments.globalTrace.register(
18-
'trace',
19-
'chrome',
20-
'./rspack-tracing.json',
21-
);
22-
}
23-
2414
export const swcLoader = require.resolve('swc-loader');
2515

2616
export const getSwcLoaderOptions = ({

packages/docusaurus/src/commands/build/buildLocale.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import fs from 'fs-extra';
99
import path from 'path';
1010
import _ from 'lodash';
11-
import {compile} from '@docusaurus/bundler';
11+
import {compile, registerBundlerTracing} from '@docusaurus/bundler';
1212
import logger, {PerfLogger} from '@docusaurus/logger';
1313
import {loadSite} from '../../server/site';
1414
import {handleBrokenLinks} from '../../server/brokenLinks';
@@ -35,6 +35,7 @@ export type BuildLocaleParams = {
3535
};
3636

3737
const SkipBundling = process.env.DOCUSAURUS_SKIP_BUNDLING === 'true';
38+
const ExitAfterBundling = process.env.DOCUSAURUS_EXIT_AFTER_BUNDLING === 'true';
3839

3940
export async function buildLocale({
4041
siteDir,
@@ -93,6 +94,9 @@ export async function buildLocale({
9394
`Skipping the Docusaurus bundling step because DOCUSAURUS_SKIP_BUNDLING='true'`,
9495
);
9596
} else {
97+
const cleanupBundlerTracing = await registerBundlerTracing({
98+
currentBundler: props.currentBundler,
99+
});
96100
// Run webpack to build JS bundle (client) and static html files (server).
97101
await PerfLogger.async(`Bundling with ${props.currentBundler.name}`, () => {
98102
return compile({
@@ -102,6 +106,10 @@ export async function buildLocale({
102106
currentBundler: configureWebpackUtils.currentBundler,
103107
});
104108
});
109+
await cleanupBundlerTracing();
110+
}
111+
if (ExitAfterBundling) {
112+
return process.exit(0);
105113
}
106114

107115
const {collectedData} = await PerfLogger.async('SSG', () =>

packages/docusaurus/src/webpack/base.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ export async function createBaseConfig({
249249
modules: ['node_modules', path.join(siteDir, 'node_modules')],
250250
},
251251
optimization: {
252+
// The optimizations.concatenateModules is expensive
253+
// - On the server, it's not useful to run it at all
254+
// - On the client, it leads to a ~3% JS assets total size decrease
255+
// Let's keep it by default, but large sites may prefer faster builds
256+
// See also https://github.com/facebook/docusaurus/pull/11176
257+
concatenateModules: !isServer,
258+
259+
// The optimizations.mergeDuplicateChunks is expensive
260+
// - On the server, it's not useful to run it at all
261+
// - On the client, we compared assets/js before/after and see 0 change
262+
// `du -sk js-before js-after` => the JS assets have the exact same size
263+
// See also https://github.com/facebook/docusaurus/pull/11176
264+
mergeDuplicateChunks: true,
265+
252266
// Only minimize client bundle in production because server bundle is only
253267
// used for static site generation
254268
minimize: minimizeEnabled,

website/docusaurus.config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,19 @@ export default async function createConfigAsync() {
164164
url: 'https://docusaurus.io',
165165
future: {
166166
v4: !isSlower, // Not accurate, but good enough
167-
experimental_faster: !isSlower,
167+
experimental_faster: isSlower
168+
? false
169+
: {
170+
// Verbose object: easier to independently test single attributes
171+
swcJsLoader: true,
172+
swcJsMinimizer: true,
173+
swcHtmlMinimizer: true,
174+
lightningCssMinimizer: true,
175+
mdxCrossCompilerCache: true,
176+
rspackBundler: true,
177+
rspackPersistentCache: true,
178+
ssgWorkerThreads: true,
179+
},
168180
experimental_storage: {
169181
namespace: true,
170182
},

0 commit comments

Comments
 (0)