打包优化
- webpack 优化
- 1、依赖转化,兼容低版本浏览器
- 2、生产环境关闭sourceMap
- 3、打包输出目录名称修改和静态资源的存放
- 4、修改图标
- 5、修改webpack配置
- 5-1、写在此处的配置可以覆盖掉脚手架本来就预设上有的配置
- 5-2、写在此处的都是预设没有配置的,脚手架本来就有的配置是不会覆盖的
- 6、完整配置
- vite 优化
webpack 优化
1、依赖转化,兼容低版本浏览器
transpileDependencies: true,
2、生产环境关闭sourceMap
productionSourceMap: false,
3、打包输出目录名称修改和静态资源的存放
outputDir: 'bundle',
assetsDir: 'static',
4、修改图标
pwa: {
iconPaths: {
favicon32: 'favicon.ico',
favicon16: 'favicon.ico',
appleTouchIcon: 'favicon.ico',
maskIcon: 'favicon.ico',
msTileImage: 'favicon.ico',
}
}
5、修改webpack配置
5-1、写在此处的配置可以覆盖掉脚手架本来就预设上有的配置
chainWebpack: config => {
config.optimization.minimizer('terser').tap(args => {
args.forEach(item => {
if (item.hasOwnProperty('terserOptions')) {
Object.assign(item['terserOptions'].compress, {
drop_debugger: true,
drop_console: true,
pure_funcs: ['console.log']
})
}
item['terserOptions']['format'] = {
comments: false
}
})
return args
})
if (process.env.NODE_ENV === "production") {
config.plugin('CompressionPlugin').use(
new CompressionWebpackPlugin({
test: /\.(js|css|less|scss|html)$/,
threshold: 10240,
deleteOriginalAssets: false,
minRatio: 0.8,
algorithm: 'gzip'
})
)
}
}
5-2、写在此处的都是预设没有配置的,脚手架本来就有的配置是不会覆盖的
configureWebpack: {
optimization: {
splitChunks: {
chunks: "all",
cacheGroups: {
elementUI: {
name: "element-chunk-vendors",
test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
priority: 30,
},
vue: {
name: "vue-chunk-vendors",
test: /[\\/]node_modules[\\/]vue(.*)[\\/]/,
chunks: "initial",
priority: 20,
reuseExistingChunk: true,
},
vueRouter: {
name: "vueRouter-chunk-vendors",
test: /[\\/]node_modules[\\/]vue-router(.*)[\\/]/,
chunks: "initial",
priority: 19,
},
vuex: {
name: "vuex-chunk-vendors",
test: /[\\/]node_modules[\\/]vuex(.*)[\\/]/,
chunks: "initial",
priority: 18,
},
echarts: {
name: "echarts-chunk-vendors",
test: /[\\/]node_modules[\\/]echarts(.*)[\\/]/,
chunks: "initial",
priority: 17,
},
libs: {
name: "chunk-libs-vendors",
test: /[\\/]node_modules[\\/]/,
priority: 1,
chunks: "initial",
},
default: {
test: /[\\/]src(.*)[\\/]/,
name: "common-chunk",
minSize: 20000,
minChunks: 2,
priority: -10,
reuseExistingChunk: true
}
}
}
},
resolve: {
alias: {
"#": path.resolve(__dirname, "src")
}
},
plugins: [
new BundleAnalyzer({
analyzerMode: 'server',
analyzerHost: '127.0.0.1',
analyzerPort: 8088,
reportFilename: 'report.html',
defaultSizes: 'parsed',
openAnalyzer: true,
generateStatsFile: false,
statsFilename: 'state.json',
statsOptions: null,
logLevel: 'info'
})
]
}
6、完整配置
const path = require("path")
const { defineConfig } = require('@vue/cli-service')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const BundleAnalyzer = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
module.exports = defineConfig({
transpileDependencies: true,
productionSourceMap: false,
outputDir: 'bundle',
assetsDir: 'static',
pwa: {
iconPaths: {
favicon32: 'favicon.ico',
favicon16: 'favicon.ico',
appleTouchIcon: 'favicon.ico',
maskIcon: 'favicon.ico',
msTileImage: 'favicon.ico',
}
},
chainWebpack: config => {
config.optimization.minimizer('terser').tap(args => {
args.forEach(item => {
if (item.hasOwnProperty('terserOptions')) {
Object.assign(item['terserOptions'].compress, {
drop_debugger: true,
drop_console: true,
pure_funcs: ['console.log']
})
}
item['terserOptions']['format'] = {
comments: false
}
})
return args
})
if (process.env.NODE_ENV === "production") {
config.plugin('CompressionPlugin').use(
new CompressionWebpackPlugin({
test: /\.(js|css|less|scss|html)$/,
threshold: 10240,
deleteOriginalAssets: false,
minRatio: 0.8,
algorithm: 'gzip'
})
)
}
},
configureWebpack: {
optimization: {
splitChunks: {
chunks: "all",
cacheGroups: {
elementUI: {
name: "element-chunk-vendors",
test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
priority: 30,
},
vue: {
name: "vue-chunk-vendors",
test: /[\\/]node_modules[\\/]vue(.*)[\\/]/,
chunks: "initial",
priority: 20,
reuseExistingChunk: true,
},
vueRouter: {
name: "vueRouter-chunk-vendors",
test: /[\\/]node_modules[\\/]vue-router(.*)[\\/]/,
chunks: "initial",
priority: 19,
},
vuex: {
name: "vuex-chunk-vendors",
test: /[\\/]node_modules[\\/]vuex(.*)[\\/]/,
chunks: "initial",
priority: 18,
},
echarts: {
name: "echarts-chunk-vendors",
test: /[\\/]node_modules[\\/]echarts(.*)[\\/]/,
chunks: "initial",
priority: 17,
},
libs: {
name: "chunk-libs-vendors",
test: /[\\/]node_modules[\\/]/,
priority: 1,
chunks: "initial",
},
default: {
test: /[\\/]src(.*)[\\/]/,
name: "common-chunk",
minSize: 20000,
minChunks: 2,
priority: -10,
reuseExistingChunk: true
}
}
}
},
resolve: {
alias: {
"#": path.resolve(__dirname, "src")
}
},
plugins: [
new BundleAnalyzer({
analyzerMode: 'server',
analyzerHost: '127.0.0.1',
analyzerPort: 8088,
reportFilename: 'report.html',
defaultSizes: 'parsed',
openAnalyzer: true,
generateStatsFile: false,
statsFilename: 'state.json',
statsOptions: null,
logLevel: 'info'
})
]
}
})
"build": "vue-cli-service build",
"build:analyze": "cross-env NODE_ENV=production npm_config_report=true vue-cli-service build"
vite 优化