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

相关推荐
小林ixn1 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
半个落月9 小时前
用 React 搭一个 WebGPU 模型加载页:从状态驱动到可复用进度条
前端·react.js
无人生还10 小时前
第 1 篇:从 Vue3 到 React 的思维转变
前端·vue.js·react.js
breeze jiang11 小时前
从原生事件到 React 组件化:用状态驱动模型加载进度 UI
前端·react.js·ui
名字还没想好☜12 小时前
React setState 连续调用「丢更新」排查:批量更新与函数式更新
前端·javascript·react.js·react·usestate
sugar__salt12 小时前
DeepSeek-R1 WebGPU (1):在浏览器里跑大模型
react.js·reactjs·react·端模型
张元清13 小时前
React useCookie Hook:把 Cookie 变成响应式状态(2026)
javascript·react.js
索西引擎14 小时前
【React】Immer.js 在现代 Redux 生态中的角色:不可变性保障的工程化实现与开发体验优化
前端·javascript·react.js
kisshyshy14 小时前
《川剧变脸 × React 状态管理?我在浏览器里跑了个端侧大模型》
前端·react.js·架构
只一14 小时前
端侧AI实战第二章:React组件工程化 + 事件系统 + 可复用进度条(WebGPU模型加载底座)
前端·react.js