Search code examples
browserify

Browserify bundle.js demystified


So when we browserify modules, it spits out this mysterious function:

(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);
            var f=new Error("Cannot find module '"+o+"'");
            throw f.code="MODULE_NOT_FOUND",f
        }
        var l=n[o]={exports:{}};
        t[o][0].call(l.exports,function(e){
             var n=t[o][1][e];
             return s(n?n:e)
        },l,l.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
})({
1:[function(require,module,exports){....

Now I've been looking all over for a source, explanation or one with readable variables but couldn't find it.

For example:

 var a=typeof require=="function"&&require;

It seems that here the require() function is created, How is this defining it?

It's mainly the fact these variables are not readable, am I wrong to assume the following?

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

But that still leaves the s(o,u), which takes 2 arguments, but the only time that it is invoked, only gets 1 arg..

},l , l.exports, e, require, module, exports)

Where does that belong to?

Can anyone help fill in the blanks?


Solution

  • Stop looking, here is commented source file ;)

    https://github.com/substack/browser-pack/blob/master/prelude.js