單頁應用會隨着項目越大,致使首屏加載速度很慢!!!如下給出在下知道的幾種優化方案css
... <body> <div id="app"> </div> <!-- built files will be auto injected --> <script src="https://cdn.bootcss.com/vue/2.5.2/vue.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/vuex/3.0.1/vuex.min.js"></script> <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script> </body> ...
module.exports = { context: path.resolve(__dirname, '../'), entry: { app: './src/main.js' }, externals:{ 'vue': 'Vue', 'vue-router': 'VueRouter', 'vuex':'Vuex', 'vue-resource': 'VueResource' }, ... }
// import Vue from 'vue' // import VueResource from 'vue-resource' // Vue.use(VueResource)
require.ensure方式html
const workCircle = r => require.ensure([], () => r(require('@/module/work-circle/Index')), 'workCircle') const workCircleList = r => require.ensure([], () => r(require('@/module/work-circle/page/List')), 'workCircleList')
import方式前端
const workCircle = () => import('@/module/work-circle/Index')
注意這裏的js文件,須要將結果拋出,而後在須要用到該js的組件中import引入vue
按需引用請查看iview官方文檔iviewnode
配置nginx,能夠參考Nginx開啓Gzip壓縮大幅提升頁面加載速度webpack
這裏須要配合Nginx服務器,Nginx開啓gzipnginx
webpack4.x如下使用compression-webpack-plugin插件,插件版本應使用1.x
webpack4.x版本以上可使用compression-webpack-plugin 2.x
module.exports = { build: { ... // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: true, // 就是這裏開啓gzip,vue-cli搭建項目,這裏默認爲false productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report } }
使用vue-cli構建項目時,默認會有這段代碼es6
if (config.build.productionGzip) { const CompressionWebpackPlugin = require('compression-webpack-plugin') webpackConfig.plugins.push( new CompressionWebpackPlugin({ asset: '[path].gz[query]', algorithm: 'gzip', test: new RegExp( '\\.(' + config.build.productionGzipExtensions.join('|') + ')$' ), threshold: 10240, minRatio: 0.8 }) ) }
兩個插件都不支持es6壓縮,因此使用此插件前須要用工具(如babel-loader)轉換es6代碼web
問題描述:項目中使用iview時,致使使用UglifyJsPlugin壓縮報錯
由於iview某插件中包含es6語法。然而兩個插件都不支持es6壓縮
解決方法以下:vue-router
module.exports = { entry: { app: './src/main.js' }, output: { path: config.build.assetsRoot, filename: '[name].js', publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath }, ... module: { loaders: [ { test: /iview.src.*?js$/, loader: 'babel' }, { test: /\.js$/, loader: 'babel', exclude: /node_modules/ } ], rules: [ ... { test: /\.js$/, loader: 'babel-loader', // resolve('/node_modules/iview/src'),resolve('/node_modules/iview/packages')解決iview打包時UglifyJs報錯 include: [resolve('src'), resolve('test'), resolve('/node_modules/iview/src'),resolve('/node_modules/iview/packages')] } ... ] } }
... const UglifyJsPlugin = require('uglifyjs-webpack-plugin') ... new UglifyJsPlugin({ // 使用外部引入的新版本的js壓縮工具 parallel: true, uglifyOptions: { ie8: false, // 啓用IE8支持 ecma: 6, // 支持的ECMAScript的版本(5,6,7或8)。影響parse,compress&& output選項 warnings: false, // 顯示警告 mangle: true, // debug false output: { comments: false, beautify: false, // debug true }, compress: { // 在UglifyJs刪除沒有用到的代碼時不輸出警告 warnings: false, // 刪除全部的 `console` 語句 // 還能夠兼容ie瀏覽器 drop_console: true, // 內嵌定義了可是隻用到一次的變量 collapse_vars: true, // 提取出出現屢次可是沒有定義成變量去引用的靜態值 reduce_vars: true, } } }), // new webpack.optimize.UglifyJsPlugin({ // compress: { // warnings: false // }, // sourceMap: true // }),
此方法有待實踐,留待下次分享 ==