当前位置: 首页> 汽车> 时评 > html如何设置背景图片_黄页广告公司_无锡百度推广开户_怎么做一个免费的网站

html如何设置背景图片_黄页广告公司_无锡百度推广开户_怎么做一个免费的网站

时间:2025/7/9 4:10:26来源:https://blog.csdn.net/weixin_42771529/article/details/148231683 浏览次数: 0次
html如何设置背景图片_黄页广告公司_无锡百度推广开户_怎么做一个免费的网站

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

配置步骤:
修改 index.js 文件
在 mini.webpackChain 中添加 terser-webpack-plugin 配置:

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) {// 生产环境移除 consoleif (process.env.NODE_ENV === 'production') {chain.optimization.minimize(true);chain.plugin('terser').use(require('terser-webpack-plugin'), [{terserOptions: {compress: {drop_console: true, // 移除所有 consoledrop_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 配置:

    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) {// 生产环境移除 consoleif (process.env.NODE_ENV === 'production') {chain.optimization.minimize(true);chain.plugin('terser').use(require('terser-webpack-plugin'), [{terserOptions: {compress: {drop_console: true, // 移除所有 consoledrop_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. 构建生产环境代码
    使用以下命令构建生产环境代码:

    pnpm build:weapp --mode production
    

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

    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 中添加更多配置,例如:

    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

关键字:html如何设置背景图片_黄页广告公司_无锡百度推广开户_怎么做一个免费的网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: