Skip to content

Commit 5ad4865

Browse files
dchermandefunctzombie
authored andcommitted
Align our sync resolving behavior with async (#90)
Since there was diverging behavior between the two codepaths, our async path would properly resolve shimmed relative path files without extensions, however our sync path would not. Additionally Normalize the path to empty.js correctly on windows. Fixes #89
1 parent 0fb4243 commit 5ad4865

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function find_shims_in_package(pkgJson, cur_path, shims, browser) {
5353
Object.keys(replacements).forEach(function(key) {
5454
var val;
5555
if (replacements[key] === false) {
56-
val = __dirname + '/empty.js';
56+
val = path.normalize(__dirname + '/empty.js');
5757
}
5858
else {
5959
val = replacements[key];
@@ -299,15 +299,17 @@ resolve.sync = function (id, opts) {
299299

300300
// we must always load shims because the browser field could shim out a module
301301
var shims = load_shims_sync(paths, opts.browser);
302+
var resid = path.resolve(opts.basedir || path.dirname(opts.filename), id);
302303

303-
if (shims[id]) {
304+
if (shims[id] || shims[resid]) {
305+
var xid = shims[id] ? id : resid;
304306
// if the shim was is an absolute path, it was fully resolved
305-
if (shims[id][0] === '/') {
306-
return shims[id];
307+
if (shims[xid][0] === '/') {
308+
return resv.sync(shims[xid], build_resolve_opts(opts, base));
307309
}
308310

309311
// module -> alt-module shims
310-
id = shims[id];
312+
id = shims[xid];
311313
}
312314

313315
var modules = opts.modules || Object.create(null);

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

Lines changed: 5 additions & 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-t/x.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-t/y.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/modules-sync.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ test('test foobar -> module-b replacement', function() {
7373
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
7474
});
7575

76+
// browser field in package.json maps "relative file" -> "relative file" with no extension
77+
test('test ./x -> ./y replacement', function() {
78+
var parent = {
79+
filename: fixtures_dir + '/module-t/index.js',
80+
package: { main: './index.js' }
81+
};
82+
83+
var path = resolve.sync('./x', parent);
84+
assert.equal(path, require.resolve('./fixtures/node_modules/module-t/y.js'));
85+
});
86+
7687
// same as above but replacing core
7788
test('test core -> module-c replacement', function() {
7889
var parent = {

test/modules.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ test('test foobar -> module-b replacement with transform', function(done) {
194194
});
195195
});
196196

197+
// browser field in package.json maps "relative file" -> "relative file" with no extension
198+
test('test ./x -> ./y replacement', function(done) {
199+
var parent = {
200+
filename: fixtures_dir + '/module-t/index.js',
201+
package: { main: './index.js' }
202+
};
203+
204+
resolve('./x', parent, function(err, path, pkg) {
205+
assert.ifError(err);
206+
assert.equal(path, require.resolve('./fixtures/node_modules/module-t/y.js'));
207+
done();
208+
});
209+
});
210+
197211
test('test foobar -> module-i replacement with transform in replacement', function(done) {
198212
var parent = {
199213
filename: fixtures_dir + '/module-j/index.js',

0 commit comments

Comments
 (0)