项目 react+taro 编写的微信 小程序,什么命令,可以减少console的显示

在 Taro 项目中,为了减少 console 的显示(例如 console.log、console.info 等),可以通过配置 terser-webpack-plugin 来移除生产环境中的 console 调用。

配置步骤:

修改 index.js 文件

在 mini.webpackChain 中添加 terser-webpack-plugin 配置:

bash 复制代码
const config = {
  projectName: 'taro-react',
  date: '2025-5-10',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [],
  defineConstants: {},
  copy: {
    patterns: [],
    options: {}
  },
  framework: 'react',
  compiler: 'webpack5',
  cache: {
    enable: false
  },
  mini: {
    webpackChain(chain) {
      // 生产环境移除 console
      if (process.env.NODE_ENV === 'production') {
        chain.optimization.minimize(true);
        chain.plugin('terser').use(require('terser-webpack-plugin'), [{
          terserOptions: {
            compress: {
              drop_console: true, // 移除所有 console
              drop_debugger: true // 移除 debugger
            }
          }
        }]);
      }
    }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {}
      }
    }
  }
};

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'));
  }
  return merge({}, config, require('./prod'));
};

构建生产环境代码

使用以下命令构建生产环境代码:

pnpm build:weapp --mode production

在 Taro 项目中,为了减少 console 的显示(例如 console.logconsole.info 等),可以通过配置 terser-webpack-plugin 来移除生产环境中的 console 调用。

配置步骤:

  1. 修改 index.js 文件

    mini.webpackChain 中添加 terser-webpack-plugin 配置:

    javascript 复制代码
    const config = {
      projectName: 'taro-react',
      date: '2025-5-10',
      designWidth: 750,
      deviceRatio: {
        640: 2.34 / 2,
        750: 1,
        828: 1.81 / 2
      },
      sourceRoot: 'src',
      outputRoot: 'dist',
      plugins: [],
      defineConstants: {},
      copy: {
        patterns: [],
        options: {}
      },
      framework: 'react',
      compiler: 'webpack5',
      cache: {
        enable: false
      },
      mini: {
        webpackChain(chain) {
          // 生产环境移除 console
          if (process.env.NODE_ENV === 'production') {
            chain.optimization.minimize(true);
            chain.plugin('terser').use(require('terser-webpack-plugin'), [{
              terserOptions: {
                compress: {
                  drop_console: true, // 移除所有 console
                  drop_debugger: true // 移除 debugger
                }
              }
            }]);
          }
        }
      },
      h5: {
        publicPath: '/',
        staticDirectory: 'static',
        postcss: {
          autoprefixer: {
            enable: true,
            config: {}
          }
        }
      }
    };
    
    module.exports = function (merge) {
      if (process.env.NODE_ENV === 'development') {
        return merge({}, config, require('./dev'));
      }
      return merge({}, config, require('./prod'));
    };
  2. 构建生产环境代码

    使用以下命令构建生产环境代码:

    bash 复制代码
    pnpm build:weapp --mode production

    或者,如果你使用的是 npm/yarn:

    bash 复制代码
    npm run build:weapp --mode production
    # 或
    yarn build:weapp --mode production
  3. 效果

    • 生产环境中,所有 console.logconsole.infoconsole.debug 等调用都会被移除。
    • console.warnconsole.error 不会被移除(用于调试和错误追踪)。
    • debugger 语句也会被移除。

注意事项:

  • 确保 terser-webpack-plugin 已安装(Taro 默认会安装)。

  • 在开发环境中,console 调用仍然会保留,方便调试。

  • 如果需要移除所有类型的 console,可以在 terserOptions.compress 中添加更多配置,例如:

    javascript 复制代码
    compress: {
      drop_console: true,
      drop_debugger: true,
      pure_funcs: ['console.log', 'console.info', 'console.debug', 'console.warn', 'console.error']
    }

Similar code found with 2 license types

相关推荐
sorryhc3 分钟前
【AI解读源码系列】ant design mobile——CapsuleTabs胶囊选项卡
前端·javascript·react.js
三花AI44 分钟前
微信测试对话式 AI 新闻播报
微信
林太白1 小时前
Vite+React+ts项目搭建(十分钟搭建个最新版React19项目吧)
前端·后端·react.js
彭于晏爱编程2 小时前
密码的,YOU不能不知道的Next.jsSSR(服务端渲染)
前端·javascript·react.js
Moonbit3 小时前
提交即有奖!MGPIC 游戏赛道官方推荐框架上线,直播同步解读赛题。 MoonBit MoonBit
后端·微信·程序员
晴空雨4 小时前
💥 React 容器组件深度解析:从 Props 拦截到事件改写
前端·react.js·设计模式
阿虎儿6 小时前
React 引用(Ref)完全指南
前端·javascript·react.js
阿虎儿6 小时前
React 事件类型完全指南:深入理解合成事件系统
前端·javascript·react.js
梨子同志7 小时前
React18中setState
react.js
golang学习记7 小时前
🔥 从0死磕全栈:以React为起点,开启我的全栈之旅
前端·react.js