How can I find the definition of the `require` function of browserify?

Hanfei Sun

In the browserify bundled file, I saw codes like this:

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
})(.........)

The definition of require function looks short, but I find the variable name in require definition seems to be uglified.. Where can I find the original definition/implementation of the require function of Browserify?

Hanfei Sun

I think I find the answer at node_modules/browserify/node_modules/browser-pack/prelude.js, but the codes look cryptic for me.. Hope someone could give a better explanation..

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I require a html template with Browserify

From Dev

Can i use wild card with browserify require?

From Dev

Why can I not use a variable as parameter in the require() function of node.js (browserify)?

From Dev

How can I mock builtin `require` function?

From Dev

Where can I find the function definition of route and resource?

From Dev

Why can't I require(...) in a loop using browserify?

From Dev

Why can't I require a constructor and immediately use it with browserify?

From Dev

Why can't I require a constructor and immediately use it with browserify?

From Dev

How can i find the definition of ?android:attr/dividerVertical?

From Dev

How can i find the definition of ?android:attr/dividerVertical?

From Dev

How can I view the definition of a function in lisp (sbcl)?

From Dev

Linker can't find function definition in a namespace

From Dev

Linker can't find function definition in a namespace

From Dev

How can I find the arguments in a php function?

From Dev

How can I find the arguments in a php function?

From Dev

How can I find the function of a python module?

From Dev

How can I find exact value for this function?

From Dev

how i require session for a function

From Dev

I can't find definition for `mock!`

From Dev

Can browserify require a vinyl file generated in gulp?

From Dev

Can browserify require a vinyl file generated in gulp?

From Dev

How can I attach a function to JQuery's find() function?

From Dev

out of three libraries, require.js is failing to load one; how can I find out why?

From Dev

How can I analyse and reduce the size of my browserify generated files?

From Dev

How can I build mapbox-gl.js with Browserify?

From Dev

How can I use bluebird-q in browser without browserify?

From Dev

How can I apply browserify to my gulp file?

From Dev

How can I build mapbox-gl.js with Browserify?

From Dev

How to I search for the definition of a function without a name

Related Related

  1. 1

    How can I require a html template with Browserify

  2. 2

    Can i use wild card with browserify require?

  3. 3

    Why can I not use a variable as parameter in the require() function of node.js (browserify)?

  4. 4

    How can I mock builtin `require` function?

  5. 5

    Where can I find the function definition of route and resource?

  6. 6

    Why can't I require(...) in a loop using browserify?

  7. 7

    Why can't I require a constructor and immediately use it with browserify?

  8. 8

    Why can't I require a constructor and immediately use it with browserify?

  9. 9

    How can i find the definition of ?android:attr/dividerVertical?

  10. 10

    How can i find the definition of ?android:attr/dividerVertical?

  11. 11

    How can I view the definition of a function in lisp (sbcl)?

  12. 12

    Linker can't find function definition in a namespace

  13. 13

    Linker can't find function definition in a namespace

  14. 14

    How can I find the arguments in a php function?

  15. 15

    How can I find the arguments in a php function?

  16. 16

    How can I find the function of a python module?

  17. 17

    How can I find exact value for this function?

  18. 18

    how i require session for a function

  19. 19

    I can't find definition for `mock!`

  20. 20

    Can browserify require a vinyl file generated in gulp?

  21. 21

    Can browserify require a vinyl file generated in gulp?

  22. 22

    How can I attach a function to JQuery's find() function?

  23. 23

    out of three libraries, require.js is failing to load one; how can I find out why?

  24. 24

    How can I analyse and reduce the size of my browserify generated files?

  25. 25

    How can I build mapbox-gl.js with Browserify?

  26. 26

    How can I use bluebird-q in browser without browserify?

  27. 27

    How can I apply browserify to my gulp file?

  28. 28

    How can I build mapbox-gl.js with Browserify?

  29. 29

    How to I search for the definition of a function without a name

HotTag

Archive