vue+webpack+element-ui项目打包优化速度与app.js、vendor.js打包后文件过大
---恢复内容开始---
从开通博客到现在也没写什么东西,最近几天一直在研究vue+webpack+element-ui项目打包速度优化,想把这几天的成果记录下来,可能对前端牛人来说我这技术比较菜,但还是希望给有需要的朋友提供一下方便。
一开始项目部署到线上后第一次访问首页的时间是7、8秒的样子,页面加载太慢了自己都接受不了何况用户。
主要是从一下几步来优化的:
1、vue路由的加载方式
import Home from '@/components/Index'
改为
const Index = resolve => require(['@/components/Index'], resolve)
上面那种普通的加载方式的缺点是把所有的路由打包在一个js文件里面页面多的话这个文件会非常大加载的很慢。
下面这种方式是按需加载在访问的时候只加载相关的路由,又被叫做(懒加载)
2、第三方依赖包的引入
main.js
import Vue from 'vue'
import router from './router'
import Element from 'element-ui'
import echarts from 'echarts' Vue.use(Element)
Vue.prototype.echarts = echarts new Vue({
el: '#app',
store,
router,
components: {App},
template: '<App/>'
})
这样引入依赖包会把所依赖的第三方包全都打包起来从而造成打包后的 app.js和vendor.js文件过大加载的很慢,把这种方式改为CDN引入。
首先把main.js种引入的第三方包注释掉,vue不用注释
import Vue from 'vue'
//import router from './router'
//import Element from 'element-ui'
//import echarts from 'echarts' //Vue.use(Element)
//Vue.prototype.echarts = echarts new Vue({
el: '#app',
store,
router,
components: {App},
template: '<App/>'
})
在webpack.base.conf.js中增加以下设置,这样就不会把依赖包打包到自己的项目里面了
module.exports = {
externals: {
'vue': 'Vue',
'element-ui': 'ElementUI',
'echarts': 'echarts',
}
}
然后在index.html中用CDN的方式引入
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.11.0/theme-chalk/index.css">
<title></title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.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.11.0/index.js"></script>
<script src="https://cdn.bootcss.com/echarts/4.2.1/echarts.min.js"></script>
</body>
</html>
这样在打包的时候就不会把这些依赖包打包到里面,vendor.js有之前的2M多变成了200多K。
3、打包生成压缩包,同时不生成.amp文件
先安装 npm install --save-dev compression-webpack-plugin
找到config/index.js 修改productionSourceMap: false,默认是true修改为false这样打包的时候就不会生成.map文件
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
/**
* Source Maps
*/
productionSourceMap: false,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// 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,
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
}
}
打包生成压缩包修改 productionGzip:true 默认是false 修改为true,修改完成后设置webpack.prod.conf.js, 这个设置在vue-lic 2.0以上版本中默认就已经设置好了,所以就不用
手动设置。
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
})
)
}
npm run build 有可能会报错
ValidationError: Compression Plugin Invalid Options
options should NOT have additional properties
这个错误是compression-webpack-plugin的版本的问题,我安装的版本是 @1.0.0 可以成功打包

打包生成压缩包部署到线上需要后台的配合设置 nginx 开启:gzip

设置完以上及步骤后现在的速度保持在1s左右,打包后的js文件也变得很小了。

以上是我对项目速度优化的全部步骤希望对有需要的小伙伴们有所帮助!!
---恢复内容结束---
从开通博客到现在也没写什么东西,最近几天一直在研究vue+webpack+element-ui项目打包速度优化,想把这几天的成果记录下来,可能对前端牛人来说我这技术比较菜,但还是希望给有需要的朋友提供一下方便。
一开始项目部署到线上后第一次访问首页的时间是7、8秒的样子,页面加载太慢了自己都接受不了何况用户。
主要是从一下几步来优化的:
1、vue路由的加载方式
import Home from '@/components/Index'
改为
const Index = resolve => require(['@/components/Index'], resolve)
上面那种普通的加载方式的缺点是把所有的路由打包在一个js文件里面页面多的话这个文件会非常大加载的很慢。
下面这种方式是按需加载在访问的时候只加载相关的路由,又被叫做(懒加载)
2、第三方依赖包的引入
main.js
import Vue from 'vue'
import router from './router'
import Element from 'element-ui'
import echarts from 'echarts' Vue.use(Element)
Vue.prototype.echarts = echarts new Vue({
el: '#app',
store,
router,
components: {App},
template: '<App/>'
})
这样引入依赖包会把所依赖的第三方包全都打包起来从而造成打包后的 app.js和vendor.js文件过大加载的很慢,把这种方式改为CDN引入。
首先把main.js种引入的第三方包注释掉,vue不用注释
import Vue from 'vue'
//import router from './router'
//import Element from 'element-ui'
//import echarts from 'echarts' //Vue.use(Element)
//Vue.prototype.echarts = echarts new Vue({
el: '#app',
store,
router,
components: {App},
template: '<App/>'
})
在index.html中用CDN的方式引入
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.11.0/theme-chalk/index.css">
<title></title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.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.11.0/index.js"></script>
<script src="https://cdn.bootcss.com/echarts/4.2.1/echarts.min.js"></script>
</body>
</html>
这样在打包的时候就不会把这些依赖包打包到里面,vendor.js有之前的2M多变成了200多K。
3、打包生成压缩包,同时不生成.amp文件
先安装 npm install --save-dev compression-webpack-plugin
找到config/index.js 修改productionSourceMap: false,默认是true修改为false这样打包的时候就不会生成.map文件
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
/**
* Source Maps
*/
productionSourceMap: false,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// 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,
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
}
}
打包生成压缩包修改 productionGzip:true 默认是false 修改为true,修改完成后设置webpack.prod.conf.js, 这个设置在vue-lic 2.0以上版本中默认就已经设置好了,所以就不用
手动设置。
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
})
)
}
npm run build 有可能会报错
ValidationError: Compression Plugin Invalid Options
options should NOT have additional properties
这个错误是compression-webpack-plugin的版本的问题,我安装的版本是 @1.0.0 可以成功打包

打包生成压缩包部署到线上需要后台的配合设置 nginx 开启:gzip

设置完以上及步骤后现在的速度保持在1s左右,打包后的js文件也变得很小了。

以上是我对项目速度优化的全部步骤希望对有需要的小伙伴们有所帮助!!
vue+webpack+element-ui项目打包优化速度与app.js、vendor.js打包后文件过大的更多相关文章
- Vue + Element UI项目初始化
1.安装相关组件 1.1安装Node 检查本地是否安装node node -v 如果没有安装,从Node官网下载 1.2安装npm npm -v 如果没有安装:使用该指令安装: npm install ...
- vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式
不多说上代码: <template> <div class="hello"> <h1>{{ msg }}</h1> <p> ...
- vue+element ui项目总结点(二)table合计栏目,按照起始年份--截止年份 插入数据并向后追加数据以最后一条年份+1
1.oninput 事件在用户输入时触发; <template> <div class="test_box"> <p>hell,你好</p ...
- webpack 打包优化的四种方法(多进程打包,多进程压缩,资源 CDN,动态 polyfill)
如今,webpack 毫无疑问是前端构建领域里最耀眼的一颗星,无论你前端走哪条路线,都需要有很强的webpack 知识.webpack 的基本用法这里就不展开讲了.主要探讨一下如何提高 webpack ...
- mac下搭建基于vue-cli 3.0的Element UI 项目
1.安装yarn管理工具(包含node.js); 2.安装全局vue-cli全家桶: yarn global add @vue/cli 3.创建.测试一个vue-cli项目: vue create a ...
- Vue框架Element UI教程-axios请求数据
Element UI手册:https://cloud.tencent.com/developer/doc/1270 中文文档:http://element-cn.eleme.io/#/zh-CN gi ...
- vue与element ui的el-checkbox的坑
一,场景 通过使用checkbox,实现如图的场景, 点击某个tag,实现选中和非选中状态. 二, 官网的例子 通过切换checked值为true或者false来实现,一个checkbox的状态切换 ...
- vue开源Element UI表单设计及代码生成器
在日常的开发工作中,表单开发是较为繁琐且重复的.本文介绍一个我自己写的,提高开发效率的小工具. 1 可视化设计器 设计器基于Element UI ,可通过点击或拖拽的方式设计基本表单, 设计器生成的代 ...
- 第五十三篇:Vue安装Element ui
好家伙,之前写的一篇过时了,用不了了,更新一波 (已新建一个vue项目) 1. 在项目目录下执行:npm i element-ui -S 2. 在main.js中写入 import ElementUI ...
随机推荐
- 代码审计之SQL注入及修复
在新手入门web安全的时候,sql注入往往是最先上手的一个漏洞,它也是危害相当大的一个漏洞,存在此漏洞的话,将有被脱裤的风险. 以下所有代码都是我自己写的,可能有不美观,代码错误等等问题,希望大家可以 ...
- .Net Reactor混淆导致匿名类处理出现的问题处理分析
.Net Reactor 是一款比较不错的混淆工具,比VS自带的那个好用很多,一直以来也陪伴着我们的成长,虽然没有完美的混淆工具,不过也算还是不错的,至少能在一定程度上对DLL进行一定的保护处理. 不 ...
- 设计模式-Builder和Factory模式区别
Builder和Factory模式区别 Builder模式结构: Factory模式一进一出,Builder模式是分步流水线作业.当你需要做一系列有序的工作或者按照一定的逻辑来完成创建一个对象时 Bu ...
- php-fpm启动,重启,退出
首先确保php-fpm正常安装,运行命令php-fpm -t输出查看: ##确定php-fpm配置正常 [root@iz2vcf47jzvf8dxrapolf7z php7.-Oct-::] NOTI ...
- python openpyxl内存不主动释放 ——关闭Excel工作簿后内存依旧(MemoryError)
在openpyxl对Excel读写操作过程中,发现内存没有马上释放,如果得多次读取大文件,内存爪机,后续代码就无法运行. 尝试:各种wb.save()或者with open等途径无法解决. 发现:因为 ...
- Android之各个版本之间的变化
(1)android5.0 Android5.0之后隐式打开服务需要指明包名 (2)android6.0 Android6.0之后涉及隐私的权限必须动态申请 (3)android8.0 android ...
- php数字函数
is_numeric() 检查变量是否包含一个合法数字 round() 取整数,四舍五入 round(数字, 小数位) ceil() 向上取整 floor() 向下取整 range() 生成范围 ...
- Java 爬虫遇上数据异步加载,试试这两种办法!
这是 Java 爬虫系列博文的第三篇,在上一篇 Java 爬虫遇到需要登录的网站,该怎么办? 中,我们简单的讲解了爬虫时遇到登录问题的解决办法,在这篇文章中我们一起来聊一聊爬虫时遇到数据异步加载的问题 ...
- selenium-webdriver中的显式等待与隐式等待
在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...
- aiohttp的安装
之前介绍的requests库是一个阻塞式HTTP请求库,当我们发出一个请求后,程序会一直等待服务器响应,知道得到响应后,程序才会进行下一步处理.其实,这个过程比较耗时.如果程序可以在这个等待过程中做一 ...