Skip to content

Commit 92060f6

Browse files
authored
fix: add info for banner plugin when drop_console is true (#1105)
1 parent 87934dc commit 92060f6

File tree

4 files changed

+147
-4
lines changed

4 files changed

+147
-4
lines changed

e2e/cases/doctor-rspack/banner-plugin.test.ts

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
import { expect, test } from '@playwright/test';
22
import { Utils } from '@rsdoctor/core/build-utils';
33
import { getSDK, setSDK } from '@rsdoctor/core/plugins';
4-
import { BannerPlugin, Compiler } from '@rspack/core';
4+
import rspack, { BannerPlugin, Compiler } from '@rspack/core';
55
import { compileByRspack } from '@scripts/test-helper';
66
import path from 'path';
77
import { createRsdoctorPlugin } from './test-utils';
8+
import TerserPlugin from 'terser-webpack-plugin';
89

910
let reportLoaderStartOrEndTimes = 0;
11+
let consoleOutput: string[] = [];
12+
13+
// Mock console.warn to capture output
14+
const originalWarn = console.warn;
15+
console.warn = (...args: any[]) => {
16+
consoleOutput.push(args.join(' '));
17+
originalWarn.apply(console, args);
18+
};
1019

1120
const header = `var header = 11111111; console.log(header)`;
1221
const footer = `var footer = 22222222; console.log(footer)`;
1322

1423
async function rspackCompile(
1524
_tapName: string,
1625
compile: typeof compileByRspack,
26+
options?: any,
1727
) {
1828
const file = path.resolve(__dirname, './fixtures/a.js');
1929
const loader = path.resolve(__dirname, './fixtures/loaders/comment.js');
@@ -106,6 +116,7 @@ async function rspackCompile(
106116
footer: true,
107117
}),
108118
],
119+
optimization: options?.optimization,
109120
});
110121

111122
return res;
@@ -135,3 +146,76 @@ test('rspack banner plugin', async () => {
135146
expect(res.assets[0].content).toContain('RSDOCTOR_START');
136147
expect(res.assets[0].content).toContain('RSDOCTOR_END');
137148
});
149+
150+
test('rspack banner plugin with terser drop_console', async () => {
151+
consoleOutput = []; // Reset console output
152+
const tapName = 'Foo';
153+
await rspackCompile(tapName, compileByRspack, {
154+
optimization: {
155+
minimizer: [
156+
new TerserPlugin({
157+
terserOptions: {
158+
compress: {
159+
drop_console: true,
160+
},
161+
},
162+
}) as any,
163+
],
164+
},
165+
});
166+
167+
// Verify warning message
168+
expect(
169+
consoleOutput.some((msg) => msg.includes('BannerPlugin detected')),
170+
).toBe(true);
171+
expect(
172+
consoleOutput.some((msg) => msg.includes('disable drop_console option')),
173+
).toBe(true);
174+
});
175+
176+
test('rspack banner plugin with default minimizer drop_console', async () => {
177+
consoleOutput = []; // Reset console output
178+
const tapName = 'Foo';
179+
await rspackCompile(tapName, compileByRspack, {
180+
optimization: {
181+
minimizer: [
182+
new rspack.SwcJsMinimizerRspackPlugin({
183+
minimizerOptions: {
184+
compress: {
185+
drop_console: true,
186+
},
187+
},
188+
}),
189+
],
190+
},
191+
});
192+
193+
// Verify warning message
194+
expect(
195+
consoleOutput.some((msg) => msg.includes('BannerPlugin detected')),
196+
).toBe(true);
197+
expect(
198+
consoleOutput.some((msg) => msg.includes('disable drop_console option')),
199+
).toBe(true);
200+
201+
const sdk = getSDK();
202+
// @ts-ignore
203+
const bundle = Utils.parseBundle(
204+
path.join(__dirname, './fixtures/rspack-banner-plugin.js'),
205+
// @ts-ignore
206+
sdk.getStoreData().moduleGraph.modules,
207+
);
208+
209+
expect(JSON.stringify(bundle.modules)).toBe(
210+
'{"":{"size":313,"sizeConvert":"313 B","content":"function (\\n __unused_webpack_module,\\n __webpack_exports__,\\n __webpack_require__,\\n ) {\\n \'use strict\';\\n __webpack_require__.r(__webpack_exports__);\\n __webpack_require__.d(__webpack_exports__, {\\n a: function () {\\n return a;\\n },\\n });\\n var a = 1;\\n }"}}',
211+
);
212+
const res = sdk.getStoreData().chunkGraph;
213+
expect(res.assets[0].content).toContain(header);
214+
expect(res.assets[0].content).toContain('RSDOCTOR_START');
215+
expect(res.assets[0].content).toContain('RSDOCTOR_END');
216+
});
217+
218+
// Restore console.warn after all tests
219+
test.afterAll(() => {
220+
console.warn = originalWarn;
221+
});

e2e/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@types/react": "^18.3.22",
2727
"loader-utils": "^2.0.4",
2828
"playwright": "1.44.1",
29+
"terser-webpack-plugin": "^5.3.14",
2930
"typescript": "^5.2.2",
3031
"webpack": "^5.97.1"
3132
}

packages/core/src/inner-plugins/plugins/bundleTagPlugin.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ export class InternalBundleTagPlugin<
3434
),
3535
);
3636

37+
// Check minimizers's drop_console configuration
38+
const minimizers = compiler.options.optimization?.minimizer || [];
39+
const terserPlugin = minimizers.find(
40+
(plugin): boolean => plugin?.constructor?.name === 'TerserPlugin',
41+
) as any;
42+
const swcPlugin = minimizers.find(
43+
(plugin): boolean =>
44+
plugin?.constructor?.name === 'SwcJsMinimizerRspackPlugin',
45+
) as any;
46+
47+
const hasTerserPlugin = !!terserPlugin;
48+
const hasSwcJsMinimizer = !!swcPlugin;
49+
50+
if (hasTerserPlugin || hasSwcJsMinimizer) {
51+
const terserDropConsole =
52+
terserPlugin?.options?.minimizer?.options?.compress
53+
?.drop_console;
54+
const swcDropConsole =
55+
swcPlugin?._args?.[0]?.minimizerOptions?.compress?.drop_console;
56+
57+
if (terserDropConsole === true || swcDropConsole === true) {
58+
logger.warn(
59+
chalk.yellow(
60+
'Warning: BannerPlugin detected in project. Please disable drop_console option in TerserPlugin or SwcJsMinimizerRspackPlugin to enable Rsdoctor analysis for BannerPlugin.',
61+
),
62+
);
63+
}
64+
}
3765
const chunks = compilation.chunks;
3866
for (let chunk of chunks) {
3967
for (const file of chunk.files) {

pnpm-lock.yaml

Lines changed: 33 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)