標籤:nod default 分享 內容 ejs isarray err 技術 detail
首先,我們先來簡單說一下,require.js的原理:
1、載入模組?
2、通過模組名解析出模組資訊,以及計算出URL?
3、通過建立SCRIPT的形式把模組載入到頁面中。?
4、判斷被載入的指令碼,如果發現它有依賴就去載入相依模組。如果不依賴其它模組,就直接執行factory方法
?5、等所有指令碼都被載入完畢就執行載入完成之後的回呼函數。
從今天起,我們跟著我們簡單的例子,通過跟蹤代碼,來瞭解require.js的源碼。
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Document</title> 7 <!-- 引入require.js --> 8 <script data-main="./test.js" src="./require.js"></script> 9 </script>10 <script>11 require([‘a‘, ‘b‘], function(a, b) {12 a.printA();13 b.printB();14 });15 </script>16 </head>17 18 <body>19 </body>20 21 </html>
執行網頁時,如果需要使用require.js作為模組載入器時,我們首先需要引入require.js。
1 <!-- 引入require.js -->2 <script data-main="./test.js" src="./require.js"></script>
1 // a.js 2 define(function() { 3 return { 4 printA: console.log("I am A") 5 } 6 }); 7 8 // b.js 9 define(function() {10 return {11 printB: console.log("I am B")12 }13 });
開啟require.js檔案我們可以發現,檔案中首先定義了三個全域變數,之後便是一個自執行函數。因此在載入require.js檔案時,自動執行自執行函數中的內容。
1 var requirejs, require, define;2 (function(global, setTimeout) {3 // 此處省略...4 }(this, (typeof setTimeout === ‘undefined‘ ? undefined : setTimeout)));
首先我們,可以將require.js分為三個部分,
1)定義全域變數和協助函數
2)模組載入核心部分
3)定義require和define兩個方法,以及項目入口。
如果我們使用跟蹤代碼的話,會發現檔案一直都在定義變數和方法,一直執行到req({});這個語句調用req方法,傳入一個Null 物件作為參數,用於建立一個預設上下文。現在我們跟蹤代碼,簡單瞭解如何建立一個預設上下文。執行下面這段代碼
1 req = requirejs = function(deps, callback, errback, optional) { 2 // 此時傳入的參數時Null 物件 3 4 //Find the right context, use default 5 var context, config, 6 contextName = defContextName; // 預設上下文名為_ 7 8 // Determine if have config object in the call. 9 // 此時傳入一個Null 物件作為設定物件10 if (!isArray(deps) && typeof deps !== ‘string‘) {11 // deps is a config object12 config = deps;13 if (isArray(callback)) {14 // Adjust args if there are dependencies15 deps = callback;16 callback = errback;17 errback = optional;18 } else {19 deps = [];20 }21 }22 // config為空白對象,沒有設定context屬性,因此不設定上下文名23 if (config && config.context) {24 contextName = config.context;25 }26 27 // getOwn函數:協助函數,用於擷取對象中對應屬性的值,此時傳回值為undefined28 context = getOwn(contexts, contextName);29 if (!context) {30 /*31 s = req.s = {32 contexts: contexts,33 newContext: newContext34 };35 */36 // 初始化時,執行下面這段代碼,調用req.s中的newContext方法建立預設上下文37 // newContext方法時require.js的核心,此時我們只需要瞭解,它建立了一個預設上下文38 // 並且這個函數,只執行一次,之後的上下文,都是通過閉包,在閉包中,修改預設內容相關的資訊39 // 儲存自己的上下文資訊40 context = contexts[contextName] = req.s.newContext(contextName);41 }42 43 // 此時config為空白對象,執行configure方法幾乎沒什麼作用44 if (config) {45 context.configure(config);46 }47 48 // 跟蹤代碼可以發現調用context.require=>context.makeRequire()=>localRequire49 // context.require = context.makeRequire();50 // function localRequire(deps, callback, errback)51 // 返回一個localRequire52 return context.require(deps, callback, errback);53 };
到這裡,以及完成了context初始化。
然後繼續執行require({});下面的語句
1 // 重設列出的函數 2 //Exports some context-sensitive methods on global require. 3 each([ 4 ‘toUrl‘, 5 ‘undef‘, 6 ‘defined‘, 7 ‘specified‘ 8 ], function(prop) { 9 //Reference from contexts instead of early binding to default context,10 //so that during builds, the latest instance of the default context11 //with its config gets used.12 req[prop] = function() {13 var ctx = contexts[defContextName];14 return ctx.require[prop].apply(ctx, arguments);15 };16 });17 18 // 擷取添加指令碼的父親節點19 if (isBrowser) {20 head = s.head = document.getElementsByTagName(‘head‘)[0];21 //If BASE tag is in play, using appendChild is a problem for IE6.22 //When that browser dies, this can be removed. Details in this jQuery bug:23 //http://dev.jquery.com/ticket/270924 baseElement = document.getElementsByTagName(‘base‘)[0];25 if (baseElement) {26 head = s.head = baseElement.parentNode;27 }28 }
以上僅僅是建立了預設上下文,並進行簡單的處理。
require.js 源碼解讀——配置預設上下文