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

代码里console.log比较多,该怎么办【热度: 340】 #1080

Open
yanlele opened this issue Jan 2, 2025 · 0 comments
Open

代码里console.log比较多,该怎么办【热度: 340】 #1080

yanlele opened this issue Jan 2, 2025 · 0 comments
Labels
web应用场景 应用场景类问题
Milestone

Comments

@yanlele
Copy link
Member

yanlele commented Jan 2, 2025

  1. ESLint配置规则软性禁止:通过配置.eslintrc.json文件,添加"no-console": "warn"规则,使代码中使用console的地方会划上黄色波浪线警示,能一定程度削减console.log数量,但无法真正阻止其使用。
  2. git commit编写规则限制提交:找到项目中的.git/hooks文件夹下的pre-commit.sample文件,将其内容修改为若提交代码中包含console.log则报错提交失败,并将文件重命名为pre-commit。但该规则可被git commit -m 'xxx' --no-verify指令绕过。
  3. 依托于 cicd 的自动检测: 在流水线部署的时候跑 eslint, 如果 console.log 代码增加, 就 拒绝部署即可;
  4. 使用插件删除
    • VSCODE插件:可在插件商店搜索remove-console并安装,找到有console.log的文件使用插件删除,但效果可能不太理想。
    • Webpack插件
      • 可使用terser-webpack-plugin,在项目基于create-react-app脚手架时可直接搜到,在使用处配置drop_console: true,能在打包后去除全部console.log

参考文档:

@yanlele yanlele added the web应用场景 应用场景类问题 label Jan 2, 2025
@yanlele yanlele added this to the milestone Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
web应用场景 应用场景类问题
Projects
None yet
Development

No branches or pull requests

1 participant