Skip to content

Commit a02119c

Browse files
committedJun 3, 2020
perf: ⚡Vue、vue-router、Vuex、axios、element-ui采用CDN的方式引入,添加gzip打包
1 parent 0c3c274 commit a02119c

File tree

4 files changed

+338
-0
lines changed

4 files changed

+338
-0
lines changed
 

‎package-lock.json

+276
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@vue/test-utils": "1.0.0-beta.29",
4545
"babel-eslint": "^10.0.1",
4646
"commitizen": "^4.1.2",
47+
"compression-webpack-plugin": "^4.0.0",
4748
"eslint": "^5.16.0",
4849
"eslint-plugin-vue": "^5.0.0",
4950
"http-proxy-middleware": "^0.20.0",

‎public/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
88
<title>vue-blog</title>
99
<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
10+
<% if (process.env.NODE_ENV === 'production') { %>
11+
<% for(var css of htmlWebpackPlugin.options.cdn.css) { %>
12+
<link rel="stylesheet" href="<%=css%>" as="style">
13+
<% } %>
14+
<% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
15+
<script src="<%=js%>"></script>
16+
<% } %>
17+
<% } %>
1018
</head>
1119
<body>
1220
<noscript>

‎vue.config.js

+53
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const CompressionPlugin = require('compression-webpack-plugin')
2+
const productionGzipExtensions = ['js', 'css']
13
module.exports = {
24
publicPath: '/vue-blog/',
35
outputDir: 'docs',
@@ -18,5 +20,56 @@ module.exports = {
1820
changeOrigin: true
1921
}
2022
}
23+
},
24+
chainWebpack: config => {
25+
if (process.env.NODE_ENV === 'production') {
26+
var externals = {
27+
vue: 'Vue',
28+
axios: 'axios',
29+
'element-ui': 'ELEMENT',
30+
'vue-router': 'VueRouter',
31+
vuex: 'Vuex'
32+
}
33+
config.externals(externals)
34+
const cdn = {
35+
css: [
36+
// element-ui css
37+
'https://cdn.bootcdn.net/ajax/libs/element-ui/2.4.5/theme-chalk/index.css'
38+
],
39+
js: [
40+
// vue
41+
'https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js',
42+
// vue-router
43+
'https://cdn.bootcdn.net/ajax/libs/vue-router/3.0.6/vue-router.min.js',
44+
// vuex
45+
'https://cdn.bootcdn.net/ajax/libs/vuex/3.1.2/vuex.min.js',
46+
// axios
47+
'https://cdn.bootcdn.net/ajax/libs/axios/0.18.0/axios.min.js',
48+
// element-ui js
49+
'https://cdn.bootcdn.net/ajax/libs/element-ui/2.4.5/index.js'
50+
]
51+
}
52+
config.plugin('html')
53+
.tap(args => {
54+
args[0].cdn = cdn
55+
return args
56+
})
57+
}
58+
},
59+
configureWebpack: config => {
60+
if (process.env.NODE_ENV === 'production') {
61+
// 生产环境
62+
config.plugins.push(
63+
new CompressionPlugin({
64+
filename: '[path].gz[query]',
65+
algorithm: 'gzip',
66+
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
67+
threshold: 10240,
68+
minRatio: 0.8
69+
})
70+
)
71+
} else {
72+
// 开发环境
73+
}
2174
}
2275
}

0 commit comments

Comments
 (0)
Failed to load comments.