-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add removeConsole feature for console stripping in builds #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
269dbce
c721251
318b14e
4b5df27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,6 +151,8 @@ pnpm dev | |
| pnpm build | ||
| ``` | ||
|
|
||
| 如果您觉得这个项目有帮助,欢迎给我们点个 star ⭐️⭐️⭐️ | ||
|
|
||
| ## 许可证 | ||
|
|
||
| [MIT](./LICENSE) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||
| import type { IApi } from '@kmijs/types' | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export default (api: IApi) => { | ||||||||||||||||||||||||||
| api.describe({ | ||||||||||||||||||||||||||
| key: 'removeConsole', | ||||||||||||||||||||||||||
| config: { | ||||||||||||||||||||||||||
| schema({ zod }) { | ||||||||||||||||||||||||||
| return zod.union([ | ||||||||||||||||||||||||||
| zod.boolean(), | ||||||||||||||||||||||||||
| zod.array(zod.enum(['error', 'warn', 'info', 'log'])), | ||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| enableBy: api.EnableBy.config, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| api.onCheckConfig(({ userConfig }) => { | ||||||||||||||||||||||||||
| if (userConfig.jsMinifier === 'uglifyJs') { | ||||||||||||||||||||||||||
| 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') | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| const compressOptions = Array.isArray(removeConsole) | |
| const swcCompressOptions = Array.isArray(removeConsole) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ 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.
Copilot
AI
Jul 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify the correctness of the updated import paths.
The import statements have been updated from
@kmi/kmijs/plugin-utilstoumi/plugin-utils. Please confirm that this is the correct import path and that thecreateDependenciesRegExpfunction is available fromumi/plugin-utils.Also applies to: 321-321
🏁 Script executed:
Length of output: 281114
Update documentation import source for createDependenciesRegExp
The docs currently import
createDependenciesRegExpfromumi/plugin-utils, but that helper is defined and exported by@kmijs/shared, notumi/plugin-utils.Please update both occurrences in
docs/docs/config/config.md:• Line 299
• Line 321
Suggested change:
📝 Committable suggestion
🤖 Prompt for AI Agents