|
1 | 1 | import { expect, test } from '@playwright/test';
|
2 | 2 | import { Utils } from '@rsdoctor/core/build-utils';
|
3 | 3 | import { getSDK, setSDK } from '@rsdoctor/core/plugins';
|
4 |
| -import { BannerPlugin, Compiler } from '@rspack/core'; |
| 4 | +import rspack, { BannerPlugin, Compiler } from '@rspack/core'; |
5 | 5 | import { compileByRspack } from '@scripts/test-helper';
|
6 | 6 | import path from 'path';
|
7 | 7 | import { createRsdoctorPlugin } from './test-utils';
|
| 8 | +import TerserPlugin from 'terser-webpack-plugin'; |
8 | 9 |
|
9 | 10 | 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 | +}; |
10 | 19 |
|
11 | 20 | const header = `var header = 11111111; console.log(header)`;
|
12 | 21 | const footer = `var footer = 22222222; console.log(footer)`;
|
13 | 22 |
|
14 | 23 | async function rspackCompile(
|
15 | 24 | _tapName: string,
|
16 | 25 | compile: typeof compileByRspack,
|
| 26 | + options?: any, |
17 | 27 | ) {
|
18 | 28 | const file = path.resolve(__dirname, './fixtures/a.js');
|
19 | 29 | const loader = path.resolve(__dirname, './fixtures/loaders/comment.js');
|
@@ -106,6 +116,7 @@ async function rspackCompile(
|
106 | 116 | footer: true,
|
107 | 117 | }),
|
108 | 118 | ],
|
| 119 | + optimization: options?.optimization, |
109 | 120 | });
|
110 | 121 |
|
111 | 122 | return res;
|
@@ -135,3 +146,76 @@ test('rspack banner plugin', async () => {
|
135 | 146 | expect(res.assets[0].content).toContain('RSDOCTOR_START');
|
136 | 147 | expect(res.assets[0].content).toContain('RSDOCTOR_END');
|
137 | 148 | });
|
| 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 | +}); |
0 commit comments