[Pkg-javascript-commits] [node-browser-pack] 21/141: client require should respect any parent require

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:23:21 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browser-pack.

commit ee3124ad5376c20de3e54708075fa830a294d3a5
Author: Roman Shtylman <shtylman at gmail.com>
Date:   Fri Feb 22 22:59:47 2013 -0500

    client require should respect any parent require
    
    This allows for chaining of require calls to delegate to a parent
    require.
---
 index.js        |  2 +-
 prelude.js      | 38 ++++++++++++++++++++++++++------------
 test/comment.js |  4 +++-
 test/raw.js     |  4 +++-
 4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/index.js b/index.js
index c88e9f9..8b0a806 100644
--- a/index.js
+++ b/index.js
@@ -2,7 +2,7 @@ var JSONStream = require('JSONStream');
 var duplexer = require('duplexer');
 var through = require('through');
 
-var prelude = '(' + require('./prelude').toString() + ')({';
+var prelude = '(' + require('./prelude').toString() + ')(typeof require !== "undefined"&&require, {';
 
 module.exports = function (opts) {
     if (!opts) opts = {};
diff --git a/prelude.js b/prelude.js
index f5d3e41..e9e803b 100644
--- a/prelude.js
+++ b/prelude.js
@@ -1,14 +1,28 @@
-module.exports = function(p,c,e){
-    function r(n){
-        if(!c[n]){
-            if(!p[n])return;
-            c[n]={exports:{}};
-            p[n][0](function(x){
-                return r(p[n][1][x])
-            },c[n],c[n].exports);
+
+// 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
+
+module.exports = function(parent_req, modules, cache, entry) {
+    function require(name){
+        if(!cache[name]) {
+            if(!modules[name]) {
+                if (parent_req) return parent_req(name);
+                throw new Error('Cannot find module \'' + name + '\'');
+            }
+            var m = cache[name] = {exports:{}};
+            modules[name][0](function(x){
+                var id = modules[name][1][x];
+                // if we cannot find the item within our internal map revert to parent
+                return id ? require(id) : parent_req(x);
+            },m,m.exports);
         }
-        return c[n].exports
+        return cache[name].exports
     }
-    for(var i=0;i<e.length;i++)r(e[i]);
-    return r
-};
+    for(var i=0;i<entry.length;i++) require(entry[i]);
+    return require;
+}
diff --git a/test/comment.js b/test/comment.js
index 8aa3e1b..88f3569 100644
--- a/test/comment.js
+++ b/test/comment.js
@@ -11,7 +11,9 @@ test('trailing comment', function (t) {
         var r = Function(['T'], 'return ' + src)(t);
         t.equal(r('xyz')(5), 555);
         t.equal(r('xyz')(5), 555);
-        t.equal(r('zzz'), undefined);
+        t.throws(function() {
+            r('zzz');
+        }, /Cannot find module 'zzz'/);
     });
     
     p.write({
diff --git a/test/raw.js b/test/raw.js
index 787edee..a92327f 100644
--- a/test/raw.js
+++ b/test/raw.js
@@ -11,7 +11,9 @@ test('raw', function (t) {
         var r = Function(['T'], 'return ' + src)(t);
         t.equal(r('xyz')(5), 555);
         t.equal(r('xyz')(5), 555);
-        t.equal(r('zzz'), undefined);
+        t.throws(function() {
+            r('zzz');
+        }, /Cannot find module 'zzz'/);
     });
     
     p.write({

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browser-pack.git



More information about the Pkg-javascript-commits mailing list