Skip to content

Commit bf28379

Browse files
committed
cleanup BundlerCPUProfilerPlugin
1 parent e0e21fc commit bf28379

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ website/i18n/**/*
4747
.netlify
4848

4949
website/rspack-tracing.json
50-
website/rspack-cpu-profile.json
50+
website/bundler-cpu-profile.json

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
"scripts": {
1616
"start": "yarn build:packages && yarn start:website",
1717
"start:website": "yarn workspace website start",
18-
"start:website:profile": "DOCUSAURUS_RSPACK_JS_PROFILE=true DOCUSAURUS_RSPACK_TRACE=true yarn workspace website start",
18+
"start:website:profile": "DOCUSAURUS_BUNDLER_CPU_PROFILE=true DOCUSAURUS_RSPACK_TRACE=true yarn workspace website start",
1919
"start:website:baseUrl": "yarn workspace website start:baseUrl",
2020
"start:website:blogOnly": "yarn workspace website start:blogOnly",
2121
"start:website:deployPreview": "cross-env NETLIFY=true CONTEXT='deploy-preview' yarn workspace website start",
2222
"examples:generate": "node admin/scripts/generateExamples.js",
2323
"build": "yarn build:packages && yarn build:website",
2424
"build:packages": "lerna run build --no-private",
2525
"build:website": "yarn workspace website build",
26-
"build:website:profile": "DOCUSAURUS_RSPACK_JS_PROFILE=true DOCUSAURUS_RSPACK_TRACE=true yarn workspace website build",
26+
"build:website:profile": "DOCUSAURUS_BUNDLER_CPU_PROFILE=true DOCUSAURUS_RSPACK_TRACE=true yarn workspace website build",
2727
"build:website:baseUrl": "yarn workspace website build:baseUrl",
2828
"build:website:blogOnly": "yarn workspace website build:blogOnly",
2929
"build:website:deployPreview:testWrap": "yarn workspace website test:swizzle:wrap:ts",

packages/docusaurus/src/webpack/base.ts

+7-40
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
import fs from 'fs-extra';
99
import path from 'path';
10-
import inspector from 'node:inspector';
1110
import {getCustomBabelConfigFilePath} from '@docusaurus/babel';
1211
import {
12+
createJsLoaderFactory,
1313
getCSSExtractPlugin,
1414
getMinimizers,
15-
createJsLoaderFactory,
1615
} from '@docusaurus/bundler';
1716

18-
import {md5Hash, getFileLoaderUtils} from '@docusaurus/utils';
19-
import {loadThemeAliases, loadDocusaurusAliases} from './aliases';
20-
import type {Configuration, Compiler} from 'webpack';
17+
import {getFileLoaderUtils, md5Hash} from '@docusaurus/utils';
18+
import {loadDocusaurusAliases, loadThemeAliases} from './aliases';
19+
import {BundlerCPUProfilerPlugin} from './plugins/BundlerCPUProfilerPlugin';
20+
import type {Configuration} from 'webpack';
2121
import type {
2222
ConfigureWebpackUtils,
2323
FasterConfig,
@@ -340,41 +340,8 @@ export async function createBaseConfig({
340340
// for more reasoning
341341
ignoreOrder: true,
342342
}),
343-
process.env.DOCUSAURUS_RSPACK_JS_PROFILE &&
344-
new RspackProfileJSCPUProfilePlugin(),
343+
process.env.DOCUSAURUS_BUNDLER_CPU_PROFILE &&
344+
new BundlerCPUProfilerPlugin(),
345345
].filter(Boolean),
346346
};
347347
}
348-
349-
// Bundle CPU profiling plugin contributed by the Rspack team
350-
// Can be opened in https://www.speedscope.app/
351-
// See also https://github.com/jerrykingxyz/docusaurus/pull/1
352-
// See also https://github.com/facebook/docusaurus/pull/10985
353-
class RspackProfileJSCPUProfilePlugin {
354-
output: string;
355-
constructor(output?: string) {
356-
this.output = output ?? './rspack-cpu-profile.json';
357-
}
358-
359-
apply(compiler: Compiler) {
360-
const session = new inspector.Session();
361-
session.connect();
362-
session.post('Profiler.enable');
363-
session.post('Profiler.start');
364-
compiler.hooks.done.tapAsync(
365-
RspackProfileJSCPUProfilePlugin.name,
366-
(_stats, callback) => {
367-
session.post('Profiler.stop', (error, param) => {
368-
if (error) {
369-
console.error('Failed to generate JS CPU profile:', error);
370-
return;
371-
}
372-
fs.writeFile(this.output, JSON.stringify(param.profile)).catch(
373-
console.error,
374-
);
375-
});
376-
return callback();
377-
},
378-
);
379-
}
380-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import inspector from 'node:inspector';
9+
import fs from 'fs-extra';
10+
import type {Compiler} from 'webpack';
11+
12+
// Bundle CPU profiling plugin, contributed by the Rspack team
13+
// Can be opened in https://www.speedscope.app/
14+
// See also https://github.com/jerrykingxyz/docusaurus/pull/1
15+
// See also https://github.com/facebook/docusaurus/pull/10985
16+
export class BundlerCPUProfilerPlugin {
17+
output: string;
18+
19+
constructor(output?: string) {
20+
this.output = output ?? './bundler-cpu-profile.json';
21+
}
22+
23+
apply(compiler: Compiler): void {
24+
const session = new inspector.Session();
25+
session.connect();
26+
session.post('Profiler.enable');
27+
session.post('Profiler.start');
28+
29+
// In dev/watch mode, we restart the profiler before each compilation
30+
compiler.hooks.watchRun.tapPromise(
31+
BundlerCPUProfilerPlugin.name,
32+
async () => {
33+
session.post('Profiler.start');
34+
},
35+
);
36+
37+
compiler.hooks.done.tapPromise(BundlerCPUProfilerPlugin.name, async () => {
38+
session.post('Profiler.stop', (error, param) => {
39+
if (error) {
40+
console.error('Failed to generate JS CPU profile:', error);
41+
return;
42+
}
43+
fs.writeFile(this.output, JSON.stringify(param.profile)).catch(
44+
console.error,
45+
);
46+
});
47+
});
48+
}
49+
}

0 commit comments

Comments
 (0)