项目 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

相关推荐
古茗前端团队1 分钟前
视频播放弱网提示实现
react.js
哈__13 分钟前
入门小白到精通,玩转 React Native 鸿蒙跨平台开发:Button 按钮组件与点击事件
react native·react.js·harmonyos
哈__13 分钟前
React Native 鸿蒙开发:内置 Share 模块实现无配置社交分享
javascript·react native·react.js
怕浪猫1 小时前
React从入门到出门 第五章 React Router 配置与原理初探
前端·javascript·react.js
哈__1 小时前
React Native 鸿蒙跨平台开发:LayoutAnimation 实现鸿蒙端表单元素的动态添加动画
react native·react.js·harmonyos
哈__1 小时前
React Native 鸿蒙跨平台开发:Vibration 实现鸿蒙端设备的震动反馈
javascript·react native·react.js
哈__2 小时前
React Native 鸿蒙跨平台开发:LayoutAnimation 实现鸿蒙端页面切换的淡入淡出过渡动画
javascript·react native·react.js
哈__2 小时前
React Native 鸿蒙跨平台开发:Animated 实现鸿蒙端组件的上下滑动入场动画
react native·react.js·harmonyos
UIUV16 小时前
模块化CSS学习笔记:从作用域问题到实战解决方案
前端·javascript·react.js
用户120391129472616 小时前
使用 Tailwind CSS 构建现代登录页面:从 Vite 配置到 React 交互细节
前端·javascript·react.js