2015-01-13 69 views
2

在browserify捆綁的文件,我看到這樣的代碼:如何找到browserify的`require`函數的定義?

require = (function e(t, n, r) { 
    function s(o, u) { 
     if (!n[o]) { 
      if (!t[o]) { 
       var a = typeof require == "function" && require; 
       if (!u && a) 
        return a(o, !0); 
       if (i) 
        return i(o, !0); 
       throw new Error("Cannot find module '" + o + "'") 
      } 
      var f = n[o] = {exports: {}}; 
      t[o][0].call(f.exports, function(e) { 
       var n = t[o][1][e]; 
       return s(n ? n : e) 
      }, f, f.exports, e, t, n, r) 
     } 
     return n[o].exports 
    } 
    var i = typeof require == "function" && require; 
    for (var o = 0; o < r.length; o++) 
     s(r[o]); 
    return s 
})(.........) 

require函數的定義看起來很短,但我覺得在require定義的變量名似乎變醜..我在哪裏可以找到原Browserify的require函數的定義/實現?

+0

https://github.com/substack/node-browserify /blob/master/index.js#L88 – andrusieczko

+0

@andrusieczko這看起來與上面的代碼有很大的不同。我不認爲這是原始代碼。 –

回答

2

我想我找到node_modules/browserify/node_modules/browser-pack/prelude.js答案,但代碼看起來神祕的我..希望有人能夠給出一個更好的解釋..

// modules are defined as an array 
// [ module function, map of requireuires ] 
// 
// map of requireuires is short require name -> numeric require 
// 
// anything defined in a previous bundle is accessed via the 
// orig method which is the requireuire for previous bundles 

(function outer (modules, cache, entry) { 
    // Save the require from previous bundle to this closure if any 
    var previousRequire = typeof require == "function" && require; 

    function newRequire(name, jumped){ 
     if(!cache[name]) { 
      if(!modules[name]) { 
       // if we cannot find the the module within our internal map or 
       // cache jump to the current global require ie. the last bundle 
       // that was added to the page. 
       var currentRequire = typeof require == "function" && require; 
       if (!jumped && currentRequire) return currentRequire(name, true); 

       // If there are other bundles on this page the require from the 
       // previous one is saved to 'previousRequire'. Repeat this as 
       // many times as there are bundles until the module is found or 
       // we exhaust the require chain. 
       if (previousRequire) return previousRequire(name, true); 
       var err = new Error('Cannot find module \'' + name + '\''); 
       err.code = 'MODULE_NOT_FOUND'; 
       throw err; 
      } 
      var m = cache[name] = {exports:{}}; 
      modules[name][0].call(m.exports, function(x){ 
       var id = modules[name][1][x]; 
       return newRequire(id ? id : x); 
      },m,m.exports,outer,modules,cache,entry); 
     } 
     return cache[name].exports; 
    } 
    for(var i=0;i<entry.length;i++) newRequire(entry[i]); 

    // Override the current require with this new one 
    return newRequire; 
}) 
+0

這一個絕對是有問題的代碼 – XGreen