Taro小程序壓縮

taro使用的是3.0版本,壓縮插件是terser-webpack-plugin

在config目錄下的index.js進(jìn)行配置

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, // 設(shè)定轉(zhuǎn)換尺寸上限
        },
      },
      cssModules: {
        enable: false, // 默認(rèn)為 false,如需使用 css modules 功能,則設(shè)為 true
        config: {
          namingPattern: 'module', // 轉(zhuǎn)換模式,取值為 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
   //壓縮配置
    webpackChain(chain, webpack) {
      if(process.env.NODE_ENV !== 'development'){//只在生產(chǎn)環(huán)境下生效
              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, // 默認(rèn)為 false,如需使用 css modules 功能,則設(shè)為 true
        config: {
          namingPattern: 'module', // 轉(zhuǎn)換模式,取值為 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'));
};

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容