Skip to content

Commit d4cd26f

Browse files
committed
Merge pull request #58 from mvayngrib/alt-browser
fix pathFilter for alt browser field
2 parents 35df264 + daa08a9 commit d4cd26f

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

index.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function nodeModulesPaths (start, cb) {
1515
for (var i = parts.length - 1; i >= 0; i--) {
1616
if (parts[i] === 'node_modules') continue;
1717
var dir = path.join.apply(
18-
path, parts.slice(0, i + 1).concat(["node_modules"])
18+
path, parts.slice(0, i + 1).concat(['node_modules'])
1919
);
2020
if (!parts[0].match(/([A-Za-z]:)/)) {
2121
dir = '/' + dir;
@@ -170,27 +170,27 @@ function build_resolve_opts(opts, base) {
170170

171171
var pathFilter = opts.pathFilter;
172172
opts.pathFilter = function(info, path, relativePath) {
173-
if(relativePath[0] != '.') {
174-
relativePath = './' + relativePath;
175-
}
176-
var mappedPath;
177-
if(pathFilter) {
178-
mappedPath = pathFilter.apply(this, arguments);
179-
}
180-
if(mappedPath) {
181-
return mappedPath;
182-
}
183-
if(!info.browser) {
184-
return;
185-
}
186-
187-
if(typeof info.browser) {
188-
mappedPath = info.browser[relativePath];
189-
if(!mappedPath && (relativePath.lastIndexOf(".js") === relativePath.length-3) ) {
190-
mappedPath = info.browser[relativePath+".js"];
191-
}
192-
return mappedPath;
193-
}
173+
if (relativePath[0] != '.') {
174+
relativePath = './' + relativePath;
175+
}
176+
var mappedPath;
177+
if (pathFilter) {
178+
mappedPath = pathFilter.apply(this, arguments);
179+
}
180+
if (mappedPath) {
181+
return mappedPath;
182+
}
183+
184+
var replacements = info[browser];
185+
if (!replacements) {
186+
return;
187+
}
188+
189+
mappedPath = replacements[relativePath];
190+
if (!mappedPath && (relativePath.lastIndexOf('.js') === relativePath.length - 3)) {
191+
mappedPath = replacements[relativePath + '.js'];
192+
}
193+
return mappedPath;
194194
};
195195

196196
return opts;
@@ -324,4 +324,3 @@ function getReplacements(info, browser) {
324324
}
325325

326326
module.exports = resolve;
327-

test/fixtures/node_modules/alt-browser-field/chromeapp-direct.js

Whitespace-only changes.

test/fixtures/node_modules/alt-browser-field/chromeapp.js

Whitespace-only changes.

test/fixtures/node_modules/alt-browser-field/direct.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/alt-browser-field/package.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/modules.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,16 @@ test('alt-browser field', function(done) {
250250
done();
251251
});
252252
});
253+
254+
test('alt-browser deep module reference mapping', function(done) {
255+
resolve('alt-browser-field/direct', {
256+
basedir: __dirname + '/fixtures',
257+
package: { main: 'fixtures' },
258+
browser: 'chromeapp'
259+
}, function(err, path, pkg) {
260+
assert.ifError(err);
261+
assert.equal(path, require.resolve('./fixtures/node_modules/alt-browser-field/chromeapp-direct'));
262+
assert.equal(pkg.main, './chromeapp.js');
263+
done();
264+
});
265+
});

0 commit comments

Comments
 (0)