[cc lang=”javascript”]
const path = require(‘path’);
const CompressionWebpackPlugin = require(‘compression-webpack-plugin’);
const UglifyJsPlugin = require(‘uglifyjs-webpack-plugin’);
const productionGzipExtensions = [‘js’, ‘css’];
const isProduction = process.env.NODE_ENV === ‘production’;
function resolve(dir) {
return path.join(__dirname, dir);
}
// 预发布环境
const isLocalBuild = process.env.IS_LOCAL_BUILD === ‘isLocalBuild’;
console.log(‘前端文件预发布打包- isLocalBuild:’, isLocalBuild);
// 非externals CND前缀设置
const CDN_URL = isLocalBuild ? ‘/’ : ‘//s.zypj.nasetech.com/’;
// 区分生产环境打包和预发布打包,使用不同的CDN
const JS_CDN = isLocalBuild ? [
// 预发布CDN
] : [
// 生产CDN
];
const cdn = {
// css: [],
js: JS_CDN
}
module.exports = {
lintOnSave: true,
baseUrl: isProduction ? CDN_URL : ‘/’,
chainWebpack: (config) => {
// build打包才使用CDN
if (isProduction) {
config.plugin(‘html’)
.tap(args => {
args[0].cdn = cdn;
return args;
})
}
config.resolve.alias
.set(‘assets’, resolve(‘src/assets’))
.set(‘pages’, resolve(‘src/pages’))
.set(‘components’, resolve(‘src/components’))
.set(‘utils’, resolve(‘src/utils’))
},
devServer: {
host: ‘0.0.0.0’,
port: 8080,
https: false,
hotOnly: false,
disableHostCheck: false,
proxy: {
‘/api/v0/’: {
// 目标 API 地址
target: ‘http://127.0.0.1:4545’,
// 将主机标头的原点更改为目标URL
changeOrigin: true,
},
},
},
configureWebpack: config => {
// 生产模式
if (isProduction) {
config.externals = {
‘vue’: ‘Vue’,
‘vue-router’: ‘VueRouter’,
‘moment’: ‘moment’
}
// 打包生产.gz包
config.plugins.push(new CompressionWebpackPlugin({
algorithm: ‘gzip’,
test: new RegExp(‘\\.(‘ + productionGzipExtensions.join(‘|’) + ‘)$’),
threshold: 10240,
minRatio: 0.8
}))
// 添加自定义代码压缩配置
config.plugins.push(
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false,
drop_debugger: true,
drop_console: true,
},
},
sourceMap: false,
parallel: true,
})
)
}
}
}[/cc]