javascript - 如何找到browserify的 `require`函数的定义?

标签 javascript amd browserify commonjs

在 browserify 捆绑文件中,我看到了这样的代码:

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 功能?

最佳答案

我想我在 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;
})

关于javascript - 如何找到browserify的 `require`函数的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919826/

相关文章:

javascript - 在浏览器历史记录中导航时,从最新页面返回多少步?

javascript - 将一个旋转对象包含在另一个旋转对象中 FabricJS

javascript - 如何在 mvc razor View 中从 IEnumerable<model> 返回带有索引的模型?

javascript - 在 AMD 模块之前添加非 AMD 模块

javascript - browserify 使用 react-router 加载 ReactJS 两次

javascript - 如何从浏览器 onClick 事件中触发击键?

requireJS - 多页填充程序中不匹配的匿名定义()模块

javascript - 在 Babel 中使用 es7 函数

javascript - 使用纯npm脚本创建vendor.js包

javascript - 使用 Webpack 需要第三方 RequireJS 模块