加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vue.config.js 6.07 KB
一键复制 编辑 原始数据 按行查看 历史
glf_gao 提交于 2022-12-16 13:58 . 1、优化双token请求逻辑
const path = require('path')
const webpack = require('webpack')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const { getThemeColors, modifyVars } = require('./src/utils/themeUtil')
const { resolveCss } = require('./src/utils/theme-color-replacer-extend')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
function resolve(dir) {
return path.join(__dirname, dir)
}
const productionGzipExtensions = ['js', 'css', 'less']
const isProd = ['production', 'prod'].includes(process.env.NODE_ENV)
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const assetsCDN = {
// webpack build externals
externals: {
vue: 'Vue',
'vue-router': 'VueRouter',
vuex: 'Vuex',
axios: 'axios',
nprogress: 'NProgress',
clipboard: 'ClipboardJS',
'@antv/data-set': 'DataSet',
'xe-utils': 'XEUtils',
'vxe-table': 'VXETable',
'viser-vue': 'ViserVue'
},
css: [],
js: [
'/cdn/vue.min.js',
'/cdn/vue-router.min.js',
'/cdn/vuex.min.js',
'/cdn/axios.min.js',
'/cdn/nprogress.min.js',
'/cdn/clipboard.min.js',
'/cdn/data-set.min.js',
'/cdn/xe-utils.umd.min.js',
'/cdn/vxe-table/3.6.6/index.umd.min.js',
'/cdn/viser-vue.min.js'
// '//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
// '//cdn.jsdelivr.net/npm/vue-router@3.3.4/dist/vue-router.min.js',
// '//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
// '//cdn.jsdelivr.net/npm/axios@0.19.2/dist/axios.min.js',
// '//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js',
// '//cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js',
// '//cdn.jsdelivr.net/npm/@antv/data-set@0.11.4/build/data-set.min.js',
// // '//cdn.jsdelivr.net/npm/js-cookie@2.2.1/src/js.cookie.min.js',
// '//cdn.jsdelivr.net/npm/xe-utils@3.5.4/dist/xe-utils.umd.min.js',
// '//cdn.jsdelivr.net/npm/vxe-table@3.5.4/lib/index.umd.min.js',
// '//cdn.jsdelivr.net/npm/viser-vue@2.4.8/umd/viser-vue.min.js'
]
}
module.exports = {
devServer: {
port: 8080,
proxy: {
[process.env.VUE_APP_API_BASE_URL]: {
// target: 'http://192.168.111.38:9005',
target: 'http://127.0.0.1:9005',
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_API_BASE_URL]: ''
}
}
},
overlay: {
warnings: false,
errors: false
},
compress: true,
disableHostCheck: true // webpack4.0 开启热更新
},
pluginOptions: {
'style-resources-loader': {
preProcessor: 'less',
patterns: [path.resolve(__dirname, './src/theme/theme.less')]
},
windicss: {}
},
configureWebpack: config => {
config.entry.app = ['babel-polyfill', 'whatwg-fetch', './src/main.js']
config.performance = {
hints: false
}
config.resolve.alias = { '@': resolve('src') }
config.plugins.push(
new ThemeColorReplacer({
fileName: 'css/theme-colors-[contenthash:8].css',
matchColors: getThemeColors(),
injectCss: true,
resolveCss
})
)
// 生产环境取消 console.log
if (isProd) {
config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
}
if (isProd) {
config.plugins.push(new BundleAnalyzerPlugin())
}
// Ignore all locale files of moment.js
config.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/))
// 生产环境下将资源压缩成gzip格式
if (isProd) {
// add `CompressionWebpack` plugin to webpack plugins
config.plugins.push(new CompressionWebpackPlugin({
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
}))
}
// if prod, add externals
if (isProd) {
config.externals = assetsCDN.externals
}
},
chainWebpack: config => {
// 生产环境下关闭css压缩的 colormin 项,因为此项优化与主题色替换功能冲突
if (isProd) {
config.plugin('optimize-css')
.tap(args => {
args[0].cssnanoOptions.preset[1].colormin = false
return args
})
}
// 生产环境下使用CDN
if (isProd) {
config.plugin('html')
.tap(args => {
args[0].cdn = assetsCDN
return args
})
}
config
.when(isProd,
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' // only package third parties that are initially dependent
},
Antd: {
name: 'chunk-Antd', // split Antd into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
config.optimization.runtimeChunk('single')
}
)
},
css: {
loaderOptions: {
less: {
lessOptions: {
modifyVars: modifyVars(),
javascriptEnabled: true
}
}
},
extract: isProd ? { ignoreOrder: true } : false
},
publicPath: process.env.VUE_APP_PUBLIC_PATH,
outputDir: 'dist',
assetsDir: 'static',
productionSourceMap: false,
lintOnSave: false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化