Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions packages/preset-bundler/src/features/removeConsole/removeConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Unify Rspack detection with appData to avoid false positives.

!!api.userConfig.rspack can be set even when bundler isn’t Rspack, causing misclassification. Prefer api.appData.bundler with a fallback.

-    const isRspack = !!memo.rspack || !!api.userConfig.rspack
+    const isRspack =
+      api.appData.bundler === 'rspack' || Boolean(memo.rspack)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const isRspack = !!memo.rspack || !!api.userConfig.rspack
const isRspack =
api.appData.bundler === 'rspack' || Boolean(memo.rspack)
🤖 Prompt for AI Agents
In packages/preset-bundler/src/features/removeConsole/removeConsole.ts around
line 31, the current Rspack detection uses !!api.userConfig.rspack which can
misclassify projects; change the logic to prefer api.appData.bundler (e.g.
api.appData?.bundler === 'rspack') and fall back to memo.rspack if appData is
unavailable, removing reliance on userConfig.rspack to avoid false positives.

// 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)
Expand Down
Loading