2019-12-12 19:28:59 +08:00
const CompressionWebpackPlugin = require ( 'compression-webpack-plugin' )
2019-12-12 20:09:31 +08:00
const UglifyJsPlugin = require ( 'uglifyjs-webpack-plugin' )
const OptimizeCssAssetsPlugin = require ( 'optimize-css-assets-webpack-plugin' )
2019-09-25 17:08:45 +08:00
const VueFilenameInjector = require ( '@d2-projects/vue-filename-injector' )
2019-05-06 10:49:25 +08:00
2019-06-11 21:24:38 +08:00
const ThemeColorReplacer = require ( 'webpack-theme-color-replacer' )
const forElementUI = require ( 'webpack-theme-color-replacer/forElementUI' )
2018-07-16 22:22:55 +08:00
// 拼接路径
2018-10-27 18:48:22 +08:00
const resolve = dir => require ( 'path' ) . join ( _ _dirname , dir )
2018-07-16 22:22:55 +08:00
2018-12-14 11:16:10 +08:00
// 增加环境变量
process . env . VUE _APP _VERSION = require ( './package.json' ) . version
process . env . VUE _APP _BUILD _TIME = require ( 'dayjs' ) ( ) . format ( 'YYYY-M-D HH:mm:ss' )
2018-07-20 18:24:47 +08:00
// 基础路径 注意发布之前要先修改这里
2019-10-11 11:10:39 +08:00
let publicPath = process . env . VUE _APP _PUBLIC _PATH || '/'
2018-07-20 18:24:47 +08:00
2018-07-16 22:22:55 +08:00
module . exports = {
2019-05-21 23:51:29 +08:00
// 根据你的实际情况更改这里
publicPath ,
2018-07-16 22:22:55 +08:00
lintOnSave : true ,
devServer : {
2019-01-17 11:33:11 +08:00
publicPath // 和 publicPath 保持一致
2018-07-16 22:22:55 +08:00
} ,
2018-11-16 22:49:27 +08:00
css : {
loaderOptions : {
// 设置 scss 公用变量文件
sass : {
data : ` @import '~@/assets/style/public.scss'; `
}
}
} ,
2019-12-12 19:28:59 +08:00
configureWebpack : {
2019-12-12 20:26:16 +08:00
externals : {
'vue' : 'Vue' ,
'vue-router' : 'VueRouter' ,
'vuex' : 'Vuex'
} ,
2019-12-12 20:20:02 +08:00
// 关闭文件过大提示,利于打包加快速度
performance : {
hints : 'warning' ,
// 入口起点的最大体积
maxEntrypointSize : 50000000 ,
// 生成文件的最大体积
maxAssetSize : 30000000 ,
// 只给出 js 文件的性能提示
assetFilter : function ( assetFilename ) {
return assetFilename . endsWith ( '.js' ) ;
}
} ,
// 公共代码抽离和代码分割
2019-12-12 20:09:31 +08:00
optimization : {
runtimeChunk : 'single' ,
2019-12-12 20:20:02 +08:00
splitChunks : {
cacheGroups : {
vendors : {
test : /[\\/]node_modules[\\/]/ ,
name : 'vendors' ,
minSize : 30000 ,
minChunks : 1 ,
chunks : 'initial' ,
priority : 1 // 设置处理的优先级,数值越大越优先处理
} ,
commons : {
test : /[\\/]src[\\/]common[\\/]/ ,
name : 'commons' ,
minSize : 30000 ,
minChunks : 3 ,
chunks : 'initial' ,
priority : - 1 ,
reuseExistingChunk : true // 允许使用已经存在的代码块
} ,
styles : {
name : 'styles' ,
test : /\.(sa|sc|c)ss$/ ,
chunks : 'all' ,
enforce : true
} ,
runtimeChunk : {
name : 'manifest'
}
}
} ,
2019-12-12 20:09:31 +08:00
minimizer : [
// 自定义js优化配置, 将会覆盖默认配置
new UglifyJsPlugin ( {
2019-12-12 20:20:02 +08:00
exclude : /\.min\.js$/ , // 过滤掉以 '.min.js' 结尾的文件,我们认为这个后缀本身就是已经压缩好的代码,没必要进行二次压缩
2019-12-12 20:09:31 +08:00
cache : true ,
parallel : true , // 开启并行压缩, 充分利用cpu
sourceMap : false ,
extractComments : false , // 移除注释
uglifyOptions : {
compress : {
unused : true ,
2019-12-12 20:20:02 +08:00
drop _console : true ,
drop _debugger : true ,
pure _funcs : [ 'console.log' ]
2019-12-12 20:09:31 +08:00
} ,
output : {
comments : false
} ,
warnings : false
}
} ) ,
// 用于优化css文件
new OptimizeCssAssetsPlugin ( {
assetNameRegExp : /\.css$/g ,
cssProcessorOptions : {
safe : true ,
autoprefixer : { disable : true } ,
mergeLonghand : false ,
discardComments : {
removeAll : true // 移除注释
}
} ,
canPrint : true
} )
]
} ,
2019-12-12 19:28:59 +08:00
plugins : [
// gzip
new CompressionWebpackPlugin ( {
filename : '[path].gz[query]' ,
test : new RegExp ( '\\.(js|css|svg|woff|ttf|json|html)$' ) ,
threshold : 10240 ,
minRatio : 0.8 ,
deleteOriginalAssets : false
} )
]
} ,
2018-07-19 17:00:45 +08:00
// 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
2018-07-16 22:22:55 +08:00
chainWebpack : config => {
2018-11-18 09:45:04 +08:00
/ * *
* 删除懒加载模块的 prefetch preload , 降低带宽压力
* https : //cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
* https : //cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
* 而且预渲染时生成的 prefetch 标签是 modern 版本的 , 低版本浏览器是不需要的
* /
config . plugins
. delete ( 'prefetch' )
. delete ( 'preload' )
2018-09-12 08:10:16 +08:00
// 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
config . resolve
. symlinks ( true )
2019-06-14 22:16:44 +08:00
config
. plugin ( 'theme-color-replacer' )
. use ( ThemeColorReplacer , [ {
fileName : 'css/theme-colors.[contenthash:8].css' ,
matchColors : [
... forElementUI . getElementUISeries ( process . env . VUE _APP _ELEMENT _COLOR ) // Element-ui主色系列
] ,
externalCssFiles : [ './node_modules/element-ui/lib/theme-chalk/index.css' ] , // optional, String or string array. Set external css files (such as cdn css) to extract colors.
changeSelector : forElementUI . changeSelector
} ] )
2018-11-18 11:07:29 +08:00
config
// 开发环境
. when ( process . env . NODE _ENV === 'development' ,
// sourcemap不包含列信息
config => config . devtool ( 'cheap-source-map' )
)
2019-12-12 10:00:30 +08:00
// 预览环境构建 vue-loader 添加 filename
2019-05-10 09:56:55 +08:00
. when ( process . env . VUE _APP _SCOURCE _LINK === 'TRUE' ,
2019-05-06 10:49:25 +08:00
VueFilenameInjector ( config , {
propName : process . env . VUE _APP _SOURCE _VIEWER _PROP _NAME
} )
2019-04-29 22:38:08 +08:00
)
2018-07-16 22:22:55 +08:00
// markdown
config . module
. rule ( 'md' )
. test ( /\.md$/ )
. use ( 'text-loader' )
. loader ( 'text-loader' )
. end ( )
// svg
const svgRule = config . module . rule ( 'svg' )
svgRule . uses . clear ( )
svgRule
. include
2018-07-17 16:14:19 +08:00
. add ( resolve ( 'src/assets/svg-icons/icons' ) )
2018-07-16 22:22:55 +08:00
. end ( )
. use ( 'svg-sprite-loader' )
. loader ( 'svg-sprite-loader' )
. options ( {
symbolId : 'd2-[name]'
} )
. end ( )
2018-07-25 09:47:43 +08:00
// image exclude
2018-07-16 22:22:55 +08:00
const imagesRule = config . module . rule ( 'images' )
imagesRule
. test ( /\.(png|jpe?g|gif|webp|svg)(\?.*)?$/ )
. exclude
2018-07-17 16:14:19 +08:00
. add ( resolve ( 'src/assets/svg-icons/icons' ) )
2018-07-16 22:22:55 +08:00
. end ( )
2018-07-17 21:54:38 +08:00
// 重新设置 alias
2018-07-19 17:00:45 +08:00
config . resolve . alias
2018-12-11 08:48:38 +08:00
. set ( '@api' , resolve ( 'src/api' ) )
2018-11-14 19:48:44 +08:00
// 判断环境加入模拟数据
2019-01-17 11:09:43 +08:00
const entry = config . entry ( 'app' )
2019-05-10 09:56:55 +08:00
if ( process . env . VUE _APP _BUILD _MODE !== 'NOMOCK' ) {
2018-11-14 16:24:56 +08:00
entry
. add ( '@/mock' )
. end ( )
}
2019-05-21 23:51:29 +08:00
} ,
2019-12-12 19:28:59 +08:00
// 不输出 map 文件
productionSourceMap : false ,
2019-05-22 00:19:48 +08:00
// i18n
2019-05-21 23:51:29 +08:00
pluginOptions : {
i18n : {
2019-05-23 21:41:04 +08:00
locale : 'zh-chs' ,
fallbackLocale : 'en' ,
2019-05-21 23:51:29 +08:00
localeDir : 'locales' ,
enableInSFC : true
}
2018-07-16 22:22:55 +08:00
}
}