Skip to content
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

项目中使用 rc-util/es/warning 后,打包后的生产代码为什么会包含警告代码? #301

Open
KAROTT7 opened this issue May 5, 2022 · 5 comments

Comments

@KAROTT7
Copy link

KAROTT7 commented May 5, 2022

问题如题所述

复现案例

https://stackblitz.com/edit/vitejs-vite-zdg5hc?file=src%2FApp.tsx
请看案例中 dist/assets/index.57005310.js 文件中 7356-7467 代码行

期待行为:

正常来说生产代码不应该包含警告(console.log/console.warn/console.error)等不影响项目执行的代码

造成问题的原因:

// 源码实现如下(只是列出部分代码)
export function warning(valid, message) {
  if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
    console.error(message)
  }
}

// 相比以上实现,下列实现则会在打包生产代码时移除相关代码
function noop() {}
export let warning = noop

if (process.env.NODE_ENV !== 'production') {
  warning = function () {
    if (!valid && console !== undefined) {
      console.error(message)
    }
  }
} 

另外,console !== undefined 可以移除,客户端和服务端不是都有 console 对象么?

@zombieJ
Copy link
Member

zombieJ commented May 6, 2022

直接 PR 吧

@afc163
Copy link
Member

afc163 commented May 6, 2022

@zombieJ 我忘了我们为啥不用 https://github.com/BerkeleyTrue/warning

@semdy
Copy link

semdy commented May 7, 2022

能不能用console.warn,有些不兼容写法一时半会改不了,console面板里又有会抛错,太影响开发了。逼得我只能通过alias将warning的路径指向本地的文件,改写里面的代码。

@KAROTT7
Copy link
Author

KAROTT7 commented May 7, 2022

同意楼上,我也觉得Antd里应该改一下 console 等级,同时增加 rc-util/src/warning 中的 note 函数来警告
对于不影响项目运行(比如有些 api 已经提示废弃其实仍然可以使用)的使用 console.warn; 对于已经废弃且使用后没有效果的 api 使用 console.error 来提示。

@zombieJ
Copy link
Member

zombieJ commented May 7, 2022

@zombieJ 我忘了我们为啥不用 https://github.com/BerkeleyTrue/warning

最早是 antd 里自行实现了 warning,目的是只 warning 用户一次。然后 #47 抽成了通用 util,这样在 rc 里也可以用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants