Skip to content

Commit

Permalink
optimize production environment packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
guoranred committed Sep 22, 2020
1 parent d23cfef commit dcf6593
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
10 changes: 9 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)

const plugins = []
if (IS_PROD) {
plugins.push('transform-remove-console')
}

module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
],
plugins
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"vue-template-compiler": "^2.6.11",
"vuepress": "^1.5.2",
"webpack-theme-color-replacer": "^1.3.12",
"whatwg-fetch": "^3.0.0"
"whatwg-fetch": "^3.0.0",
"compression-webpack-plugin": "^2.0.0",
"babel-plugin-transform-remove-console": "^6.9.4"
},
"eslintConfig": {
"root": true,
Expand Down
35 changes: 27 additions & 8 deletions vue.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
let 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')

const productionGzipExtensions = ['js', 'css']
const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
Expand All @@ -14,7 +17,8 @@ const assetsCDN = {
axios: 'axios',
nprogress: 'NProgress',
clipboard: 'ClipboardJS',
'@antv/data-set': 'DataSet'
'@antv/data-set': 'DataSet',
'js-cookie': 'Cookies'
},
css: [
],
Expand All @@ -25,7 +29,8 @@ const assetsCDN = {
'//cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js',
'//cdn.jsdelivr.net/npm/[email protected]/nprogress.min.js',
'//cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js',
'//cdn.jsdelivr.net/npm/@antv/[email protected]/build/data-set.min.js'
'//cdn.jsdelivr.net/npm/@antv/[email protected]/build/data-set.min.js',
'//cdn.jsdelivr.net/npm/[email protected]/src/js.cookie.min.js'
]
}

Expand All @@ -49,6 +54,9 @@ module.exports = {
},
configureWebpack: config => {
config.entry.app = ["babel-polyfill", "whatwg-fetch", "./src/main.js"];
config.performance = {
hints: false
}
config.plugins.push(
new ThemeColorReplacer({
fileName: 'css/theme-colors-[contenthash:8].css',
Expand All @@ -57,7 +65,22 @@ module.exports = {
resolveCss
})
)
config.externals = isProd ? assetsCDN.externals : {}
// 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 项,因为此项优化与主题色替换功能冲突
Expand All @@ -68,11 +91,7 @@ module.exports = {
return args
})
}
config.plugin('html')
.tap(args => {
args[0].title = 'Vue Antd Admin'
return args
})
// 生产环境下使用CDN
if (isProd) {
config.plugin('html')
.tap(args => {
Expand Down

0 comments on commit dcf6593

Please sign in to comment.