react-component / react-component/util

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

Open
#301 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
670
Forks
205
Avg merge
11d 17h
Merged PRs (30d)
4

Description

问题如题所述

复现案例

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 对象么?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the StackBlitz reproduction and the rc-util/es/warning entry point, then inspect the generated dist/assets/index.57005310.js lines 7356-7467. Compare the production bundle with the warning implementation shown in the issue; done means warning code is removed from production output without breaking development behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript, vite
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.