- 提取
.env.development
.env.dev-prod
.env.production
.env.test
等配置- 将
base_api
base_url
dist_url
放到配置文件 - 修改
scripts
中的命令
- 将
jenkins
流水线持续集成- cdn加速
- 剔除
console/debugger
- 全局样式引用
- 合理使用
mixin
- 模块化
vuex
router
- 预加载
prefetch
- 路由懒加载
- 配置文件夹别名
- 去除map文件
productinSourceMap: process.env.NODE_ENV !== "production",
- 分离css
优化less/sass
css: {
sourceMap: process.env.NODE_ENV === 'development',
requireModuleExtension: true,
loaderOptions: {
css: {
modules: {
getLocalIdent: (context, localIdentName, localName, options) => {
let i = 0
while(i < cssModuleExcludes.length) {
if (cssModuleExcludes[i].test(localName)) return localName
i += 1
}
let filePath = context.context.replace(context.rootContext, '')
let filename = context.resourcePath.replace(context.context, '').replace(/^\/|\\|\.vue$/g, '')
filePath = upath.normalize(filePath)
filePath = filePath.replace(/\/src\/components\//, '/c/')
filePath = filePath.replace(/\/src\/views\//, '/v/')
filePath = filePath.split(/\/|\\/).filter(i => i.length).join('-')
if (filename === 'index') return `${filePath}__${localName}`
return `${filePath}-${filename}__${localName}`
},
},
},
// pass options to less-loader
less: {
lessOptions: {
modifyVars: {
'primary-color': '#3977EB',
'font-family': ' "Microsoft YaHei","Chinese Quote","PingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans GB", "Helvetica Neue", Helvetica, Arial, sans-serif,"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
},
javascriptEnabled: true
},
},
}
},
图片压缩
lodash
优化打包结果分析
chainWebpack(config) {
config.plugin("lodashReplace")
.use(new LodashModuleReplacementPlugin());//优化lodash
// 移除 prefetch 插件
config.plugins.delete('prefetch')
// 移除 preload 插件
config.plugins.delete('preload')
const entry = config.entry('app')
entry.add('babel-polyfill')
// 关闭打包大小限制
config.performance.set('hints', false)
// 进行打包情况的监测
if (process.env.analyzer) {
config
.plugin('webpack-bundle-analyzer')
.use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
}
config
.plugin('ignore-moment')
.use(new webpack.IgnorePlugin(
/^\.\/locale$/,
// /^\.\/locale\/zh-cn$/,
/moment$/
))//忽略/moment/locale下的所有文件
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
},
公共代码抽离
开启
gzip
configureWebpack: config => {
const conf = {
output: {
sourcePrefix: ' '
},
amd: {
toUrlUndefined: true
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve('src'),
}
},
plugins: [],
}
// 添加gzip压缩
if (process.env.NODE_ENV === 'production') {
conf.plugins.push(new CompressionPlugin({
test: /\.js$|\.html$|\.css$/, // 匹配文件名
threshold: 10240, // 对超过 10K 的数据进行压缩
deleteOriginalAssets: false,
}))
}
return conf
}