feat: add removeConsole feature for console stripping in builds#27
feat: add removeConsole feature for console stripping in builds#27xierenyuan merged 4 commits intomainfrom
Conversation
|
Warning Rate limit exceeded@xierenyuan has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 5 minutes and 4 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughA new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Bundler
participant RemoveConsolePlugin
User->>Bundler: Configure build with removeConsole option
Bundler->>RemoveConsolePlugin: Register and validate removeConsole config
RemoveConsolePlugin-->>Bundler: Modify minifier options to remove console methods
Bundler-->>User: Produce build output with specified console methods removed
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Pull Request Overview
This PR implements a new removeConsole feature that allows stripping console statements from production builds to reduce bundle size and improve performance. The feature supports different JavaScript minifiers (esbuild, terser, swc) and bundlers (webpack, rspack) with appropriate configuration validation.
- Adds a new removeConsole plugin with schema validation and minifier-specific configuration
- Integrates the plugin into the preset-bundler package
- Updates documentation to remove redundant import statements and adds promotional content
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/preset-bundler/src/index.ts | Registers the new removeConsole feature plugin |
| packages/preset-bundler/src/features/removeConsole/removeConsole.ts | Core implementation of console removal feature with multi-minifier support |
| docs/docs/guide/rspack.md | Removes redundant import statement from code example |
| docs/docs/config/html-config.md | Removes redundant import statements from multiple code examples |
| docs/docs/config/config.md | Updates import path references in code examples |
| README.zh-CN.md | Adds promotional star request message |
| README.md | Adds promotional star request message |
packages/preset-bundler/src/features/removeConsole/removeConsole.ts
Outdated
Show resolved
Hide resolved
| throw new Error('removeConsole 不支持 uglifyJs') | ||
| } | ||
| if (api.appData.bundler === 'webpack' && userConfig.jsMinifier === 'swc') { | ||
| throw new Error('removeConsole 在 webpack 模式下不支持使用 swc 压缩') |
There was a problem hiding this comment.
Error message is in Chinese. Consider using English for consistency with the codebase, or provide internationalization support.
| throw new Error('removeConsole 在 webpack 模式下不支持使用 swc 压缩') | |
| throw new Error('removeConsole does not support using swc compression in webpack mode') |
| ? api.userConfig.jsMinifier || 'swc' | ||
| : api.userConfig.jsMinifier || 'esbuild' | ||
|
|
||
| const compressOptions = Array.isArray(removeConsole) |
There was a problem hiding this comment.
The variable name 'compressOptions' is used twice with different structures (lines 35 and 55). Consider using more specific names like 'swcCompressOptions' and 'esbuildCompressOptions' to avoid confusion.
| const compressOptions = Array.isArray(removeConsole) | |
| const swcCompressOptions = Array.isArray(removeConsole) |
| const compressOptions = Array.isArray(removeConsole) | ||
| ? { pure: removeConsole.map((method) => `console.${method}`) } | ||
| : { drop: ['console'] } | ||
| memo.jsMinifierOptions = { | ||
| ...memo.jsMinifierOptions, | ||
| ...compressOptions, |
There was a problem hiding this comment.
This variable shadows the 'compressOptions' variable declared on line 35. Consider using a different name like 'esbuildOptions' to improve code clarity.
| const compressOptions = Array.isArray(removeConsole) | |
| ? { pure: removeConsole.map((method) => `console.${method}`) } | |
| : { drop: ['console'] } | |
| memo.jsMinifierOptions = { | |
| ...memo.jsMinifierOptions, | |
| ...compressOptions, | |
| const esbuildOptions = Array.isArray(removeConsole) | |
| ? { pure: removeConsole.map((method) => `console.${method}`) } | |
| : { drop: ['console'] } | |
| memo.jsMinifierOptions = { | |
| ...memo.jsMinifierOptions, | |
| ...esbuildOptions, |
packages/preset-bundler/src/features/removeConsole/removeConsole.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (5)
README.zh-CN.md (1)
154-155: Hyper-link the call-to-action & tweak wording for clarityAdding a star reminder is great, but readers can’t click directly. Consider linking to the repo and polishing the copy slightly.
-如果您觉得这个项目有帮助,欢迎给我们点个 star ⭐️⭐️⭐️ +如果您觉得这个项目对您有所帮助,请在 GitHub 上给我们点个 ⭐️ [Star](https://github.com/kmijs/kmi)README.md (1)
155-156: Add link & tighten phrasingSame comment as the Chinese README—make the CTA actionable and slightly crisper.
-If you find this helpful, welcome to give us a star ⭐️⭐️⭐️ +If you find this project helpful, please consider giving it a ⭐️ [Star](https://github.com/kmijs/kmi) on GitHubpackages/preset-bundler/src/features/removeConsole/removeConsole.ts (3)
18-23: Consider using English for error messages to maintain consistency.The error messages are currently in Chinese. For better maintainability and consistency with typical JavaScript/TypeScript codebases, consider using English error messages.
- throw new Error('removeConsole 不支持 uglifyJs') + throw new Error('removeConsole does not support uglifyJs minifier')- throw new Error('removeConsole 在 webpack 模式下不支持使用 swc 压缩') + throw new Error('removeConsole does not support swc minifier in webpack mode')
30-33: Clarify the default minifier logic.The default minifier selection logic appears inconsistent. Both branches check for
rspackbut assign different defaults. Consider clarifying this logic.- const jsMinifier = - api.appData.bundler === 'rspack' - ? api.userConfig.jsMinifier || 'swc' - : api.userConfig.jsMinifier || 'esbuild' + const defaultMinifier = api.appData.bundler === 'rspack' ? 'swc' : 'esbuild' + const jsMinifier = api.userConfig.jsMinifier || defaultMinifier
26-77: Consider extracting configuration logic into helper functions.The main function is quite long and handles multiple minifier configurations. Consider extracting the configuration logic for better maintainability.
// Helper functions function createSwcConfig(removeConsole: boolean | string[], existingOptions: any) { const compressOptions = Array.isArray(removeConsole) ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } : { drop_console: true } const existingMinifierOptions = existingOptions || {} const existingMinimizerOptions = existingMinifierOptions.minimizerOptions || {} const existingCompress = existingMinimizerOptions.compress || {} return { ...existingMinifierOptions, minimizerOptions: { ...existingMinimizerOptions, compress: { ...existingCompress, ...compressOptions, }, }, } } function createEsbuildConfig(removeConsole: boolean | string[], existingOptions: any) { const esbuildOptions = Array.isArray(removeConsole) ? { pure: removeConsole.map((method) => `console.${method}`) } : { drop: ['console'] } return { ...existingOptions, ...esbuildOptions, } } function createTerserConfig(removeConsole: boolean | string[], existingOptions: any) { const compressOptions = Array.isArray(removeConsole) ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } : { drop_console: true } const existingCompress = existingOptions?.compress || {} return { ...existingOptions, compress: { ...existingCompress, ...compressOptions, }, } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
README.md(1 hunks)README.zh-CN.md(1 hunks)docs/docs/config/config.md(2 hunks)docs/docs/config/html-config.md(0 hunks)docs/docs/guide/rspack.md(0 hunks)packages/preset-bundler/src/features/removeConsole/removeConsole.ts(1 hunks)packages/preset-bundler/src/index.ts(1 hunks)
💤 Files with no reviewable changes (2)
- docs/docs/guide/rspack.md
- docs/docs/config/html-config.md
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Cursor BugBot
- GitHub Check: build-and-test (22.x, windows-latest)
- GitHub Check: lint
- GitHub Check: build-and-test (22.x, ubuntu-latest)
🔇 Additional comments (1)
packages/preset-bundler/src/index.ts (1)
19-19: LGTM! Feature plugin registration follows established pattern.The
removeConsolefeature plugin is correctly registered following the same pattern as other feature plugins in the preset.
|
|
||
| ```ts [config/config.ts] | ||
| import { createDependenciesRegExp } from '@kmi/kmijs/plugin-utils' | ||
| import { createDependenciesRegExp } from 'umi/plugin-utils' |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Verify the correctness of the updated import paths.
The import statements have been updated from @kmi/kmijs/plugin-utils to umi/plugin-utils. Please confirm that this is the correct import path and that the createDependenciesRegExp function is available from umi/plugin-utils.
Also applies to: 321-321
🏁 Script executed:
#!/bin/bash
# Description: Verify the existence and availability of createDependenciesRegExp from umi/plugin-utils
# Search for createDependenciesRegExp in the codebase
echo "Searching for createDependenciesRegExp function..."
rg -A 3 "createDependenciesRegExp" --type ts --type js
echo -e "\n=== Checking umi/plugin-utils exports ==="
# Look for umi/plugin-utils in package.json or similar files
fd "package\.json" --exec grep -l "umi" {} \;
echo -e "\n=== Checking for plugin-utils imports ==="
# Search for other uses of plugin-utils imports
rg "plugin-utils" --type ts --type js -A 2 -B 2Length of output: 281114
Update documentation import source for createDependenciesRegExp
The docs currently import createDependenciesRegExp from umi/plugin-utils, but that helper is defined and exported by @kmijs/shared, not umi/plugin-utils.
Please update both occurrences in docs/docs/config/config.md:
• Line 299
• Line 321
Suggested change:
- import { createDependenciesRegExp } from 'umi/plugin-utils'
+ import { createDependenciesRegExp } from '@kmijs/shared'📝 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.
| import { createDependenciesRegExp } from 'umi/plugin-utils' | |
| -import { createDependenciesRegExp } from 'umi/plugin-utils' | |
| +import { createDependenciesRegExp } from '@kmijs/shared' |
🤖 Prompt for AI Agents
In docs/docs/config/config.md at lines 299 and 321, the import source for
createDependenciesRegExp is incorrectly set to umi/plugin-utils. Change both
import statements to import createDependenciesRegExp from @kmijs/shared instead
to reflect the correct module source.
| const compressOptions = Array.isArray(removeConsole) | ||
| ? { pure_funcs: removeConsole.map((method) => `console.${method}`) } | ||
| : { drop_console: true } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Eliminate duplicate options creation logic.
The compressOptions creation is duplicated for different minifiers. This can lead to confusion and maintenance issues.
Consider restructuring the logic to avoid duplication:
- const compressOptions = Array.isArray(removeConsole)
- ? { pure_funcs: removeConsole.map((method) => `console.${method}`) }
- : { drop_console: true }
-
if (isRspack && jsMinifier === 'swc') {
+ 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,
},
},
}
return memo
}
// esbuild
if (jsMinifier === 'esbuild') {
- const compressOptions = Array.isArray(removeConsole)
- ? { pure: removeConsole.map((method) => `console.${method}`) }
- : { drop: ['console'] }
+ const esbuildOptions = Array.isArray(removeConsole)
+ ? { pure: removeConsole.map((method) => `console.${method}`) }
+ : { drop: ['console'] }
memo.jsMinifierOptions = {
...memo.jsMinifierOptions,
- ...compressOptions,
+ ...esbuildOptions,
}
return memo
}
// terser
if (jsMinifier === 'terser') {
+ const terserOptions = Array.isArray(removeConsole)
+ ? { pure_funcs: removeConsole.map((method) => `console.${method}`) }
+ : { drop_console: true }
memo.jsMinifierOptions = {
...memo.jsMinifierOptions,
compress: {
...memo.jsMinifierOptions?.compress,
- ...compressOptions,
+ ...terserOptions,
},
}
return memo
}Also applies to: 55-57
🤖 Prompt for AI Agents
In packages/preset-bundler/src/features/removeConsole/removeConsole.ts around
lines 35-37 and 55-57, the logic for creating compressOptions is duplicated for
different minifiers, which can cause confusion and maintenance problems.
Refactor the code to centralize the creation of compressOptions by extracting
the shared logic into a single function or variable that can be reused for both
minifiers, eliminating the repeated code blocks.
| memo.jsMinifierOptions = { | ||
| ...memo.jsMinifierOptions, | ||
| minimizerOptions: { | ||
| ...memo.jsMinifierOptions?.minimizerOptions, | ||
| compress: { | ||
| ...memo.jsMinifierOptions?.minimizerOptions?.compress, | ||
| ...compressOptions, | ||
| }, | ||
| }, | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Improve type safety for minifier options.
The code accesses nested properties without proper type checking, which could lead to runtime errors.
Consider adding type guards or using optional chaining more defensively:
if (isRspack && jsMinifier === 'swc') {
+ const existingMinifierOptions = memo.jsMinifierOptions || {}
+ const existingMinimizerOptions = existingMinifierOptions.minimizerOptions || {}
+ const existingCompress = existingMinimizerOptions.compress || {}
+
memo.jsMinifierOptions = {
- ...memo.jsMinifierOptions,
+ ...existingMinifierOptions,
minimizerOptions: {
- ...memo.jsMinifierOptions?.minimizerOptions,
+ ...existingMinimizerOptions,
compress: {
- ...memo.jsMinifierOptions?.minimizerOptions?.compress,
+ ...existingCompress,
...compressOptions,
},
},
}
return memo
}Similar improvements should be applied to the terser configuration block.
Also applies to: 67-74
🤖 Prompt for AI Agents
In packages/preset-bundler/src/features/removeConsole/removeConsole.ts around
lines 40 to 49 and 67 to 74, the code accesses nested properties of
jsMinifierOptions without sufficient type safety, risking runtime errors. Add
type guards to verify the existence and correct type of each nested property
before spreading or accessing them, or enhance optional chaining usage to safely
handle undefined values. This ensures that the minifier options and terser
configuration blocks are accessed and merged only when valid, preventing
potential crashes.
…le.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…le.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Close #26
Summary by CodeRabbit
New Features
Documentation