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

Adding basedir to fix node resolver #154

Merged
merged 3 commits into from
Nov 13, 2018
Merged
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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ Deps.prototype.walk = function (id, parent, cb) {
var current = {
id: file,
filename: file,
basedir: path.dirname(file),
paths: self.paths,
package: pkg,
inNodeModules: parent.inNodeModules || !isTopLevel
Expand Down
5 changes: 5 additions & 0 deletions test/files/resolve/bar/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var bar2 = require('./bar2');

module.exports = function () {
return 'bar';
};
3 changes: 3 additions & 0 deletions test/files/resolve/bar/bar2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return 'bar2';
};
3 changes: 3 additions & 0 deletions test/files/resolve/foo/baz/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return 'baz';
};
6 changes: 6 additions & 0 deletions test/files/resolve/foo/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var bar = require('../bar/bar.js');
var baz = require('./baz/baz.js');

module.exports = function () {
return 'foo';
};
121 changes: 121 additions & 0 deletions test/resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
var fs = require('fs');
var path = require('path');

var mdeps = require('../');
var test = require('tap').test;
var nodeResolve = require('resolve');
var browserResolve = require('browser-resolve');

var missing = path.join(__dirname, '/missing');

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

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

var expectedRows = [
{
"deps": {},
"file": files.baz,
"id": files.baz,
"source": sources.baz
},
{
"deps": {},
"file": files.bar2,
"id": files.bar2,
"source": sources.bar2
},
{
"deps": {
"./bar2": files.bar2
},
"file": files.bar,
"id": files.bar,
"source": sources.bar
},
{
"deps": {
"../bar/bar.js": files.bar,
"./baz/baz.js": files.baz
},
"entry": true,
"file": files.foo,
"id": "foo",
"source": sources.foo
}
];

test('browser resolve - missing', function (t) {
t.plan(1);
var d = mdeps({resolve: browserResolve});

d.end({id: 'missing', file: missing, entry: true});

d.on('end', function () {
t.fail('errored');
});
d.on('error', function (err) {
t.match(
String(err),
/Cannot find module .*/
);
});
});

test('node resolve - missing', function (t) {
t.plan(1);
var d = mdeps({resolve: nodeResolve});

d.end({id: 'missing', file: missing, entry: true});

d.on('end', function () {
t.fail('errored');
});
d.on('error', function (err) {
t.match(
String(err),
/Cannot find module .*/
);
});
});

test('browser resolve', function (t) {
t.plan(1);
var d = mdeps({resolve: browserResolve});

d.end({id: 'foo', file: files.foo, entry: true});

var rows = [];
d.on('data', function (row) {rows.push(row)});
d.on('end', function () {
t.same(rows, expectedRows);
});
d.on('error', function () {
t.fail('errored');
});
});

test('node resolve', function (t) {
t.plan(1);
var d = mdeps({resolve: nodeResolve});

d.end({id: 'foo', file: files.foo, entry: true});

var rows = [];
d.on('data', function (row) {rows.push(row)});
d.on('end', function () {
t.same(rows, expectedRows);
});
d.on('error', function () {
t.fail('errored');
});
});