2019-12-12 19:28:59 +08:00
const CompressionWebpackPlugin = require ( 'compression-webpack-plugin' )
2019-09-25 17:08:45 +08:00
const VueFilenameInjector = require ( '@d2-projects/vue-filename-injector' )
2019-06-11 21:24:38 +08:00
const ThemeColorReplacer = require ( 'webpack-theme-color-replacer' )
const forElementUI = require ( 'webpack-theme-color-replacer/forElementUI' )
2019-12-13 19:45:18 +08:00
const cdnDependencies = require ( './dependencies-cdn' )
2020-05-08 09:44:14 +08:00
const { chain , set , each } = require ( 'lodash' )
2019-06-11 21:24:38 +08:00
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
// 基础路径 注意发布之前要先修改这里
2020-05-08 09:44:14 +08:00
const publicPath = process . env . VUE _APP _PUBLIC _PATH || '/'
2018-07-20 18:24:47 +08:00
2019-12-14 11:18:05 +08:00
// 设置不参与构建的库
2020-05-08 09:44:14 +08:00
const externals = { }
2020-02-17 20:45:13 +08:00
cdnDependencies . forEach ( pkg => { externals [ pkg . name ] = pkg . library } )
2019-12-13 10:34:20 +08:00
// 引入文件的 cdn 链接
const cdn = {
2019-12-13 19:45:18 +08:00
css : cdnDependencies . map ( e => e . css ) . filter ( e => e ) ,
js : cdnDependencies . map ( e => e . js ) . filter ( e => e )
2019-12-13 10:34:20 +08:00
}
2020-02-17 22:32:06 +08:00
// 多页配置,默认未开启,如需要请参考 https://cli.vuejs.org/zh/config/#pages
const pages = undefined
// const pages = {
// index: './src/main.js',
// subpage: './src/subpage.js'
// }
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 : {
2020-02-17 21:04:59 +08:00
publicPath , // 和 publicPath 保持一致
disableHostCheck : process . env . NODE _ENV === 'development' // 关闭 host check, 方便使用 ngrok 之类的内网转发工具
2018-07-16 22:22:55 +08:00
} ,
2018-11-16 22:49:27 +08:00
css : {
loaderOptions : {
// 设置 scss 公用变量文件
sass : {
2020-05-08 09:44:14 +08:00
prependData : '@import \'~@/assets/style/public.scss\';'
2018-11-16 22:49:27 +08:00
}
}
} ,
2020-02-17 22:32:06 +08:00
pages ,
2019-12-13 10:34:20 +08:00
configureWebpack : config => {
const configNew = { }
if ( process . env . NODE _ENV === 'production' ) {
configNew . externals = externals
configNew . plugins = [
// gzip
new CompressionWebpackPlugin ( {
filename : '[path].gz[query]' ,
2019-12-14 01:42:14 +08:00
test : new RegExp ( '\\.(' + [ 'js' , 'css' ] . join ( '|' ) + ')$' ) ,
2019-12-13 10:34:20 +08:00
threshold : 10240 ,
minRatio : 0.8 ,
deleteOriginalAssets : false
} )
]
}
return configNew
2019-12-12 19:28:59 +08:00
} ,
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 => {
2019-12-13 10:34:20 +08:00
/ * *
* 添加 CDN 参数到 htmlWebpackPlugin 配置中
2020-02-17 22:32:06 +08:00
* 已适配多页
2019-12-13 10:34:20 +08:00
* /
2020-02-17 22:32:06 +08:00
const htmlPluginNames = chain ( pages ) . keys ( ) . map ( page => 'html-' + page ) . value ( )
const targetHtmlPluginNames = htmlPluginNames . length ? htmlPluginNames : [ 'html' ]
each ( targetHtmlPluginNames , name => {
config . plugin ( name ) . tap ( options => {
set ( options , '[0].cdn' , process . env . NODE _ENV === 'production' ? cdn : [ ] )
return options
} )
2019-12-13 10:34:20 +08:00
} )
2020-02-17 22:32:06 +08:00
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主色系列
] ,
2020-05-08 09:44:14 +08:00
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.
2019-06-14 22:16:44 +08:00
changeSelector : forElementUI . changeSelector
} ] )
2018-11-18 11:07:29 +08:00
config
2019-12-14 01:42:14 +08:00
// 开发环境 sourcemap 不包含列信息
2018-11-18 11:07:29 +08:00
. when ( process . env . NODE _ENV === 'development' ,
config => config . devtool ( 'cheap-source-map' )
)
2019-12-12 10:00:30 +08:00
// 预览环境构建 vue-loader 添加 filename
2020-02-17 21:06:27 +08:00
. when (
process . env . VUE _APP _SCOURCE _LINK === 'TRUE' ,
config => VueFilenameInjector ( config , {
2019-05-06 10:49:25 +08:00
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' ) )
2019-12-13 08:09:04 +08:00
// 分析工具
2019-12-13 08:31:26 +08:00
if ( process . env . npm _config _report ) {
config
. plugin ( 'webpack-bundle-analyzer' )
. use ( require ( 'webpack-bundle-analyzer' ) . BundleAnalyzerPlugin )
}
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
}
}