require-ensurerequire-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-module-require

webpack ------require,ensure的更多相关文章

  1. vue项目优化之按需加载组件-使用webpack require.ensure

    require-ensure和require-amd的区别: require-amd 说明: 同AMD规范的require函数,使用时传递一个模块数组和回调函数,模块都被下载下来且都被执行后才执行回调 ...

  2. vue按需加载组件-webpack require.ensure

    使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体, 打包位置是 dist/static/js/app.[contenthash].j ...

  3. webpack: require.ensure与require AMD的区别

    http://blog.csdn.net/zhbhun/article/details/46826129

  4. webpack require.ensure 按需加载

    使用 vue-cli构建的项目,在 默认情况下 ,会将所有的js代码打包为一个整体比如index.js.当使用存在多个路由代码的时候,index.js可能会超大,影响加载速度. 这个每个路由页面除了i ...

  5. webpack中利用require.ensure()实现按需加载

    webpack中的require.ensure()可以实现按需加载资源包括js,css等,它会给里面require的文件单独打包,不和主文件打包在一起,webpack会自动配置名字,如0.js,1.j ...

  6. vue项目实现按需加载的3种方式:vue异步组件技术、es提案的import()、webpack提供的require.ensure()

    1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件. 举例如下: { path: '/promisedemo ...

  7. route按需加载的3种方式:vue异步组件、es提案的import()、webpack的require.ensure()

    1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件.举例如下: { path: '/promisedemo' ...

  8. 在vue中使用import()来代替require.ensure()实现代码打包分离

    最近看到一种router的写法 import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const login = ...

  9. require.ensure的用法;异步加载-代码分割;

    webpack异步加载的原理 webpack ensure相信大家都听过.有人称它为异步加载,也有人说做代码切割,那这 个家伙到底是用来干嘛的?其实说白了,它就是把js模块给独立导出一个.js文件的, ...

随机推荐

  1. javascrit字符串截取

    昨天遇见一个问题就是一个地址后面加参数第一次是需要添加参数,以后每次点击按钮的时候是替换如果不进行处理的话如果页面不刷新,地址会不断的添加越来越长,所以

  2. information_schema.optimizer_trace学习

    information_schema.optimizer_trace 用于追踪优化器的优化过程:通常来说这张表中是没有数据的,要想开户追踪要把 @@session.optimizer_trace='e ...

  3. LINUX SSH客户端的中文乱码问题

       原因在于文件/etc/sysconfig/i18n 这个文件是系统的区域语言设置, i18n是 国际化internationalization的缩写 i和n之间正好18个字母 解释: LANG= ...

  4. Mysql基本类型(五种年日期时间类型)——mysql之二

    转自:<MySQL技术内幕:时间和日期数据类型> http://tech.it168.com/a2012/0904/1393/000001393605_all.shtml

  5. TD数量不确定时如何让其宽度平均分布

    D数量不确定时如何让其宽度平均分布?答案很简单,我们只要在table里面加上一下代码就可以实现. table { width: 100%; table-layout: fixed; }

  6. SQL Server索引 - 索引(物化)视图 <第九篇>

    一.索引视图基本概念 索引视图实际上是一种将一组唯一值“物化”为群集索引形式的视图,所为物化就是几乎和表一样,其数据也是会存储一份的(会占用硬盘空间,但是查询速度快,例如可以将count(),sum( ...

  7. Jquery之家5个顶级Material Design框架

    谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...

  8. LeeCode-Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...

  9. zedboard--Opencv的移植(十)

    今天终于把Opencv的移植搞定了,花了一天的时间,主要是参考了书上和rainysky的博客.下载的2.3.1的版本 第一步肯定是下载opencv的源码包了,在opencv的官网上下载http://s ...

  10. Swift中的设计模式

    设计模式(Design Pattern)是 对软件设计中普遍存在的各种问题,所提出的解决方案.这个术语是由埃里希·伽玛等人(Erich Gamma,Richard Helm,Ralph Johnson ...