单页应用会随着项目越大,致使首屏加载速度很慢!!!如下给出在下知道的几种优化方案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 // }),
此方法有待实践,留待下次分享 ==