Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve deps to row.id rather than row.file #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Deps (opts) {
this._emittedPkg = {};
this.visited = {};
this.walking = {};
this.entries = [];
this.entries = {};
this._input = [];

this.paths = opts.paths || process.env.NODE_PATH || '';
Expand Down Expand Up @@ -87,7 +87,7 @@ Deps.prototype._transform = function (row, enc, next) {
var basedir = defined(row.basedir, self.basedir);

if (row.entry !== false) {
self.entries.push(path.resolve(basedir, row.file || row.id));
self.entries[path.resolve(basedir, row.file || row.id)] = row.id;
}

self.lookupPackage(row.file, function (err, pkg) {
Expand Down Expand Up @@ -199,7 +199,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {

var isTopLevel;
if (opts.builtin) isTopLevel = false;
else isTopLevel = this.entries.some(function (main) {
else isTopLevel = Object.keys(this.entries).some(function (main) {
var m = path.relative(path.dirname(main), file);
return m.split(/[\\\/]/).indexOf('node_modules') < 0;
});
Expand Down Expand Up @@ -398,7 +398,11 @@ Deps.prototype.walk = function (id, parent, cb) {
package: pkg
};
self.walk(id, current, function (err, r) {
resolved[id] = r;
if (typeof self.entries[r] !== 'undefined') {
resolved[id] = self.entries[r];
} else {
resolved[id] = r;
}
if (--p === 0) done();
});
});
Expand All @@ -411,7 +415,7 @@ Deps.prototype.walk = function (id, parent, cb) {
if (!rec.deps) rec.deps = resolved;
if (!rec.file) rec.file = file;

if (self.entries.indexOf(file) >= 0) {
if (self.entries.hasOwnProperty(file)) {
rec.entry = true;
}
self.push(rec);
Expand Down
8 changes: 4 additions & 4 deletions test/cache_partial_expose.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ test('preserves expose and entry with partial cache', function(t) {
id: files.bar,
file: files.bar,
source: sources.bar,
deps: {xyz: files.xyz}
deps: {xyz: 'xyz'}
},
{
file: files.foo,
id: files.foo,
source: sources.foo,
deps: {'./lib/abc': files.abc}
deps: {'./lib/abc': 'abc'}
},
{
id: 'abc',
Expand All @@ -84,8 +84,8 @@ test('preserves expose and entry with partial cache', function(t) {
source: sources.main,
deps: {
'./bar': files.bar,
abc: files.abc,
xyz: files.xyz
abc: 'abc',
xyz: 'xyz'
},
entry: true
},
Expand Down
53 changes: 53 additions & 0 deletions test/resolve_deps_to_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var parser = require('../');
var test = require('tape');
var fs = require('fs');
var path = require('path');

var files = {
main: path.join(__dirname, '/files/main.js'),
foo: path.join(__dirname, '/files/foo.js'),
bar: path.join(__dirname, '/files/bar.js')
};

var sources = Object.keys(files).reduce(function (acc, file) {
acc[file] = fs.readFileSync(files[file], 'utf8');
return acc;
}, {});

test('deps resolve to id', function (t) {
t.plan(1);
var p = parser();
var fooID = path.relative(process.cwd(), files.foo);
p.write({ file: files.main, entry: true });
p.end({ file: files.foo, id: fooID, entry: true });

var rows = [];
p.on('data', function (row) { rows.push(row) });
p.on('end', function () {
t.same(rows.sort(cmp), [
{
id: files.main,
file: files.main,
source: sources.main,
entry: true,
deps: { './foo': fooID }
},
{
id: fooID,
file: files.foo,
source: sources.foo,
entry: true,
deps: { './bar': files.bar }
},
{
id: files.bar,
file: files.bar,
source: sources.bar,
deps: {}
}
].sort(cmp));
});
});

function cmp (a, b) { return a.id < b.id ? -1 : 1 }