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' )
2020-12-03 22:03:58 +08:00
const { set , each , compact , map , keys } = require ( 'lodash' )
2019-06-11 21:24:38 +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
2020-12-03 22:03:58 +08:00
// Add environment variable
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' )
2020-12-03 22:03:58 +08:00
// Build configuration for multiple pages
const pages = {
index : {
entry : 'src/main.js' ,
template : 'public/index.html' ,
filename : 'index.html' ,
chunks : [
'manifest' ,
'index' ,
'chunk-index' ,
'chunk-vendor' ,
'chunk-common' ,
'chunk-vue' ,
'chunk-element'
]
} ,
mobile : {
entry : 'src.mobile/main.js' ,
template : 'public/mobile.html' ,
filename : 'mobile.html' ,
chunks : [
'manifest' ,
'mobile' ,
'chunk-mobile' ,
'chunk-vendor' ,
'chunk-common' ,
'chunk-vue'
]
}
}
2019-12-13 10:34:20 +08:00
2020-12-03 22:03:58 +08:00
// Set up the external dependency package introduced by the method of using CDN
// For example
// if you set the Axios related link configuration here
// Axios will no longer participate in the packaging during the construction. Finally
// the external link you configured will be used to import Axios in the build result
2019-12-13 10:34:20 +08:00
const cdn = {
2020-12-03 22:03:58 +08:00
// Which external dependencies related to index page are introduced in the form of CDN links
index : [
// {
// name: 'axios',
// library: 'axios',
// js: 'https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js',
// css: ''
// }
] ,
// Which external dependencies related to mobile page are introduced in the form of CDN links
mobile : [
// {
// name: 'axios',
// library: 'axios',
// js: 'https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js',
// css: ''
// }
]
2019-12-13 10:34:20 +08:00
}
2020-12-03 22:03:58 +08:00
// Set external dependent packages that do not participate in the build
const externals = { }
keys ( pages ) . forEach ( name => {
cdn [ name ] . forEach ( p => {
externals [ p . name ] = p . library
} )
} )
2020-02-17 22:32:06 +08:00
2018-07-16 22:22:55 +08:00
module . exports = {
2020-12-03 22:03:58 +08:00
publicPath : process . env . VUE _APP _PUBLIC _PATH || '/' ,
2018-07-16 22:22:55 +08:00
lintOnSave : true ,
devServer : {
2020-12-03 22:03:58 +08:00
publicPath : process . env . VUE _APP _PUBLIC _PATH || '/' ,
disableHostCheck : process . env . NODE _ENV === 'development'
2018-07-16 22:22:55 +08:00
} ,
2018-11-16 22:49:27 +08:00
css : {
loaderOptions : {
sass : {
2021-07-20 22:05:22 +08:00
additionalData : '@use "@/assets/style/public.scss" as *;'
2020-12-03 22:03:58 +08:00
} ,
less : {
lessOptions : {
modifyVars : {
blue : '#2262AB'
}
}
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
} ,
2020-12-03 22:03:58 +08:00
// default: 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 => {
2020-12-03 22:03:58 +08:00
config . optimization . runtimeChunk ( {
name : 'manifest'
} )
config . optimization . splitChunks ( {
cacheGroups : {
// External dependencies common to all pages
libs : {
name : 'chunk-vendor' ,
chunks : 'initial' ,
minChunks : 1 ,
test : /[\\/]node_modules[\\/]/ ,
priority : 1 ,
reuseExistingChunk : true ,
enforce : true
} ,
// Code common to all pages
common : {
name : 'chunk-common' ,
chunks : 'initial' ,
minChunks : 2 ,
maxInitialRequests : 5 ,
minSize : 0 ,
priority : 2 ,
reuseExistingChunk : true ,
enforce : true
} ,
// External dependencies that are only used by the index page
index : {
name : 'chunk-index' ,
chunks : 'all' ,
minChunks : 1 ,
test : /[\\/]node_modules[\\/](sortablejs|screenfull|nprogress|hotkeys-js|fuse\.js|better-scroll|lowdb|shortid)[\\/]/ ,
priority : 3 ,
reuseExistingChunk : true ,
enforce : true
} ,
// External dependencies that are only used by the mobile page
mobile : {
name : 'chunk-mobile' ,
chunks : 'all' ,
minChunks : 1 ,
test : /[\\/]node_modules[\\/](vant)[\\/]/ ,
priority : 3 ,
reuseExistingChunk : true ,
enforce : true
} ,
// Vue family packages
vue : {
name : 'chunk-vue' ,
test : /[\\/]node_modules[\\/](vue|vue-router|vuex)[\\/]/ ,
chunks : 'all' ,
priority : 3 ,
reuseExistingChunk : true ,
enforce : true
} ,
// only element-ui
element : {
name : 'chunk-element' ,
test : /[\\/]node_modules[\\/]element-ui[\\/]/ ,
chunks : 'all' ,
priority : 3 ,
reuseExistingChunk : true ,
enforce : true
}
}
} )
// Add the CDN settings to the settings of the htmlwebpackplugin plug-in
keys ( pages ) . forEach ( name => {
const packages = cdn [ name ]
config . plugin ( ` html- ${ name } ` ) . tap ( options => {
const setting = {
css : compact ( map ( packages , 'css' ) ) ,
js : compact ( map ( packages , 'js' ) )
}
set ( options , '[0].cdn' , process . env . NODE _ENV === 'production' ? setting : [ ] )
2020-02-17 22:32:06 +08:00
return options
} )
2019-12-13 10:34:20 +08:00
} )
2020-12-03 22:03:58 +08:00
// Remove prefetch preload settings for lazy load modules
each ( keys ( pages ) , name => {
config . plugins . delete ( ` prefetch- ${ name } ` )
config . plugins . delete ( ` preload- ${ name } ` )
} )
// webpack-theme-color-replacer
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
2020-12-03 22:03:58 +08:00
// The development environment sourcemap does not contain column information
2018-11-18 11:07:29 +08:00
. when ( process . env . NODE _ENV === 'development' ,
config => config . devtool ( 'cheap-source-map' )
)
2020-12-03 22:03:58 +08:00
// Add file name
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 ( )
2020-12-03 22:03:58 +08:00
// set alias
2018-07-19 17:00:45 +08:00
config . resolve . alias
2020-12-03 22:03:58 +08:00
. set ( '@.mobile' , resolve ( 'src.mobile' ) )
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
}
}