咱们以 vue-cli 工具为例,使用 vue-router 搭建SPA应用,UI框架选用 element-ui , ajax方案选用 axios, 并引入 vuex ,使用 vuex-router-sync 将 router
同步到 store
,服务器使用本地Nginx服务。javascript
vue-init webpack vue-spa-starter-kit
cd vue-spa-starter-kit npm install npm install vuex element-ui axios -S npm run dev
vue-cli会自动打开浏览器,能够看到效果。咱们在入口文件中引入vue-router
、element-ui
、axios
css
// src/main.js import 'babel-polyfill' import Vue from 'vue' import App from './App' import axios from 'axios' import store from './store' import router from './router' import {sync} from 'vuex-router-sync' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.config.productionTip = false Vue.use(ElementUI) Vue.prototype.$http = axios sync(store, router) /* eslint-disable no-new */ new Vue({ el: '#app', store, router, template: '<App/>', components: { App } })
接下来咱们不作任何修改,使用默认的配置进行打包,Nginx采用默认配置,部署到Nginx,启动Nginx服务 html
能够看出,在没有开发任何页面及功能的状况下,vendor.js
有788kb。若是咱们再依赖一些其余的库,好比 echarts
等,vendor.js
能到 1M 以上。vue
咱们要将 vue
、 vue-router
、 vuex
、element-ui
从 vendor.js
中分离出来,使用CDN资源引入。国内的CDN服务推荐使用 BootCDN。国外不是很好用。。。java
首先在模板文件index.html
中添加如下内容:webpack
...
<head> <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.0.7/theme-chalk/index.css"> </head> <body> <div id="app"></div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/element-ui/2.0.7/index.js"></script> <!-- built files will be auto injected --> </body>
修改 build/webpack.base.conf.js
。关于 externals
配置项请自行查阅相关资料。ios
module.exports = { ... externals: { 'vue': 'Vue', 'vuex': 'Vuex', 'vue-router': 'VueRouter', 'element-ui': 'ELEMENT' } ... }
修改 src/router/index.js
nginx
// import Vue from 'vue' import VueRouter from 'vue-router' // 注释掉 // Vue.use(VueRouter) ...
修改 src/store/index.js
git
...
// 注释掉 // Vue.use(Vuex) ...
修改 src/main.js
github
import 'babel-polyfill' import Vue from 'vue' import App from './App' import axios from 'axios' import store from './store' import router from './router' import {sync} from 'vuex-router-sync' import ELEMENT from 'element-ui' // import 'element-ui/lib/theme-chalk/index.css' Vue.config.productionTip = false Vue.use(ELEMENT) Vue.prototype.$http = axios sync(store, router) /* eslint-disable no-new */ new Vue({ el: '#app', store, router, template: '<App/>', components: { App } })
注意!这里
element-ui
变量名要使用ELEMENT
,由于element-ui
的 umd 模块名是ELEMENT
再次打包,部署到Nginx服务,能够看到:
vendor.js
减小到了 112kb,提高85.5%!
再看CDN资源,5个请求共216kb,耗时619ms!
对 vendor.js
咱们优化完了,接下来咱们优化服务器上的资源。先看看没有开启 gzip 的效果有 13.5kb
Nginx开启gzip,修改nginx配置文件 nginx.conf
:
...
http {
...
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.1;
gzip_comp_level 2; # 压缩级别
# 要压缩的mine类型
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss image/jpeg image/gif image/png image/svg+xml;
gzip_vary off;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\."; # IE6不支持gzip
...
}
关于 nginx gzip,请自行查阅相关资料
重启nginx服务,能够看到服务器上的资源通过gzip压缩以后有 9kb,压缩比 13.3%。
上文中咱们修改的是 build/webpack.base.conf.js
,这样一来,本地开发的时候咱们就不能使用 vue-devtools
Chrome调试工具了,为了方便调试,咱们须要将development和production 配置分开。
build/webpack.base.conf.js
中 externals
配置项挪到 build/webpack.prod.conf.js
中:// build/webpack.prod.conf.js module.exports = { ... externals: { 'vue': 'Vue', 'vuex': 'Vuex', 'vue-router': 'VueRouter', 'element-ui': 'ELEMENT' } ... }
index.html
模板文件,并命名为 index.prod.html
; 将原来的 index.html
重命名为 index.dev.html
,并删除 CDN 资源:<!-- index.dev.html --> <head> ... <!-- <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.0.7/theme-chalk/index.css"> --> </head> <body> <div id="app"></div> <!-- <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/element-ui/2.0.7/index.js"></script> --> <!-- built files will be auto injected --> </body> <!-- index.prod.html --> <head> <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.0.7/theme-chalk/index.css"> </head> <body> <div id="app"></div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/element-ui/2.0.7/index.js"></script> <!-- built files will be auto injected --> </body>
build/webpack.dev.conf.js
:plugins: [
...
new HtmlWebpackPlugin({
filename: 'index.html', template: 'index.dev.html', inject: true }) ]
build/webpack.prod.conf.js
:plugins: [
...
new HtmlWebpackPlugin({
filename: 'index.html', template: 'index.prod.html', inject: true, ... }) ]
vue-router
、vuex
、element-ui
的引用:// src/main.js ... if (process.env.NODE_ENV === 'development') { require('element-ui/lib/theme-chalk/index.css') } ... // src/router/index.js import Vue from 'vue' import VueRouter from 'vue-router' if (process.env.NODE_ENV === 'development') { Vue.use(VueRouter) } ... // src/store/index.js import Vue from 'vue' import Vuex from 'vuex' if (process.env.NODE_ENV === 'development') { Vue.use(Vuex) } ...
这样咱们就能够愉快的在开发环境使用 vue-devtools
Chrome调试工具了!