Skip to content

Commit 3e5f9ec

Browse files
authored
perf: skip assets traversal if source map is disabled (#6457)
1 parent f221010 commit 3e5f9ec

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

e2e/cases/source-map/multi-environments/index.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
13
import { expect, findFile, rspackTest } from '@e2e/helper';
24

35
rspackTest(
@@ -12,12 +14,16 @@ rspackTest(
1214
path.includes('static/js/index.js.map') && !path.includes('web2'),
1315
);
1416
const web2JsMapPath = findFile(files, 'web2/static/js/index.js.map');
17+
const sourceContent = readFileSync(
18+
join(__dirname, './src/index.js'),
19+
'utf-8',
20+
);
1521

1622
expect(JSON.parse(files[web1JsMapPath])).toEqual({
1723
version: 3,
1824
file: 'static/js/index.js',
1925
sources: ['../../../src/index.js'],
20-
sourcesContent: ["console.log('hello');\n"],
26+
sourcesContent: [sourceContent],
2127
names: ['console'],
2228
// cspell:disable-next-line
2329
mappings: 'AAAAA,QAAQ,GAAG,CAAC',
@@ -26,7 +32,7 @@ rspackTest(
2632
version: 3,
2733
file: 'static/js/index.js',
2834
sources: ['../../../../../src/index.js'],
29-
sourcesContent: ["console.log('hello');\n"],
35+
sourcesContent: [sourceContent],
3036
names: ['console'],
3137
// cspell:disable-next-line
3238
mappings: 'AAAAA,QAAQ,GAAG,CAAC',

packages/core/src/plugins/sourceMap.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const pluginSourceMap = (): RsbuildPlugin => ({
3232
absoluteResourcePath: string;
3333
}) => absoluteResourcePath;
3434

35+
const enableCssSourceMap = (config: NormalizedEnvironmentConfig) => {
36+
const { sourceMap } = config.output;
37+
return typeof sourceMap === 'object' && sourceMap.css;
38+
};
39+
3540
api.modifyBundlerChain((chain, { bundler, environment }) => {
3641
const { config } = environment;
3742
const devtool = getDevtool(config);
@@ -42,8 +47,7 @@ export const pluginSourceMap = (): RsbuildPlugin => ({
4247

4348
// When JS source map is disabled, but CSS source map is enabled,
4449
// add `SourceMapDevToolPlugin` to let Rspack generate CSS source map.
45-
const { sourceMap } = config.output;
46-
if (!devtool && typeof sourceMap === 'object' && sourceMap.css) {
50+
if (!devtool && enableCssSourceMap(config)) {
4751
chain.plugin('source-map-css').use(bundler.SourceMapDevToolPlugin, [
4852
{
4953
test: /\.css$/,
@@ -61,6 +65,14 @@ export const pluginSourceMap = (): RsbuildPlugin => ({
6165
// Source maps has been extracted to separate files on this stage
6266
{ stage: 'optimize-transfer' },
6367
({ assets, compilation, sources, environment }) => {
68+
// If source map is disabled, skip the processing.
69+
if (
70+
!compilation.options.devtool &&
71+
!enableCssSourceMap(environment.config)
72+
) {
73+
return;
74+
}
75+
6476
// If devtoolModuleFilenameTemplate is not the default template,
6577
// which means users want to customize it, skip the default processing.
6678
if (

0 commit comments

Comments
 (0)