<备忘录>在Vite上创建的React应用中,一瞬间禁用所有的console.log的方法

在构建时批量停用React中的console.log。

在将应用程序上传到服务器之前,我有一种非常方便的方法可以同时取消所有console.log输出,但是由于经常忘记操作步骤,所以将其记录在备忘录中。

安装 rollup-plugin-strip

npm install @rollup/plugin-strip --save-dev

2、编辑 vite.config 中的 defineConfig()。

import strip from '@rollup/plugin-strip';

export default defineConfig({
  plugins: [
    react(),
    strip({
      // remove all console.log calls
      include: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
      functions: ['console.log'],
    }),
  ],
})

只需要这一点,就可以在构建时抹掉React内的所有console.log。
非常方便。*在构建时,只是省略了console.log,文件本身仍然保留。

广告
将在 10 秒后关闭
bannerAds