'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'vue Element Admin' // page title
const port = process.env.port || process.env.npm_config_port || 9527 // dev port
// https://cli.vuejs.org/config/
module.exports = {
// 相對路徑,構建出來的包存放的路徑
publicPath: '/',
// 構建出來的包輸出的目錄 [default: dist]
outputDir: 'dist',
// 防止靜態資源的目錄
assetsDir: 'static',
// 保存使用eslint-loader檢查
lintOnSave: process.env.NODE_ENV === 'development',
// 不需要生產環境的sourceMap,設為false加速生產環境構建
productionSourceMap: false,
// 用於改變原devServer配置項的默認行為
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
before: require('./mock/mock-server.js'),
// 在開發環境下將api請求代理到api服務器
proxy: {
'/api': {
target: 'http://192.168.1.192:13012',
changeOrigin: true,
secure: false,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
},
/**
* webpack相關配置,基於webpack-merge
* object:合並至最終的配置中
* function:會接受被解析的配置作為參數
* */
configureWebpack: {
name: name,
resolve: {
alias: {
'@': resolve('src')
}
}
},
// 基於webpack-chain對內部的webpack配置進行更細粒度的修改
chainWebpack(config) {
// 它可以提高首屏的速度,建議開啟預加載
config.plugin('preload').tap(() => [
{
rel: 'preload',
fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
include: 'initial'
}
])
// 當頁面太多時,會導致太多無意義的請求
config.plugins.delete('prefetch')
// 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()
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
// 打包代碼分割
config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // 僅限於最初依賴的第三方
},
elementUI: {
name: 'chunk-elementUI', // 將elementUI拆分為單個包
priority: 20, // 重量需要大於libs和app,否則將打包到libs或app中
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 為了適應cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // 您可以自定義您的規則
minChunks: 3, // 最小公共數
priority: 5,
reuseExistingChunk: true
}
}
})
// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single')
}
)
}
}