一. 使用 CDN 加速
index.html
页面引入 cdn script
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>AI Finance</title> </head> <body> <noscript> <strong>We're sorry but yarn-frotend-cli3 doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> <script src="https://cdn.bootcss.com/vue/2.5.21/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.2/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script> </body> </html>
项目根目录下的 vue.config.js 添加 externals
配置,把引入的 cdn 库这这里配置后, yarn builid
就不会把这些库打包了
module.exports = { configureWebpack: (config) => { config.externals = { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter', 'axios': 'axios' } ...
二. 启用 Gzip 压缩
Vue 项目默认不启动 Gzip 压缩, 需要安装 compression-webpack-plugin 包, 配置 config 文件.
yarn add compression-webpack-plugin
vue.config.js 配置文件中添加 configureWebpack 设置, 如下
const CompressionWebpackPlugin = require('compression-webpack-plugin') const productionGzipExtensions = ['js', 'css'] configureWebpack: { plugins: [ new CompressionWebpackPlugin({ filename: '[path].gz[query]', algorithm: 'gzip', test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'), threshold: 10240, minRatio: 0.8 }) ] }
注意 filename: '[path].gz[query]',
这一行, 这里的 compression-webpack-plugin 版本为 2.0 , 如果 compression-webpack-plugin 版本为 1.0 ,需要把 filename 参数换成 asset
Node
服务添加 gzip
支持
npm i express compression
let express = require('express') let app = express() let compression = require('compression') app.use(compression())
Nginx
服务添加 gzip
支持
http { gzip on; gzip_static on; gzip_min_length 1024; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; gzip_vary off; gzip_disable "MSIE [1-6]\."; }
Nginx
服务添加 gzip
支持 222
gzip_vary on
http { gzip on; gzip_static on; gzip_min_length 1024; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; gzip_vary on; gzip_disable "MSIE [1-6]\."; }
使用 UglifyJsPlugin 压缩代码
yarn add uglifyjs-webpack-plugin
vue.config.js 中添加配置
const UglifyJsPlugin = require('uglifyjs-webpack-plugin') module.exports = { configureWebpack: (config) => { config.plugins.push(new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false, drop_debugger: true, drop_console: true } }, sourceMap: false, parallel: true })) } }