Skip to content

Commit 224a2b1

Browse files
committed
Merge pull request #73 from src-code/issue-69
Fixing resolution of deep module references without a file extension
2 parents 42f96b2 + 17ccad2 commit 224a2b1

File tree

6 files changed

+32
-2
lines changed

6 files changed

+32
-2
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function build_resolve_opts(opts, base) {
168168
};
169169

170170
var pathFilter = opts.pathFilter;
171-
opts.pathFilter = function(info, path, relativePath) {
171+
opts.pathFilter = function(info, resvPath, relativePath) {
172172
if (relativePath[0] != '.') {
173173
relativePath = './' + relativePath;
174174
}
@@ -186,8 +186,11 @@ function build_resolve_opts(opts, base) {
186186
}
187187

188188
mappedPath = replacements[relativePath];
189-
if (!mappedPath && (relativePath.lastIndexOf('.js') === relativePath.length - 3)) {
189+
if (!mappedPath && path.extname(relativePath) === '') {
190190
mappedPath = replacements[relativePath + '.js'];
191+
if (!mappedPath) {
192+
mappedPath = replacements[relativePath + '.json'];
193+
}
191194
}
192195
return mappedPath;
193196
};

test/fixtures/node_modules/module-n/browser-bar.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/module-n/browser-foo.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/module-n/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/module-n/package.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/modules.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ test('deep module reference mapping', function(done) {
9999
});
100100
});
101101

102+
// package.json has browser field as object
103+
// test that file resolves even though the file extension is omitted
104+
test('deep module reference mapping without file extension - .js', function(done) {
105+
resolve('module-n/foo', { basedir: __dirname + '/fixtures', package: { main: 'fixtures' } }, function(err, path, pkg) {
106+
assert.ifError(err);
107+
assert.equal(path, require.resolve('./fixtures/node_modules/module-n/browser-foo'));
108+
done();
109+
});
110+
});
111+
test('deep module reference mapping without file extension - .json', function(done) {
112+
resolve('module-n/bar', { basedir: __dirname + '/fixtures', package: { main: 'fixtures' } }, function(err, path, pkg) {
113+
assert.ifError(err);
114+
assert.equal(path, require.resolve('./fixtures/node_modules/module-n/browser-bar'));
115+
done();
116+
});
117+
});
118+
102119
// browser field in package.json maps ./foo.js -> ./browser.js
103120
// when we resolve ./foo while in module-e, this mapping should take effect
104121
// the result is that ./foo resolves to ./browser

0 commit comments

Comments
 (0)