[Pkg-javascript-commits] [node-module-deps] 29/444: now using browser-resolve directly

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:47:40 UTC 2017


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

rouca pushed a commit to branch master
in repository node-module-deps.

commit fc85eccb6073d0ca6ed11189d8f73fd32f0faf36
Author: James Halliday <mail at substack.net>
Date:   Tue Feb 26 19:02:49 2013 -0800

    now using browser-resolve directly
---
 index.js       | 104 ++++++++++++++++++++++++++-------------------------------
 package.json   |   2 +-
 test/bundle.js |   1 +
 3 files changed, 50 insertions(+), 57 deletions(-)

diff --git a/index.js b/index.js
index a846001..9a203f6 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,8 @@
 var fs = require('fs');
 var path = require('path');
 
-var required = require('required');
+var browserResolve = require('browser-resolve');
+var detective = require('detective');
 var through = require('through');
 
 module.exports = function (mains, opts) {
@@ -10,74 +11,65 @@ module.exports = function (mains, opts) {
         return path.resolve(file);
     });
     
-    var files = {};
-    var cache = {};
+    var visited = {};
     var pending = 0;
+    var cache = {};
     
     var output = through();
     
     if (!opts) opts = {};
     if (opts.cache === undefined) opts.cache = cache;
+    var resolve = opts.resolve || browserResolve;
     opts.includeSource = true;
     
-    mains.forEach(function (file) {
-        pending ++;
-        var p = 2, src, rows;
-        
-        function done () {
-            if (!files[file]) {
-                files[file] = {
-                    id: file,
-                    source: src,
-                    entry: true,
-                    deps: rows.reduce(function (acc, dep) {
-                        acc[dep.id] = dep.filename;
-                        return acc;
-                    }, {})
-                };
-                output.queue(files[file]);
-            };
-            
-            walk(rows);
-            if (--pending === 0) output.queue(null);
-        }
-        
-        fs.readFile(file, 'utf8', function (err, s) {
-            if (err) return output.emit('error', err);
-            src = s;
-            if (--p === 0) done();
-        });
-        
-        required(file, opts, function (err, r) {
-            if (err) return output.emit('error', err);
-            rows = r;
-            if (--p === 0) done();
-        });
-    });
+    var top = { id: '/', filename: '/', paths: [] };
+    mains.forEach(function (main) { walk(main, top) });
     
-    if (pending === 0) process.nextTick(function () {
-        output.queue(null);
-    });
+    if (mains.length === 0) {
+        process.nextTick(output.queue.bind(output, null));
+    }
     
     return output;
     
-    function walk (rows) {
-        rows.forEach(function (row) {
-            if (files[row.filename]) return;
-            var r = files[row.filename] = {
-                id: row.filename,
-                source: row.source,
-                deps: (row.deps || []).reduce(function (acc, dep) {
-                    acc[dep.id] = dep.filename;
-                    return acc;
-                }, {})
-            };
-            if (mains.indexOf(row.filename) >= 0) {
-                r.entry = true;
-            }
-            output.queue(r);
+    function walk (id, parent, cb) {
+        pending ++;
+        
+        resolve(id, parent, function (err, file) {
+            if (err) return output.emit('error', err);
+            if (cb) cb(file);
+            if (visited[file]) { --pending; return };
+            visited[file] = true;
             
-            walk(row.deps || []);
+            fs.readFile(file, 'utf8', function (err, src) {
+                if (err) output.emit('error', err);
+                else parseDeps(file, src);
+                
+                if (--pending === 0) output.queue(null);
+            });
+        });
+    }
+    
+    function parseDeps (file, src) {
+        var deps = detective(src);
+        var p = deps.length;
+        var current = { id: file, filename: file, paths: [] };
+        var resolved = {};
+        
+        deps.forEach(function (id) {
+            walk(id, current, function (r) {
+                resolved[id] = r;
+                if (--p > 0) return;
+                
+                var rec = {
+                    id: file,
+                    source: src,
+                    deps: resolved
+                };
+                if (mains.indexOf(r) >= 0) {
+                    rec.entry = true;
+                }
+                output.queue(rec);
+            });
         });
     }
 };
diff --git a/package.json b/package.json
index 03b2997..ccb6a3a 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     },
     "dependencies": {
         "JSONStream": "~0.4.3",
-        "required": "~0.3.0",
+        "browser-resolve": "~0.0.3",
         "through": "~2.2.0"
     },
     "devDependencies": {
diff --git a/test/bundle.js b/test/bundle.js
index b1605bb..7ee8d6d 100644
--- a/test/bundle.js
+++ b/test/bundle.js
@@ -6,6 +6,7 @@ var packer = require('browser-pack');
 test('bundle', function (t) {
     t.plan(1);
     var p = parser(__dirname + '/files/main.js');
+    p.on('error', t.fail.bind(t));
     var pack = packer();
     
     p.pipe(JSONStream.stringify()).pipe(pack);

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



More information about the Pkg-javascript-commits mailing list