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, // 設(shè)定轉(zhuǎn)換尺寸上限
},
},
cssModules: {
enable: false, // 默認為 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, // 默認為 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'));
};