最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解webpack require.ensure與require AMD的區(qū)別

 更新時間:2017年12月13日 12:00:35   作者:zhbhun  
本篇文章主要介紹了詳解webpack require.ensure與require AMD的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

簡介

require-ensurerequire-amd的區(qū)別:

require-amd

說明: 同AMD規(guī)范的require函數(shù),使用時傳遞一個模塊數(shù)組和回調(diào)函數(shù),模塊都被下載下來且都被執(zhí)行后才執(zhí)行回調(diào)函數(shù)

語法: require(dependencies: String[], [callback: function(...)])

參數(shù)

  1. dependencies: 模塊依賴數(shù)組
  2. callback: 回調(diào)函數(shù)

require-ensure

說明: require.ensure在需要的時候才下載依賴的模塊,當參數(shù)指定的模塊都下載下來了(下載下來的模塊還沒執(zhí)行),便執(zhí)行

參數(shù)指定的回調(diào)函數(shù)。require.ensure會創(chuàng)建一個chunk,且可以指定該chunk的名稱,如果這個chunk名已經(jīng)存在了,則將本次依賴的模塊合并到已經(jīng)存在的chunk中,最后這個chunk在webpack構建的時候會單獨生成一個文件。

語法: require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])

  1. dependencies: 依賴的模塊數(shù)組
  2. callback: 回調(diào)函數(shù),該函數(shù)調(diào)用時會傳一個require參數(shù)
  3. chunkName: 模塊名,用于構建時生成文件時命名使用

注意點:requi.ensure的模塊只會被下載下來,不會被執(zhí)行,只有在回調(diào)函數(shù)使用require(模塊名)后,這個模塊才會被執(zhí)行。

示例

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");
});

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');

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

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) {
  console.log("aaa");
  require("./module1");
  console.log("bbb");
}, 'common');

require.ensure(["./module2"], function(require) {
  console.log("ccc");
  require("./module2");
  console.log("ddd");
}, 'common');

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

宜宾市| 师宗县| 广水市| 河间市| 乐安县| 揭阳市| 武平县| 赣榆县| 斗六市| 怀集县| 响水县| 格尔木市| 玉屏| 综艺| 泸州市| 淮阳县| 成安县| 武隆县| 西林县| 河东区| 铜梁县| 顺平县| 广东省| 科技| 禄丰县| 宝鸡市| 德庆县| 丰都县| 甘谷县| 兴化市| 汝阳县| 壤塘县| 儋州市| 开阳县| 巫溪县| 博罗县| 监利县| 开封市| 连城县| 嘉定区| 三河市|