Skip to content

Commit 2f1a54d

Browse files
committed
walk remembers paths that were in node_modules
1 parent f3114e1 commit 2f1a54d

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

index.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ function Deps (opts) {
6969
};
7070
}
7171

72+
Deps.prototype._isTopLevel = function (file) {
73+
var isTopLevel = this.entries.some(function (main) {
74+
var m = path.relative(path.dirname(main), file);
75+
return m.split(/[\\\/]/).indexOf('node_modules') < 0;
76+
});
77+
if (!isTopLevel) {
78+
var m = path.relative(this.basedir, file);
79+
isTopLevel = m.split(/[\\\/]/).indexOf('node_modules') < 0;
80+
}
81+
return isTopLevel;
82+
};
83+
7284
Deps.prototype._transform = function (row, enc, next) {
7385
var self = this;
7486
if (typeof row === 'string') {
@@ -197,20 +209,12 @@ Deps.prototype.readFile = function (file, id, pkg) {
197209
};
198210

199211
Deps.prototype.getTransforms = function (file, pkg, opts) {
200-
// TODO: Needs FAKE path here.
201212
if (!opts) opts = {};
202213
var self = this;
203214

204215
var isTopLevel;
205-
if (opts.builtin) isTopLevel = false;
206-
else isTopLevel = this.entries.some(function (main) {
207-
var m = path.relative(path.dirname(main), file);
208-
return m.split(/[\\\/]/).indexOf('node_modules') < 0;
209-
});
210-
if (!isTopLevel && !opts.builtin) {
211-
var m = path.relative(this.basedir, file);
212-
isTopLevel = m.split(/[\\\/]/).indexOf('node_modules') < 0;
213-
}
216+
if (opts.builtin || opts.inNodeModules) isTopLevel = false;
217+
else isTopLevel = this._isTopLevel(file);
214218

215219
var transforms = [].concat(isTopLevel ? this.transforms : [])
216220
.concat(getTransforms(pkg, {
@@ -368,24 +372,25 @@ Deps.prototype.walk = function (id, parent, cb) {
368372
}
369373

370374
var c = self.cache && self.cache[file];
371-
if (c) return fromDeps(file, c.source, c.package, Object.keys(c.deps));
375+
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
372376

373377
self.readFile(file, id, pkg)
374378
.pipe(self.getTransforms(fakePath || file, pkg, {
375-
builtin: builtin
379+
builtin: builtin,
380+
inNodeModules: parent.inNodeModules
376381
}))
377382
.pipe(concat(function (body) {
378-
fromSource(file, body.toString('utf8'), pkg);
383+
fromSource(file, body.toString('utf8'), pkg, fakePath);
379384
}))
380385
;
381386
});
382387

383-
function fromSource (file, src, pkg) {
388+
function fromSource (file, src, pkg, fakePath) {
384389
var deps = rec.noparse ? [] : self.parseDeps(file, src);
385-
if (deps) fromDeps(file, src, pkg, deps);
390+
if (deps) fromDeps(file, src, pkg, fakePath, deps);
386391
}
387392

388-
function fromDeps (file, src, pkg, deps) {
393+
function fromDeps (file, src, pkg, fakePath, deps) {
389394
var p = deps.length;
390395
var resolved = {};
391396

@@ -399,11 +404,13 @@ Deps.prototype.walk = function (id, parent, cb) {
399404
if (--p === 0) done();
400405
return;
401406
}
407+
var isTopLevel = self._isTopLevel(fakePath || file);
402408
var current = {
403409
id: file,
404410
filename: file,
405411
paths: self.paths,
406-
package: pkg
412+
package: pkg,
413+
inNodeModules: parent.inNodeModules || !isTopLevel
407414
};
408415
self.walk(id, current, function (err, r) {
409416
resolved[id] = r;

0 commit comments

Comments
 (0)