Taro小程序压缩

taro使用的是3.0版本,压缩插件是terser-webpack-plugin

在config目录下的index.js进行配置

import path from 'path';
import TerserPlugin from "terser-webpack-plugin" //(如果包里有的话直接引入,不用安装,没有的话要安装 cnpm install --save terser-webpack-plugin)

const config = {
  projectName: 'taro-react-starter',
  date: '2020-6-12',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [
    '@tarojs/plugin-less', 
    '@tarojs/plugin-stylus', 
    '@tarojs/plugin-sass', 
  ],
  alias: {
    '@/assets': path.resolve(__dirname, '../src/assets'),
    '@/components': path.resolve(__dirname, '../src/components'),
    '@/models': path.resolve(__dirname, '../src/models'),
    '@/pages': path.resolve(__dirname, '../src/pages'),
    '@/utils': path.resolve(__dirname, '../src/utils'),
    '@/config': path.resolve(__dirname, '../src/config'),
  },
  defineConstants: {},
  copy: {
    patterns: [],
    options: {},
  },
  framework: 'react',
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {},
      },
      url: {
        enable: true,
        config: {
          limit: 1024, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
   //压缩配置
    webpackChain(chain, webpack) {
      if(process.env.NODE_ENV !== 'development'){//只在生产环境下生效
              chain.mode("production");
         chain.optimization.minimize(true);
         chain.plugin("terser").use(TerserPlugin, [
           {
             cache: true,
             extractComments: false,
                  parallel: true,
             terserOptions: {
               output: {
                 comments: false
               },
                    compress: {
                       pure_funcs: ["console.log"],//过滤掉打印
                    }
             }
           }
         ]); 
      }
       
   }
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {},
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
  },
};

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

你可能感兴趣的:(Taro小程序压缩)