从Vue CLI 3.0 开始,Webpack的配置文件从webpack.config.js变为了vue.config.js。在实际开发中还要很必要配置一下的,
1.先安装一下插件
npm i uglifyjs-webpack-plugin --save-dev
npm i compression-webpack-plugin --save-dev
cnpm i image-webpack-loader --save-dev(注意,这个一定要用cnpm下载否则最近一直在报错)
2.
const path = require('path')
const UglifyPlugin = require('uglifyjs-webpack-plugin')
const CompressionPlugin = require("compression-webpack-plugin")
module.exports = {
publicPath: './',
outputDir: 'dist',
filenameHashing: true,
lintOnSave: false,
productionSourceMap: false,
chainWebpack: config => {
config.module.rule('images')
.test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
.use('url-loader')
.loader('url-loader')
.options({
limit: 10240
})
.end()
.test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
.use('image-webpack-loader')
.loader('image-webpack-loader')
.options({ bypassOnDebug: true })
.end()
if (process.env.NODE_ENV === 'production') {
config.plugin('compressionPlugin')
.use(new CompressionPlugin({
test:/\.js$|\.html$|.\css/,
threshold: 10240,
deleteOriginalAssets: false
}))
}
},
configureWebpack: (config) => {
if (process.env.NODE_ENV === 'production') {
config.mode = 'production'
config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
const optimization = {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 20000,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name (module) {
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
return `npm.${packageName.replace('@', '')}`
},
common: {
chunks: 'all',
test: /[\\/]src[\\/]js[\\/]/,
name: 'nnnn',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0,
priority: 60
},
}
}
},
用UglifyPlugin也试了,但是不起作用】
minimizer: [new UglifyPlugin({
uglifyOptions: {
warnings: false,
compress: {
drop_console: true,
drop_debugger: false,
pure_funcs: ['console.log']
}
}
})]
}
Object.assign(config, {
optimization
})
} else {
config.mode = 'development'
}
Object.assign(config, {
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@c': path.resolve(__dirname, './src/components'),
'@p': path.resolve(__dirname, './src/pages'),
'@v': path.resolve(__dirname, './src/views'),
}
}
})
},
css: {
extract: true,
sourceMap: true,
requireModuleExtension: true,
loaderOptions: {
css: {
modules: {
localIdentName: '[local]_[hash:base64:8]'
}
}
}
},
parallel: require('os').cpus().length > 1,
devServer: {
open: true,
inline: true,
host: '0.0.0.0',
port: 8024,
https: false,
overlay: {
warnings: true,
errors: true
},
proxy: {
'/api': {
target: '',
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
},
pluginOptions: {}
}
|