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"
}
};- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
example.amd.js
require(["./module1"], function(module1) {
console.log("aaa");
var module2 = require("./module2");
console.log("bbb");
});- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
module1.js
console.log("module1");
module.exports = 1;- 1
- 2
- 1
- 2
module2.js
console.log("module2");
module.exports = 2;- 1
- 2
- 1
- 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
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
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"
}
};- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
example.ensure.js
require.ensure(["./module1"], function(require) {
console.log("aaa");
var module2 = require("./module2");
console.log("bbb");
require("./module1");
}, 'test');- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
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
- 2
- 3
- 4
- 1
- 2
- 3
- 4
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"
}
};- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
example.ensur.chunk.js
require.ensure(["./module1"], function(require) {
console.log("aaa");
require("./module1");
console.log("bbb");
}, 'common'); require.ensure(["./module2"], function(require) {
console.log("ccc");
require("./module2");
console.log("ddd");
}, 'common');- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
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
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
源代码
webpack ------require,ensure的更多相关文章
- vue项目优化之按需加载组件-使用webpack require.ensure
require-ensure和require-amd的区别: require-amd 说明: 同AMD规范的require函数,使用时传递一个模块数组和回调函数,模块都被下载下来且都被执行后才执行回调 ...
- vue按需加载组件-webpack require.ensure
使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体, 打包位置是 dist/static/js/app.[contenthash].j ...
- webpack: require.ensure与require AMD的区别
http://blog.csdn.net/zhbhun/article/details/46826129
- webpack require.ensure 按需加载
使用 vue-cli构建的项目,在 默认情况下 ,会将所有的js代码打包为一个整体比如index.js.当使用存在多个路由代码的时候,index.js可能会超大,影响加载速度. 这个每个路由页面除了i ...
- webpack中利用require.ensure()实现按需加载
webpack中的require.ensure()可以实现按需加载资源包括js,css等,它会给里面require的文件单独打包,不和主文件打包在一起,webpack会自动配置名字,如0.js,1.j ...
- vue项目实现按需加载的3种方式:vue异步组件技术、es提案的import()、webpack提供的require.ensure()
1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件. 举例如下: { path: '/promisedemo ...
- route按需加载的3种方式:vue异步组件、es提案的import()、webpack的require.ensure()
1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件.举例如下: { path: '/promisedemo' ...
- 在vue中使用import()来代替require.ensure()实现代码打包分离
最近看到一种router的写法 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const login = ...
- require.ensure的用法;异步加载-代码分割;
webpack异步加载的原理 webpack ensure相信大家都听过.有人称它为异步加载,也有人说做代码切割,那这 个家伙到底是用来干嘛的?其实说白了,它就是把js模块给独立导出一个.js文件的, ...
随机推荐
- Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 凸包(hd1392)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- order by 中 使用decode
表内容如下: 实现以name字段中的ABCDE的顺序排序,以及按照money字段从小到大排序. select name,money from t2 ,,,,) , money
- DES、AES、TEA加密算法的比较
1. DES算法介绍: DES算法具有对称性, 既可以用于加密又可以用于解密.对称性带来的一个很大的好处在于硬件实现, DES 的加密和解密可以用完全相同的硬件来实现.DES 算法的明文分组是 ...
- ***.M51文件详细注释
;说明:这是1950编译后生成的Keil_1910.M51文件,以此为例来讲解M51文件 // :: PAGE BL51 BANKED LINKER/LOCATER V6., INVOKED BY: ...
- Arcgis api For silverlight 加载QQ地图
原文 http://www.cnblogs.com/thinkaspx/archive/2012/11/07/2759079.html //本篇博客仅在技术上探讨可行性 //如果要使用Q 地图,请 ...
- First Missing Positive 解答
Question Given an unsorted integer array, find the first missing positive integer. For example,Given ...
- LeeCode-Insertion Sort List
Sort a linked list using insertion sort. /** * Definition for singly-linked list. * struct ListNode ...
- Js数组的操作push,pop,shift,unshift等方法详细介绍
js中针对数组操作的方法还是比较多的,今天突然想到来总结一下,也算是温故而知新吧.不过不会针对每个方法进行讲解,我只是选择其中的一些来讲. 首 先来讲一下push和pop方法,这两个方法只会对数组从尾 ...
- actionInvocation
1.actionInvocation是什么 ActionInvocation就是Action的调用者.ActionInvocation在Action的执行过程中,负责Interceptor.Actio ...