From 3f13d60cd943b15880414149bdf87600d97b1cce Mon Sep 17 00:00:00 2001 From: yk2012 Date: Wed, 3 Sep 2025 15:19:41 +0800 Subject: [PATCH 1/4] test: add e2e tests for removeConsole configuration [AI] --- e2e/test-cases/remove-console/basic/.umirc.ts | 5 ++ .../basic/__tests__/index.test.ts | 56 +++++++++++++++++++ .../remove-console/basic/pages/index.tsx | 30 ++++++++++ .../remove-console/disabled/.umirc.ts | 5 ++ .../disabled/__tests__/index.test.ts | 48 ++++++++++++++++ .../remove-console/disabled/pages/index.tsx | 33 +++++++++++ e2e/test-cases/remove-console/package.json | 19 +++++++ .../remove-console/selective/.umirc.ts | 5 ++ .../selective/__tests__/index.test.ts | 52 +++++++++++++++++ .../remove-console/selective/pages/index.tsx | 31 ++++++++++ .../esbuild/.umirc.ts | 6 ++ .../esbuild/__tests__/index.test.ts | 40 +++++++++++++ .../esbuild/pages/index.tsx | 32 +++++++++++ .../with-different-minifiers/terser/.umirc.ts | 6 ++ .../terser/__tests__/index.test.ts | 40 +++++++++++++ .../terser/pages/index.tsx | 30 ++++++++++ 16 files changed, 438 insertions(+) create mode 100644 e2e/test-cases/remove-console/basic/.umirc.ts create mode 100644 e2e/test-cases/remove-console/basic/__tests__/index.test.ts create mode 100644 e2e/test-cases/remove-console/basic/pages/index.tsx create mode 100644 e2e/test-cases/remove-console/disabled/.umirc.ts create mode 100644 e2e/test-cases/remove-console/disabled/__tests__/index.test.ts create mode 100644 e2e/test-cases/remove-console/disabled/pages/index.tsx create mode 100644 e2e/test-cases/remove-console/package.json create mode 100644 e2e/test-cases/remove-console/selective/.umirc.ts create mode 100644 e2e/test-cases/remove-console/selective/__tests__/index.test.ts create mode 100644 e2e/test-cases/remove-console/selective/pages/index.tsx create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts create mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx diff --git a/e2e/test-cases/remove-console/basic/.umirc.ts b/e2e/test-cases/remove-console/basic/.umirc.ts new file mode 100644 index 0000000..49d238b --- /dev/null +++ b/e2e/test-cases/remove-console/basic/.umirc.ts @@ -0,0 +1,5 @@ +export default { + presets: [require.resolve('@kmijs/preset-bundler')], + rspack: {}, + removeConsole: true, +} diff --git a/e2e/test-cases/remove-console/basic/__tests__/index.test.ts b/e2e/test-cases/remove-console/basic/__tests__/index.test.ts new file mode 100644 index 0000000..782cc9e --- /dev/null +++ b/e2e/test-cases/remove-console/basic/__tests__/index.test.ts @@ -0,0 +1,56 @@ +import path from 'node:path' +import { createUmi, unwrapOutputJSON } from '@e2e/helper' +import { expect, test } from 'vitest' + +test('removeConsole: true should remove all console statements', async () => { + const cwd = path.join(__dirname, '..') + const service = createUmi(cwd) + + await service.run({ name: 'build' }) + + const distPath = path.join(cwd, 'dist') + const files = await unwrapOutputJSON(distPath) + + // 获取主要的JS文件 + const jsFiles = Object.entries(files).filter( + ([name]) => name.endsWith('.js') && name.includes('umi'), + ) + + expect(jsFiles.length).toBeGreaterThan(0) + + // 验证所有console语句都被移除 + for (const [fileName, content] of jsFiles) { + expect( + content, + `File ${fileName} should not contain console.log`, + ).not.toContain('console.log') + expect( + content, + `File ${fileName} should not contain console.warn`, + ).not.toContain('console.warn') + expect( + content, + `File ${fileName} should not contain console.error`, + ).not.toContain('console.error') + expect( + content, + `File ${fileName} should not contain console.info`, + ).not.toContain('console.info') + expect( + content, + `File ${fileName} should not contain console.debug`, + ).not.toContain('console.debug') + } +}) + +test('should verify test page contains console statements before build', () => { + const testPagePath = path.join(__dirname, '../pages/index.tsx') + const fs = require('fs') + const content = fs.readFileSync(testPagePath, 'utf-8') + + // 确保测试页面确实包含console语句 + expect(content).toContain('console.log') + expect(content).toContain('console.warn') + expect(content).toContain('console.error') + expect(content).toContain('console.info') +}) diff --git a/e2e/test-cases/remove-console/basic/pages/index.tsx b/e2e/test-cases/remove-console/basic/pages/index.tsx new file mode 100644 index 0000000..86ccf62 --- /dev/null +++ b/e2e/test-cases/remove-console/basic/pages/index.tsx @@ -0,0 +1,30 @@ +import React, { useEffect } from 'react' + +export default function HomePage() { + useEffect(() => { + console.log('This log should be removed') + console.warn('This warning should be removed') + console.error('This error should be removed') + console.info('This info should be removed') + console.debug('This debug should be removed') + }, []) + + const handleClick = () => { + console.log('Button clicked - should be removed') + console.warn('Button warning - should be removed') + } + + const handleSubmit = () => { + console.error('Form error - should be removed') + console.info('Form submitted - should be removed') + } + + return ( +
+

Remove Console Test - Basic

+

All console statements should be removed when removeConsole: true

+ + +
+ ) +} diff --git a/e2e/test-cases/remove-console/disabled/.umirc.ts b/e2e/test-cases/remove-console/disabled/.umirc.ts new file mode 100644 index 0000000..91e3499 --- /dev/null +++ b/e2e/test-cases/remove-console/disabled/.umirc.ts @@ -0,0 +1,5 @@ +export default { + presets: [require.resolve('@kmijs/preset-bundler')], + rspack: {}, + // removeConsole is not configured - console statements should be kept +} diff --git a/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts b/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts new file mode 100644 index 0000000..9be57d4 --- /dev/null +++ b/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts @@ -0,0 +1,48 @@ +import path from 'node:path' +import { createUmi, unwrapOutputJSON } from '@e2e/helper' +import { expect, test } from 'vitest' + +test('without removeConsole config - all console statements should be kept', async () => { + const cwd = path.join(__dirname, '..') + const service = createUmi(cwd) + + await service.run({ name: 'build' }) + + const distPath = path.join(cwd, 'dist') + const files = await unwrapOutputJSON(distPath) + + // 获取主要的JS文件 + const jsFiles = Object.entries(files).filter( + ([name]) => name.endsWith('.js') && name.includes('umi'), + ) + + expect(jsFiles.length).toBeGreaterThan(0) + + // 验证所有console语句都被保留 + for (const [fileName, content] of jsFiles) { + expect(content, `File ${fileName} should contain console.log`).toContain( + 'console.log', + ) + expect(content, `File ${fileName} should contain console.warn`).toContain( + 'console.warn', + ) + expect(content, `File ${fileName} should contain console.error`).toContain( + 'console.error', + ) + expect(content, `File ${fileName} should contain console.info`).toContain( + 'console.info', + ) + } +}) + +test('should verify test page contains console statements before build', () => { + const testPagePath = path.join(__dirname, '../pages/index.tsx') + const fs = require('fs') + const content = fs.readFileSync(testPagePath, 'utf-8') + + // 确保测试页面确实包含console语句 + expect(content).toContain('console.log') + expect(content).toContain('console.warn') + expect(content).toContain('console.error') + expect(content).toContain('console.info') +}) diff --git a/e2e/test-cases/remove-console/disabled/pages/index.tsx b/e2e/test-cases/remove-console/disabled/pages/index.tsx new file mode 100644 index 0000000..ccbdac0 --- /dev/null +++ b/e2e/test-cases/remove-console/disabled/pages/index.tsx @@ -0,0 +1,33 @@ +import React, { useEffect } from 'react' + +export default function HomePage() { + useEffect(() => { + console.log('This log should be kept') + console.warn('This warning should be kept') + console.error('This error should be kept') + console.info('This info should be kept') + console.debug('This debug should be kept') + }, []) + + const handleClick = () => { + console.log('Button clicked - should be kept') + console.warn('Button warning - should be kept') + } + + const handleSubmit = () => { + console.error('Form error - should be kept') + console.info('Form submitted - should be kept') + } + + return ( +
+

Remove Console Test - Disabled

+

+ All console statements should be kept when removeConsole is not + configured +

+ + +
+ ) +} diff --git a/e2e/test-cases/remove-console/package.json b/e2e/test-cases/remove-console/package.json new file mode 100644 index 0000000..2eb18e4 --- /dev/null +++ b/e2e/test-cases/remove-console/package.json @@ -0,0 +1,19 @@ +{ + "name": "remove-console-e2e-tests", + "version": "1.0.0", + "private": true, + "description": "E2E tests for removeConsole configuration", + "scripts": { + "test": "vitest" + }, + "dependencies": { + "react": "^18.0.0", + "react-dom": "^18.0.0" + }, + "devDependencies": { + "@types/react": "^18.0.0", + "@types/react-dom": "^18.0.0", + "typescript": "^5.0.0", + "vitest": "^1.0.0" + } +} diff --git a/e2e/test-cases/remove-console/selective/.umirc.ts b/e2e/test-cases/remove-console/selective/.umirc.ts new file mode 100644 index 0000000..6464c55 --- /dev/null +++ b/e2e/test-cases/remove-console/selective/.umirc.ts @@ -0,0 +1,5 @@ +export default { + presets: [require.resolve('@kmijs/preset-bundler')], + rspack: {}, + removeConsole: ['log', 'warn'], +} diff --git a/e2e/test-cases/remove-console/selective/__tests__/index.test.ts b/e2e/test-cases/remove-console/selective/__tests__/index.test.ts new file mode 100644 index 0000000..f6f855e --- /dev/null +++ b/e2e/test-cases/remove-console/selective/__tests__/index.test.ts @@ -0,0 +1,52 @@ +import path from 'node:path' +import { createUmi, unwrapOutputJSON } from '@e2e/helper' +import { expect, test } from 'vitest' + +test('removeConsole: ["log", "warn"] should only remove specified methods', async () => { + const cwd = path.join(__dirname, '..') + const service = createUmi(cwd) + + await service.run({ name: 'build' }) + + const distPath = path.join(cwd, 'dist') + const files = await unwrapOutputJSON(distPath) + + // 获取主要的JS文件 + const jsFiles = Object.entries(files).filter( + ([name]) => name.endsWith('.js') && name.includes('umi'), + ) + + expect(jsFiles.length).toBeGreaterThan(0) + + for (const [fileName, content] of jsFiles) { + // 这些应该被移除 + expect( + content, + `File ${fileName} should not contain console.log`, + ).not.toContain('console.log') + expect( + content, + `File ${fileName} should not contain console.warn`, + ).not.toContain('console.warn') + + // 这些应该保留 + expect(content, `File ${fileName} should contain console.error`).toContain( + 'console.error', + ) + expect(content, `File ${fileName} should contain console.info`).toContain( + 'console.info', + ) + } +}) + +test('should verify test page contains all console statements before build', () => { + const testPagePath = path.join(__dirname, '../pages/index.tsx') + const fs = require('fs') + const content = fs.readFileSync(testPagePath, 'utf-8') + + // 确保测试页面确实包含所有类型的console语句 + expect(content).toContain('console.log') + expect(content).toContain('console.warn') + expect(content).toContain('console.error') + expect(content).toContain('console.info') +}) diff --git a/e2e/test-cases/remove-console/selective/pages/index.tsx b/e2e/test-cases/remove-console/selective/pages/index.tsx new file mode 100644 index 0000000..197ed4b --- /dev/null +++ b/e2e/test-cases/remove-console/selective/pages/index.tsx @@ -0,0 +1,31 @@ +import React, { useEffect } from 'react' + +export default function HomePage() { + useEffect(() => { + console.log('This log should be removed') + console.warn('This warning should be removed') + console.error('This error should be kept') + console.info('This info should be kept') + console.debug('This debug should be kept') + }, []) + + const handleClick = () => { + console.log('Button clicked - should be removed') + console.warn('Button warning - should be removed') + console.error('Button error - should be kept') + } + + const handleSubmit = () => { + console.error('Form error - should be kept') + console.info('Form submitted - should be kept') + } + + return ( +
+

Remove Console Test - Selective

+

Only log and warn should be removed, error and info should be kept

+ + +
+ ) +} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts new file mode 100644 index 0000000..d50a94c --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts @@ -0,0 +1,6 @@ +export default { + presets: [require.resolve('@kmijs/preset-bundler')], + rspack: {}, + jsMinifier: 'esbuild', + removeConsole: true, +} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts new file mode 100644 index 0000000..473fdb9 --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts @@ -0,0 +1,40 @@ +import path from 'node:path' +import { createUmi, unwrapOutputJSON } from '@e2e/helper' +import { expect, test } from 'vitest' + +test('removeConsole with esbuild minifier should remove all console statements', async () => { + const cwd = path.join(__dirname, '..') + const service = createUmi(cwd) + + await service.run({ name: 'build' }) + + const distPath = path.join(cwd, 'dist') + const files = await unwrapOutputJSON(distPath) + + // 获取主要的JS文件 + const jsFiles = Object.entries(files).filter( + ([name]) => name.endsWith('.js') && name.includes('umi'), + ) + + expect(jsFiles.length).toBeGreaterThan(0) + + // 验证所有console语句都被移除 + for (const [fileName, content] of jsFiles) { + expect( + content, + `File ${fileName} should not contain console.log`, + ).not.toContain('console.log') + expect( + content, + `File ${fileName} should not contain console.warn`, + ).not.toContain('console.warn') + expect( + content, + `File ${fileName} should not contain console.error`, + ).not.toContain('console.error') + expect( + content, + `File ${fileName} should not contain console.info`, + ).not.toContain('console.info') + } +}) diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx new file mode 100644 index 0000000..30e9cf0 --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx @@ -0,0 +1,32 @@ +import React, { useEffect } from 'react' + +export default function HomePage() { + useEffect(() => { + console.log('This log should be removed with esbuild') + console.warn('This warning should be removed with esbuild') + console.error('This error should be removed with esbuild') + console.info('This info should be removed with esbuild') + console.debug('This debug should be removed with esbuild') + }, []) + + const handleClick = () => { + console.log('Button clicked - should be removed with esbuild') + console.warn('Button warning - should be removed with esbuild') + } + + const handleSubmit = () => { + console.error('Form error - should be removed with esbuild') + console.info('Form submitted - should be removed with esbuild') + } + + return ( +
+

Remove Console Test - ESBuild Minifier

+

+ All console statements should be removed when using esbuild minifier +

+ + +
+ ) +} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts new file mode 100644 index 0000000..4b2da9a --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts @@ -0,0 +1,6 @@ +export default { + presets: [require.resolve('@kmijs/preset-bundler')], + rspack: {}, + jsMinifier: 'terser', + removeConsole: true, +} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts b/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts new file mode 100644 index 0000000..a08bb36 --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts @@ -0,0 +1,40 @@ +import path from 'node:path' +import { createUmi, unwrapOutputJSON } from '@e2e/helper' +import { expect, test } from 'vitest' + +test('removeConsole with terser minifier should remove all console statements', async () => { + const cwd = path.join(__dirname, '..') + const service = createUmi(cwd) + + await service.run({ name: 'build' }) + + const distPath = path.join(cwd, 'dist') + const files = await unwrapOutputJSON(distPath) + + // 获取主要的JS文件 + const jsFiles = Object.entries(files).filter( + ([name]) => name.endsWith('.js') && name.includes('umi'), + ) + + expect(jsFiles.length).toBeGreaterThan(0) + + // 验证所有console语句都被移除 + for (const [fileName, content] of jsFiles) { + expect( + content, + `File ${fileName} should not contain console.log`, + ).not.toContain('console.log') + expect( + content, + `File ${fileName} should not contain console.warn`, + ).not.toContain('console.warn') + expect( + content, + `File ${fileName} should not contain console.error`, + ).not.toContain('console.error') + expect( + content, + `File ${fileName} should not contain console.info`, + ).not.toContain('console.info') + } +}) diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx b/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx new file mode 100644 index 0000000..c3214ad --- /dev/null +++ b/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx @@ -0,0 +1,30 @@ +import React, { useEffect } from 'react' + +export default function HomePage() { + useEffect(() => { + console.log('This log should be removed with terser') + console.warn('This warning should be removed with terser') + console.error('This error should be removed with terser') + console.info('This info should be removed with terser') + console.debug('This debug should be removed with terser') + }, []) + + const handleClick = () => { + console.log('Button clicked - should be removed with terser') + console.warn('Button warning - should be removed with terser') + } + + const handleSubmit = () => { + console.error('Form error - should be removed with terser') + console.info('Form submitted - should be removed with terser') + } + + return ( +
+

Remove Console Test - Terser Minifier

+

All console statements should be removed when using terser minifier

+ + +
+ ) +} From 7bd81b0fbe3b285f8d081dd9beecf3541f5e7db9 Mon Sep 17 00:00:00 2001 From: yk2012 Date: Thu, 4 Sep 2025 16:32:20 +0800 Subject: [PATCH 2/4] fix: switch to terser minifier for rspack removeConsole functionality [AI] --- .../features/removeConsole/removeConsole.ts | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts index d179a84..ea2e6ca 100644 --- a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts +++ b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts @@ -19,37 +19,47 @@ export default (api: IApi) => { throw new Error('removeConsole does not support uglifyJs') } if (api.appData.bundler === 'webpack' && userConfig.jsMinifier === 'swc') { - throw new Error('removeConsole does not support using swc compression in webpack mode') + throw new Error( + 'removeConsole does not support using swc compression in webpack mode', + ) } }) api.modifyConfig((memo) => { const { removeConsole } = memo - const isRspack = api.appData.bundler === 'rspack' - // Default is esbuild - const jsMinifier = - api.appData.bundler === 'rspack' - ? api.userConfig.jsMinifier || 'swc' - : api.userConfig.jsMinifier || 'esbuild' - - const compressOptions = Array.isArray(removeConsole) - ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } - : { drop_console: true } + // Fix: Check for rspack config instead of bundler name + const isRspack = !!memo.rspack || !!api.userConfig.rspack + // Default is esbuild for webpack, swc for rspack + const jsMinifier = isRspack + ? api.userConfig.jsMinifier || 'swc' + : api.userConfig.jsMinifier || 'esbuild' if (isRspack && jsMinifier === 'swc') { + // SWC configuration issue: drop_console doesn't work in current Rspack version + // Workaround: Automatically switch to terser when removeConsole is enabled + + // Force switch to terser for removeConsole functionality + memo.jsMinifier = 'terser' + + // Apply terser configuration + const compressOptions = Array.isArray(removeConsole) + ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } + : { drop_console: true } + memo.jsMinifierOptions = { ...memo.jsMinifierOptions, - minimizerOptions: { - ...memo.jsMinifierOptions?.minimizerOptions, - compress: { - ...memo.jsMinifierOptions?.minimizerOptions?.compress, - ...compressOptions, - }, + compress: { + ...memo.jsMinifierOptions?.compress, + ...compressOptions, }, } return memo } + const compressOptions = Array.isArray(removeConsole) + ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } + : { drop_console: true } + // esbuild if (jsMinifier === 'esbuild') { const compressOptions = Array.isArray(removeConsole) From b31f93b218f344e2d3041e7873d669090b051ba6 Mon Sep 17 00:00:00 2001 From: yk2012 Date: Thu, 4 Sep 2025 17:03:03 +0800 Subject: [PATCH 3/4] refactor: update preset-bundler import paths in test cases [AI] --- e2e/test-cases/remove-console/basic/.umirc.ts | 2 +- e2e/test-cases/remove-console/disabled/.umirc.ts | 2 +- e2e/test-cases/remove-console/selective/.umirc.ts | 2 +- .../remove-console/with-different-minifiers/esbuild/.umirc.ts | 2 +- .../remove-console/with-different-minifiers/terser/.umirc.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/e2e/test-cases/remove-console/basic/.umirc.ts b/e2e/test-cases/remove-console/basic/.umirc.ts index 49d238b..cb05170 100644 --- a/e2e/test-cases/remove-console/basic/.umirc.ts +++ b/e2e/test-cases/remove-console/basic/.umirc.ts @@ -1,5 +1,5 @@ export default { - presets: [require.resolve('@kmijs/preset-bundler')], + presets: [require.resolve('../../../../packages/preset-bundler')], rspack: {}, removeConsole: true, } diff --git a/e2e/test-cases/remove-console/disabled/.umirc.ts b/e2e/test-cases/remove-console/disabled/.umirc.ts index 91e3499..f9b547f 100644 --- a/e2e/test-cases/remove-console/disabled/.umirc.ts +++ b/e2e/test-cases/remove-console/disabled/.umirc.ts @@ -1,5 +1,5 @@ export default { - presets: [require.resolve('@kmijs/preset-bundler')], + presets: [require.resolve('../../../../packages/preset-bundler')], rspack: {}, // removeConsole is not configured - console statements should be kept } diff --git a/e2e/test-cases/remove-console/selective/.umirc.ts b/e2e/test-cases/remove-console/selective/.umirc.ts index 6464c55..f946180 100644 --- a/e2e/test-cases/remove-console/selective/.umirc.ts +++ b/e2e/test-cases/remove-console/selective/.umirc.ts @@ -1,5 +1,5 @@ export default { - presets: [require.resolve('@kmijs/preset-bundler')], + presets: [require.resolve('../../../../packages/preset-bundler')], rspack: {}, removeConsole: ['log', 'warn'], } diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts index d50a94c..0660eb2 100644 --- a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts +++ b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts @@ -1,5 +1,5 @@ export default { - presets: [require.resolve('@kmijs/preset-bundler')], + presets: [require.resolve('../../../../../packages/preset-bundler')], rspack: {}, jsMinifier: 'esbuild', removeConsole: true, diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts index 4b2da9a..11012cd 100644 --- a/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts +++ b/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts @@ -1,5 +1,5 @@ export default { - presets: [require.resolve('@kmijs/preset-bundler')], + presets: [require.resolve('../../../../../packages/preset-bundler')], rspack: {}, jsMinifier: 'terser', removeConsole: true, From 22a4895a0a10b6760128c0d151829338dc07d56e Mon Sep 17 00:00:00 2001 From: yk2012 Date: Wed, 10 Sep 2025 15:39:39 +0800 Subject: [PATCH 4/4] test: add e2e tests for removeConsole feature --- e2e/test-cases/remove-console/basic/.umirc.ts | 5 -- .../basic/__tests__/index.test.ts | 56 ------------------- .../remove-console/basic/pages/index.tsx | 30 ---------- .../remove-console/disabled/.umirc.ts | 5 -- .../disabled/__tests__/index.test.ts | 48 ---------------- .../remove-console/disabled/pages/index.tsx | 33 ----------- e2e/test-cases/remove-console/package.json | 19 ------- .../remove-console/selective/.umirc.ts | 5 -- .../selective/__tests__/index.test.ts | 52 ----------------- .../remove-console/selective/pages/index.tsx | 31 ---------- .../esbuild/.umirc.ts | 6 -- .../esbuild/__tests__/index.test.ts | 40 ------------- .../esbuild/pages/index.tsx | 32 ----------- .../with-different-minifiers/terser/.umirc.ts | 6 -- .../terser/__tests__/index.test.ts | 40 ------------- .../terser/pages/index.tsx | 30 ---------- 16 files changed, 438 deletions(-) delete mode 100644 e2e/test-cases/remove-console/basic/.umirc.ts delete mode 100644 e2e/test-cases/remove-console/basic/__tests__/index.test.ts delete mode 100644 e2e/test-cases/remove-console/basic/pages/index.tsx delete mode 100644 e2e/test-cases/remove-console/disabled/.umirc.ts delete mode 100644 e2e/test-cases/remove-console/disabled/__tests__/index.test.ts delete mode 100644 e2e/test-cases/remove-console/disabled/pages/index.tsx delete mode 100644 e2e/test-cases/remove-console/package.json delete mode 100644 e2e/test-cases/remove-console/selective/.umirc.ts delete mode 100644 e2e/test-cases/remove-console/selective/__tests__/index.test.ts delete mode 100644 e2e/test-cases/remove-console/selective/pages/index.tsx delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts delete mode 100644 e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx diff --git a/e2e/test-cases/remove-console/basic/.umirc.ts b/e2e/test-cases/remove-console/basic/.umirc.ts deleted file mode 100644 index cb05170..0000000 --- a/e2e/test-cases/remove-console/basic/.umirc.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default { - presets: [require.resolve('../../../../packages/preset-bundler')], - rspack: {}, - removeConsole: true, -} diff --git a/e2e/test-cases/remove-console/basic/__tests__/index.test.ts b/e2e/test-cases/remove-console/basic/__tests__/index.test.ts deleted file mode 100644 index 782cc9e..0000000 --- a/e2e/test-cases/remove-console/basic/__tests__/index.test.ts +++ /dev/null @@ -1,56 +0,0 @@ -import path from 'node:path' -import { createUmi, unwrapOutputJSON } from '@e2e/helper' -import { expect, test } from 'vitest' - -test('removeConsole: true should remove all console statements', async () => { - const cwd = path.join(__dirname, '..') - const service = createUmi(cwd) - - await service.run({ name: 'build' }) - - const distPath = path.join(cwd, 'dist') - const files = await unwrapOutputJSON(distPath) - - // 获取主要的JS文件 - const jsFiles = Object.entries(files).filter( - ([name]) => name.endsWith('.js') && name.includes('umi'), - ) - - expect(jsFiles.length).toBeGreaterThan(0) - - // 验证所有console语句都被移除 - for (const [fileName, content] of jsFiles) { - expect( - content, - `File ${fileName} should not contain console.log`, - ).not.toContain('console.log') - expect( - content, - `File ${fileName} should not contain console.warn`, - ).not.toContain('console.warn') - expect( - content, - `File ${fileName} should not contain console.error`, - ).not.toContain('console.error') - expect( - content, - `File ${fileName} should not contain console.info`, - ).not.toContain('console.info') - expect( - content, - `File ${fileName} should not contain console.debug`, - ).not.toContain('console.debug') - } -}) - -test('should verify test page contains console statements before build', () => { - const testPagePath = path.join(__dirname, '../pages/index.tsx') - const fs = require('fs') - const content = fs.readFileSync(testPagePath, 'utf-8') - - // 确保测试页面确实包含console语句 - expect(content).toContain('console.log') - expect(content).toContain('console.warn') - expect(content).toContain('console.error') - expect(content).toContain('console.info') -}) diff --git a/e2e/test-cases/remove-console/basic/pages/index.tsx b/e2e/test-cases/remove-console/basic/pages/index.tsx deleted file mode 100644 index 86ccf62..0000000 --- a/e2e/test-cases/remove-console/basic/pages/index.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import React, { useEffect } from 'react' - -export default function HomePage() { - useEffect(() => { - console.log('This log should be removed') - console.warn('This warning should be removed') - console.error('This error should be removed') - console.info('This info should be removed') - console.debug('This debug should be removed') - }, []) - - const handleClick = () => { - console.log('Button clicked - should be removed') - console.warn('Button warning - should be removed') - } - - const handleSubmit = () => { - console.error('Form error - should be removed') - console.info('Form submitted - should be removed') - } - - return ( -
-

Remove Console Test - Basic

-

All console statements should be removed when removeConsole: true

- - -
- ) -} diff --git a/e2e/test-cases/remove-console/disabled/.umirc.ts b/e2e/test-cases/remove-console/disabled/.umirc.ts deleted file mode 100644 index f9b547f..0000000 --- a/e2e/test-cases/remove-console/disabled/.umirc.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default { - presets: [require.resolve('../../../../packages/preset-bundler')], - rspack: {}, - // removeConsole is not configured - console statements should be kept -} diff --git a/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts b/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts deleted file mode 100644 index 9be57d4..0000000 --- a/e2e/test-cases/remove-console/disabled/__tests__/index.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import path from 'node:path' -import { createUmi, unwrapOutputJSON } from '@e2e/helper' -import { expect, test } from 'vitest' - -test('without removeConsole config - all console statements should be kept', async () => { - const cwd = path.join(__dirname, '..') - const service = createUmi(cwd) - - await service.run({ name: 'build' }) - - const distPath = path.join(cwd, 'dist') - const files = await unwrapOutputJSON(distPath) - - // 获取主要的JS文件 - const jsFiles = Object.entries(files).filter( - ([name]) => name.endsWith('.js') && name.includes('umi'), - ) - - expect(jsFiles.length).toBeGreaterThan(0) - - // 验证所有console语句都被保留 - for (const [fileName, content] of jsFiles) { - expect(content, `File ${fileName} should contain console.log`).toContain( - 'console.log', - ) - expect(content, `File ${fileName} should contain console.warn`).toContain( - 'console.warn', - ) - expect(content, `File ${fileName} should contain console.error`).toContain( - 'console.error', - ) - expect(content, `File ${fileName} should contain console.info`).toContain( - 'console.info', - ) - } -}) - -test('should verify test page contains console statements before build', () => { - const testPagePath = path.join(__dirname, '../pages/index.tsx') - const fs = require('fs') - const content = fs.readFileSync(testPagePath, 'utf-8') - - // 确保测试页面确实包含console语句 - expect(content).toContain('console.log') - expect(content).toContain('console.warn') - expect(content).toContain('console.error') - expect(content).toContain('console.info') -}) diff --git a/e2e/test-cases/remove-console/disabled/pages/index.tsx b/e2e/test-cases/remove-console/disabled/pages/index.tsx deleted file mode 100644 index ccbdac0..0000000 --- a/e2e/test-cases/remove-console/disabled/pages/index.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import React, { useEffect } from 'react' - -export default function HomePage() { - useEffect(() => { - console.log('This log should be kept') - console.warn('This warning should be kept') - console.error('This error should be kept') - console.info('This info should be kept') - console.debug('This debug should be kept') - }, []) - - const handleClick = () => { - console.log('Button clicked - should be kept') - console.warn('Button warning - should be kept') - } - - const handleSubmit = () => { - console.error('Form error - should be kept') - console.info('Form submitted - should be kept') - } - - return ( -
-

Remove Console Test - Disabled

-

- All console statements should be kept when removeConsole is not - configured -

- - -
- ) -} diff --git a/e2e/test-cases/remove-console/package.json b/e2e/test-cases/remove-console/package.json deleted file mode 100644 index 2eb18e4..0000000 --- a/e2e/test-cases/remove-console/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "remove-console-e2e-tests", - "version": "1.0.0", - "private": true, - "description": "E2E tests for removeConsole configuration", - "scripts": { - "test": "vitest" - }, - "dependencies": { - "react": "^18.0.0", - "react-dom": "^18.0.0" - }, - "devDependencies": { - "@types/react": "^18.0.0", - "@types/react-dom": "^18.0.0", - "typescript": "^5.0.0", - "vitest": "^1.0.0" - } -} diff --git a/e2e/test-cases/remove-console/selective/.umirc.ts b/e2e/test-cases/remove-console/selective/.umirc.ts deleted file mode 100644 index f946180..0000000 --- a/e2e/test-cases/remove-console/selective/.umirc.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default { - presets: [require.resolve('../../../../packages/preset-bundler')], - rspack: {}, - removeConsole: ['log', 'warn'], -} diff --git a/e2e/test-cases/remove-console/selective/__tests__/index.test.ts b/e2e/test-cases/remove-console/selective/__tests__/index.test.ts deleted file mode 100644 index f6f855e..0000000 --- a/e2e/test-cases/remove-console/selective/__tests__/index.test.ts +++ /dev/null @@ -1,52 +0,0 @@ -import path from 'node:path' -import { createUmi, unwrapOutputJSON } from '@e2e/helper' -import { expect, test } from 'vitest' - -test('removeConsole: ["log", "warn"] should only remove specified methods', async () => { - const cwd = path.join(__dirname, '..') - const service = createUmi(cwd) - - await service.run({ name: 'build' }) - - const distPath = path.join(cwd, 'dist') - const files = await unwrapOutputJSON(distPath) - - // 获取主要的JS文件 - const jsFiles = Object.entries(files).filter( - ([name]) => name.endsWith('.js') && name.includes('umi'), - ) - - expect(jsFiles.length).toBeGreaterThan(0) - - for (const [fileName, content] of jsFiles) { - // 这些应该被移除 - expect( - content, - `File ${fileName} should not contain console.log`, - ).not.toContain('console.log') - expect( - content, - `File ${fileName} should not contain console.warn`, - ).not.toContain('console.warn') - - // 这些应该保留 - expect(content, `File ${fileName} should contain console.error`).toContain( - 'console.error', - ) - expect(content, `File ${fileName} should contain console.info`).toContain( - 'console.info', - ) - } -}) - -test('should verify test page contains all console statements before build', () => { - const testPagePath = path.join(__dirname, '../pages/index.tsx') - const fs = require('fs') - const content = fs.readFileSync(testPagePath, 'utf-8') - - // 确保测试页面确实包含所有类型的console语句 - expect(content).toContain('console.log') - expect(content).toContain('console.warn') - expect(content).toContain('console.error') - expect(content).toContain('console.info') -}) diff --git a/e2e/test-cases/remove-console/selective/pages/index.tsx b/e2e/test-cases/remove-console/selective/pages/index.tsx deleted file mode 100644 index 197ed4b..0000000 --- a/e2e/test-cases/remove-console/selective/pages/index.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import React, { useEffect } from 'react' - -export default function HomePage() { - useEffect(() => { - console.log('This log should be removed') - console.warn('This warning should be removed') - console.error('This error should be kept') - console.info('This info should be kept') - console.debug('This debug should be kept') - }, []) - - const handleClick = () => { - console.log('Button clicked - should be removed') - console.warn('Button warning - should be removed') - console.error('Button error - should be kept') - } - - const handleSubmit = () => { - console.error('Form error - should be kept') - console.info('Form submitted - should be kept') - } - - return ( -
-

Remove Console Test - Selective

-

Only log and warn should be removed, error and info should be kept

- - -
- ) -} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts deleted file mode 100644 index 0660eb2..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/.umirc.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default { - presets: [require.resolve('../../../../../packages/preset-bundler')], - rspack: {}, - jsMinifier: 'esbuild', - removeConsole: true, -} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts deleted file mode 100644 index 473fdb9..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/__tests__/index.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import path from 'node:path' -import { createUmi, unwrapOutputJSON } from '@e2e/helper' -import { expect, test } from 'vitest' - -test('removeConsole with esbuild minifier should remove all console statements', async () => { - const cwd = path.join(__dirname, '..') - const service = createUmi(cwd) - - await service.run({ name: 'build' }) - - const distPath = path.join(cwd, 'dist') - const files = await unwrapOutputJSON(distPath) - - // 获取主要的JS文件 - const jsFiles = Object.entries(files).filter( - ([name]) => name.endsWith('.js') && name.includes('umi'), - ) - - expect(jsFiles.length).toBeGreaterThan(0) - - // 验证所有console语句都被移除 - for (const [fileName, content] of jsFiles) { - expect( - content, - `File ${fileName} should not contain console.log`, - ).not.toContain('console.log') - expect( - content, - `File ${fileName} should not contain console.warn`, - ).not.toContain('console.warn') - expect( - content, - `File ${fileName} should not contain console.error`, - ).not.toContain('console.error') - expect( - content, - `File ${fileName} should not contain console.info`, - ).not.toContain('console.info') - } -}) diff --git a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx b/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx deleted file mode 100644 index 30e9cf0..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/esbuild/pages/index.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import React, { useEffect } from 'react' - -export default function HomePage() { - useEffect(() => { - console.log('This log should be removed with esbuild') - console.warn('This warning should be removed with esbuild') - console.error('This error should be removed with esbuild') - console.info('This info should be removed with esbuild') - console.debug('This debug should be removed with esbuild') - }, []) - - const handleClick = () => { - console.log('Button clicked - should be removed with esbuild') - console.warn('Button warning - should be removed with esbuild') - } - - const handleSubmit = () => { - console.error('Form error - should be removed with esbuild') - console.info('Form submitted - should be removed with esbuild') - } - - return ( -
-

Remove Console Test - ESBuild Minifier

-

- All console statements should be removed when using esbuild minifier -

- - -
- ) -} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts b/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts deleted file mode 100644 index 11012cd..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/terser/.umirc.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default { - presets: [require.resolve('../../../../../packages/preset-bundler')], - rspack: {}, - jsMinifier: 'terser', - removeConsole: true, -} diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts b/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts deleted file mode 100644 index a08bb36..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/terser/__tests__/index.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import path from 'node:path' -import { createUmi, unwrapOutputJSON } from '@e2e/helper' -import { expect, test } from 'vitest' - -test('removeConsole with terser minifier should remove all console statements', async () => { - const cwd = path.join(__dirname, '..') - const service = createUmi(cwd) - - await service.run({ name: 'build' }) - - const distPath = path.join(cwd, 'dist') - const files = await unwrapOutputJSON(distPath) - - // 获取主要的JS文件 - const jsFiles = Object.entries(files).filter( - ([name]) => name.endsWith('.js') && name.includes('umi'), - ) - - expect(jsFiles.length).toBeGreaterThan(0) - - // 验证所有console语句都被移除 - for (const [fileName, content] of jsFiles) { - expect( - content, - `File ${fileName} should not contain console.log`, - ).not.toContain('console.log') - expect( - content, - `File ${fileName} should not contain console.warn`, - ).not.toContain('console.warn') - expect( - content, - `File ${fileName} should not contain console.error`, - ).not.toContain('console.error') - expect( - content, - `File ${fileName} should not contain console.info`, - ).not.toContain('console.info') - } -}) diff --git a/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx b/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx deleted file mode 100644 index c3214ad..0000000 --- a/e2e/test-cases/remove-console/with-different-minifiers/terser/pages/index.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import React, { useEffect } from 'react' - -export default function HomePage() { - useEffect(() => { - console.log('This log should be removed with terser') - console.warn('This warning should be removed with terser') - console.error('This error should be removed with terser') - console.info('This info should be removed with terser') - console.debug('This debug should be removed with terser') - }, []) - - const handleClick = () => { - console.log('Button clicked - should be removed with terser') - console.warn('Button warning - should be removed with terser') - } - - const handleSubmit = () => { - console.error('Form error - should be removed with terser') - console.info('Form submitted - should be removed with terser') - } - - return ( -
-

Remove Console Test - Terser Minifier

-

All console statements should be removed when using terser minifier

- - -
- ) -}