vue项目优化之按需加载组件-使用webpack require.ensure
require-ensure和require-amd的区别:
- require-amd
- 说明: 同AMD规范的require函数,使用时传递一个模块数组和回调函数,模块都被下载下来且都被执行后才执行回调函数
- 语法:
require(dependencies: String[], [callback: function(...)])
- 参数
- dependencies: 模块依赖数组
- callback: 回调函数
- require-ensure
- 说明:
require.ensure在需要的时候才下载依赖的模块,当参数指定的模块都下载下来了(下载下来的模块还没执行),便执行参数指定的回调函数。require.ensure会创建一个chunk,且可以指定该chunk的名称,如果这个chunk名已经存在了,则将本次依赖的模块合并到已经存在的chunk中,最后这个chunk在webpack构建的时候会单独生成一个文件。 - 语法:
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
- dependencies: 依赖的模块数组
- callback: 回调函数,该函数调用时会传一个require参数
- chunkName: 模块名,用于构建时生成文件时命名使用
- 注意点:requi.ensure的模块只会被下载下来,不会被执行,只有在回调函数使用require(模块名)后,这个模块才会被执行。
- 说明:
示例
require-amd
源代码
webpack.config.amd.js
var path = require("path");
module.exports = {
entry: "./example.amd.js",
output: {
path: path.join(__dirname, "amd"),
filename: "[name].bundle.js",
chunkFilename: "[id].chunk.js"
}
};example.amd.js
require(["./module1"], function(module1) {
console.log("aaa");
var module2 = require("./module2");
console.log("bbb");
});- 1
module1.js
console.log("module1");
module.exports = 1;module2.js
console.log("module2");
module.exports = 2;
构建结果
命令行中运行webpack --config webpack.config.amd.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
运行结果
浏览器中运行amd/index.html,控制台输出:
module1
aaa
module2
bbb
require-ensure
源代码
webpack.config.ensure.js
var path = require("path");
module.exports = {
entry: "./example.ensure.js",
output: {
path: path.join(__dirname, "ensure"),
filename: "[name].bundle.js",
chunkFilename: "[name].chunk.js"
}
};example.ensure.js
require.ensure(["./module1"], function(require) {
console.log("aaa");
var module2 = require("./module2");
console.log("bbb");
require("./module1");
}, 'test');- 1
- 2
module1.js
同上- module2.js
同上
构建结果
命令行中运行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
运行结果
浏览器中运行ensure/index.html,控制台输出:
aaa
module2
bbb
module1
- 1
require-ensure-chunk
源代码
webpack.config.ensure.chunk.js
var path = require("path");
module.exports = {
entry: "./example.ensur.chunk.js",
output: {
path: path.join(__dirname, "ensure-chunk"),
filename: "[name].bundle.js",
chunkFilename: "[name].chunk.js"
}
};example.ensur.chunk.js
require.ensure(["./module1"], function(require) {
8
console.log("aaa");
require("./module1");
console.log("bbb");
}, 'common'); require.ensure(["./module2"], function(require) {
console.log("ccc");
require("./module2");
console.log("ddd");
}, 'common')- 9
- 10
module1.js
同上- module2.js
同上
构建结果
命令行中运行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
运行结果
浏览器中运行ensure/index.html,控制台输出:
aaa
module1
bbb
ccc
1module2
ddd
使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体,
打包位置是 dist/static/js/app.[contenthash].js
类似下面的路由代码
router/index.js 路由相关信息,该路由文件引入了多个 .vue组件
import Hello from '@/components/Hello'
import Province from '@/components/Province'
import Segment from '@/components/Segment'
import User from '@/components/User'
import Loading from '@/components/Loading'
执行 npm run build 会打包为一个整体 app.[contenthash].js ,这个文件是非常大,可能几兆或者几十兆,加载会很慢
所以我们需要分模块打包,把我们想要组合在一起的组件打包到一个 chunk块中去
分模块打包需要下面这样使用 webpack的 require.ensure,并且在最后加入一个 chunk名,
相同 chunk名字的模块将会打包到一起
router/index.js 修改为懒加载组件
const Province = r => require.ensure([], () => r(require('@/components/Province.vue')), 'chunkname1')
const Segment = r => require.ensure([], () => r(require('@/components/Segment.vue')), 'chunkname1')
const Loading = r => require.ensure([], () => r(require('@/components/Loading.vue')), 'chunkname3')
const User = r => require.ensure([], () => r(require('@/components/User.vue')), 'chunkname3')
根据 chunkame的不同, 上面的四个组件, 将会被分成3个块打包,最终打包之后与组件相关的js文件会分为3个 (除了app.js,manifest.js, vendor.js)
分模块打包之后在 dist目录下是这样的, 这样就把一个大的 js文件分为一个个小的js文件了,按需去下载,其他的使用方法和import的效果一样
参考vue-router官方文档: https://router.vuejs.org/zh-cn/advanced/lazy-loading.html
vue项目优化之按需加载组件-使用webpack require.ensure的更多相关文章
- vue项目实现路由按需加载的3种方式
vue异步组件技术 ==== 异步加载vue-router配置路由 , 使用vue的异步组件技术 , 可以实现按需加载 .但是,这种情况下一个组件生成一个js文件 /* vue异步组件技术 */ { ...
- vue项目优化之路由懒加载
const login = () =>import('@/views/login'); export default new Router({ routes:[ { path:'/login', ...
- 前端性能优化之按需加载(React-router+webpack)
一.什么是按需加载 和异步加载script的目的一样(异步加载script的方法),按需加载/代码切割也可以解决首屏加载的速度. 什么时候需要按需加载 如果是大文件,使用按需加载就十分合适.比如一个近 ...
- vue按需加载组件,异步组件
说实话,我一开始也不知道什么叫按需加载组件,组件还可以按需加载???后来知道了 学不完啊...没关系,看我的 按需加载组件,或者异步组件,主要是应用了component的 is 属性 template ...
- 仿ElementUI构建自己的Vue组件库用babel-plugin-component按需加载组件及自定义SASS主题
最近使用ElementUI做项目的时候用Babel的插件babel-plugin-component做按需加载,使得组件打包的JS和CSS包体积大大缩小,加载速度也大大提升,所有想模仿做一个组件库也来 ...
- vue按需加载组件-webpack require.ensure
使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体, 打包位置是 dist/static/js/app.[contenthash].j ...
- 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)
一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...
- iview简单使用+按需加载组件的方法(全局和局部)
1,简单使用 vue项目中使用iview非常简单: 首先安装依赖: $ npm install iview --save 会安装最新版本的依赖,安装完成后package.json会出现如下图配置 表示 ...
- 【react学习二】create-react-app 接入antd 并按需加载组件
1.安装 cnpm i babel-plugin-import --save-dev 2.使用 在根目录下的package.json下的bable中添加相应代码 "babel": ...
随机推荐
- python 标准库 -- shutil
shutil shutil.move(src,dst) shutil.move('/tmp/20170223/new','/tmp/20170223/test') # 移动文件, 重命名等 shuti ...
- Linux下Shadow socks的安装和配置
实在受不了在Windows下编程,所以自己就安装了一个Ubutun,公司用的FQ软件shadowsocks在Windows上用起来很简单很爽,但是在Ubutun上的安装和配置就没那么简单了,写下这篇文 ...
- linux服务器查看redis版本:
linux服务器查看redis版本:redis-server-v
- Redis 基础数据结构与对象
Redis用到的底层数据结构有:简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等,Redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这些数据结构创建了一个对象系统,这个系统包 ...
- winform利用委托delegate进行窗体间通信,相同标题已经存在??
前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...
- 在suse上折腾iptables
需求背景:有台服务器希望屏蔽掉某IP对它的SSH连接. 临时客串下DevOps,下面的做法可能在专业运维的同学里不太专业,还请指教. 该服务器的操作系统是SuSE Linux,服务器上是安装了ipta ...
- python webserver, based on SimpleHTTPServer
#-*- coding:utf-8 -*- #author: lichmama #email: nextgodhand@163.com #filename: httpd.py import io im ...
- Watson API - Personality Insight For Certificate
Personality Insight For Certificate 1.Describe the intended use of the Personality Insights service ...
- Storm集群安装部署步骤
本文以Twitter Storm官方Wiki为基础,详细描述如何快速搭建一个Storm集群,其中,项目实践中遇到的问题及经验总结,在相应章节以"注意事项"的形式给出. 1. Sto ...
- 用java调用oracle存储过程总结
以前一直没有动存储过程是用来干嘛的,后来请教朋友才换为自己的理解方式,用自己通俗的语言来说,就是把sql语句换为一个过程,也可以说是一个方法,每次直接给参数调用就好,使用存储过程查询速度快,系统只编译 ...