From 269dbce25b2bd4cc601040940af9423b714b99f8 Mon Sep 17 00:00:00 2001 From: xierenyuan Date: Mon, 21 Jul 2025 10:22:29 +0800 Subject: [PATCH 1/4] feat: add removeConsole feature for console stripping in builds --- README.md | 2 + README.zh-CN.md | 2 + .../features/removeConsole/removeConsole.ts | 78 +++++++++++++++++++ packages/preset-bundler/src/index.ts | 1 + 4 files changed, 83 insertions(+) create mode 100644 packages/preset-bundler/src/features/removeConsole/removeConsole.ts diff --git a/README.md b/README.md index 96ba432..ed7b53d 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,8 @@ pnpm dev pnpm build ``` +If you find this helpful, welcome to give us a star ⭐️⭐️⭐️ + ## License [MIT](./LICENSE) diff --git a/README.zh-CN.md b/README.zh-CN.md index 9621c04..e78793b 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -151,6 +151,8 @@ pnpm dev pnpm build ``` +如果您觉得这个项目有帮助,欢迎给我们点个 star ⭐️⭐️⭐️ + ## 许可证 [MIT](./LICENSE) diff --git a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts new file mode 100644 index 0000000..b4db2e6 --- /dev/null +++ b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts @@ -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 不支持 uglifyJs') + } + if (api.appData.bundler === 'webpack' && userConfig.jsMinifier === 'swc') { + throw new Error('removeConsole 在 webpack 模式下不支持使用 swc 压缩') + } + }) + + api.modifyConfig((memo) => { + const { removeConsole } = memo + const isRspack = api.appData.bundler === 'rspack' + // 默认是 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 } + + if (isRspack && jsMinifier === 'swc') { + 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'] } + memo.jsMinifierOptions = { + ...memo.jsMinifierOptions, + ...compressOptions, + } + return memo + } + + // terser + if (jsMinifier === 'terser') { + memo.jsMinifierOptions = { + ...memo.jsMinifierOptions, + compress: { + ...memo.jsMinifierOptions?.compress, + ...compressOptions, + }, + } + return memo + } + return memo + }) +} diff --git a/packages/preset-bundler/src/index.ts b/packages/preset-bundler/src/index.ts index a6df9ac..f4d3c10 100644 --- a/packages/preset-bundler/src/index.ts +++ b/packages/preset-bundler/src/index.ts @@ -16,6 +16,7 @@ export default (api: IApi) => { require.resolve('./features/transformConfig'), require.resolve('./features/define/define'), require.resolve('./features/codeSplitting/codeSplitting'), + require.resolve('./features/removeConsole/removeConsole'), // plugins require.resolve('@kmijs/plugin-svgr'), From c7212510ab34afaa92004bdedc3b096e117aaaec Mon Sep 17 00:00:00 2001 From: xierenyuan Date: Mon, 21 Jul 2025 10:23:36 +0800 Subject: [PATCH 2/4] docs: update import paths from @kmi/kmijs to umi --- docs/docs/config/config.md | 4 ++-- docs/docs/config/html-config.md | 8 -------- docs/docs/guide/rspack.md | 2 -- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/docs/docs/config/config.md b/docs/docs/config/config.md index e977979..34d3065 100644 --- a/docs/docs/config/config.md +++ b/docs/docs/config/config.md @@ -296,7 +296,7 @@ export default defineConfig({ 比如将 node_modules 下的 `@m-ui/react` 库拆分到 `m-ui.js` 中: ```ts [config/config.ts] -import { createDependenciesRegExp } from '@kmi/kmijs/plugin-utils' +import { createDependenciesRegExp } from 'umi/plugin-utils' export default defineConfig({ codeSplitting: { @@ -318,7 +318,7 @@ export default defineConfig({ 可以通过 `codeSplitting.override` 配置项来自定义 Rspack(Webpack) 拆包配置, 此配置会和 Rspack(Webpack) 的 splitChunks 配置进行合并(cacheGroups 配置也会合并)。比如: ```ts [config/config.ts] -import { createDependenciesRegExp } from '@kmi/kmijs/plugin-utils' +import { createDependenciesRegExp } from 'umi/plugin-utils' export default defineConfig({ codeSplitting: { diff --git a/docs/docs/config/html-config.md b/docs/docs/config/html-config.md index d247742..e2fd32b 100644 --- a/docs/docs/config/html-config.md +++ b/docs/docs/config/html-config.md @@ -110,8 +110,6 @@ links: [{ href: '/foo.css', rel: 'preload' }] ``` ```ts [config/config.ts] -import { defineConfig } from '@kmi/kmijs' - export default defineConfig({ links: [{ href: '/foo.css', rel: 'preload' }] // [!code ++] }) @@ -175,8 +173,6 @@ type MetaOptions = { 比如设置 description: ```ts [config/config.ts] -import { defineConfig } from '@kmi/kmijs' - export default defineConfig({ metas: { description: 'a description of the page', // [!code ++] @@ -203,8 +199,6 @@ type MetaOptions = { 比如配置配置 X-UA-Compatible ```ts [config/config.ts] -import { defineConfig } from '@kmi/kmijs' - export default defineConfig({ metas: { 'X-UA-Compatible': { // [!code ++] @@ -233,8 +227,6 @@ type Metas = Array 示例: ```ts [config/config.ts] -import { defineConfig } from '@kmi/kmijs' - export default defineConfig({ metas: [ { name: 'keywords', content: 'kmi, kmijs' }, // [!code ++] diff --git a/docs/docs/guide/rspack.md b/docs/docs/guide/rspack.md index 70b7b33..042fdf8 100644 --- a/docs/docs/guide/rspack.md +++ b/docs/docs/guide/rspack.md @@ -14,8 +14,6 @@ 从 Umi 的 `4.4.11` 版本起,在一个已有的 Umi 项目中,你仅需在 `config/config.ts` 中添加以下配置,即可启用 Rspack 构建: ```ts [config/config.ts] -import { defineConfig } from '@kmi/kmijs'; - export default defineConfig({ // Configure Kmi preset presets: ['@kmijs/preset-bundler'], From 318b14e05ee231ec62841bd87ec82c63202d0bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=B9=B3?= Date: Mon, 21 Jul 2025 10:44:05 +0800 Subject: [PATCH 3/4] Update packages/preset-bundler/src/features/removeConsole/removeConsole.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../src/features/removeConsole/removeConsole.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts index b4db2e6..80a0de0 100644 --- a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts +++ b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts @@ -16,10 +16,10 @@ export default (api: IApi) => { api.onCheckConfig(({ userConfig }) => { if (userConfig.jsMinifier === 'uglifyJs') { - throw new Error('removeConsole 不支持 uglifyJs') + throw new Error('removeConsole does not support uglifyJs') } if (api.appData.bundler === 'webpack' && userConfig.jsMinifier === 'swc') { - throw new Error('removeConsole 在 webpack 模式下不支持使用 swc 压缩') + throw new Error('removeConsole does not support using swc compression in webpack mode') } }) From 4b5df27444032f8dbfa06cefbb3f7d63837f7b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=B9=B3?= Date: Mon, 21 Jul 2025 10:49:53 +0800 Subject: [PATCH 4/4] Update packages/preset-bundler/src/features/removeConsole/removeConsole.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../preset-bundler/src/features/removeConsole/removeConsole.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts index 80a0de0..d179a84 100644 --- a/packages/preset-bundler/src/features/removeConsole/removeConsole.ts +++ b/packages/preset-bundler/src/features/removeConsole/removeConsole.ts @@ -26,7 +26,7 @@ export default (api: IApi) => { api.modifyConfig((memo) => { const { removeConsole } = memo const isRspack = api.appData.bundler === 'rspack' - // 默认是 esbuild + // Default is esbuild const jsMinifier = api.appData.bundler === 'rspack' ? api.userConfig.jsMinifier || 'swc'