browser-resolve
Advanced tools
Comparing version
21
index.js
@@ -59,2 +59,3 @@ // builtin | ||
catch (err) { | ||
err.message = pkg_path + ' : ' + err.message | ||
return cb(err); | ||
@@ -65,3 +66,3 @@ } | ||
// many packages used this field historically | ||
if (info.browserify) { | ||
if (typeof info.browserify === 'string' && !info.browser) { | ||
info.browser = info.browserify; | ||
@@ -92,3 +93,3 @@ } | ||
if (key[0] !== '/' && key[0] !== '.') { | ||
return shims[key] = val; | ||
return shims[key] = info.browser[key]; | ||
} | ||
@@ -137,3 +138,9 @@ | ||
if (shims[id]) { | ||
return cb(null, shims[id]); | ||
// if the shim was is an absolute path, it was fully resolved | ||
if (shims[id][0] === '/') { | ||
return cb(null, shims[id], opts.package); | ||
} | ||
// module -> alt-module shims | ||
id = shims[id]; | ||
} | ||
@@ -145,3 +152,5 @@ | ||
paths: opts.paths, | ||
extensions: opts.extensions, | ||
basedir: base, | ||
package: opts.package, | ||
packageFilter: function(info) { | ||
@@ -151,3 +160,3 @@ if (opts.packageFilter) info = opts.packageFilter(info); | ||
// support legacy browserify field | ||
if (info.browserify) { | ||
if (typeof info.browserify === 'string' && !info.browser) { | ||
info.browser = info.browserify; | ||
@@ -171,5 +180,5 @@ } | ||
} | ||
}, function(err, full) { | ||
}, function(err, full, pkg) { | ||
var resolved = (shims) ? shims[full] || full : full; | ||
cb(null, resolved); | ||
cb(null, resolved, pkg); | ||
}); | ||
@@ -176,0 +185,0 @@ }); |
{ | ||
"name": "browser-resolve", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "resolve which handles browser field support in package.json", | ||
@@ -20,3 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"resolve": "0.3.1" | ||
"resolve": "0.4.0" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
@@ -8,5 +8,6 @@ var assert = require('assert'); | ||
test('index.js of module dir', function(done) { | ||
resolve('module-a', { paths: [ fixtures_dir ] }, function(err, path) { | ||
resolve('module-a', { paths: [ fixtures_dir ], package: { main: 'fixtures' } }, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-a/index')); | ||
assert.strictEqual(pkg, undefined); | ||
done(); | ||
@@ -18,5 +19,6 @@ }); | ||
test('alternate main', function(done) { | ||
resolve('module-b', { paths: [ fixtures_dir ] }, function(err, path) { | ||
resolve('module-b', { paths: [ fixtures_dir ], package: { main: 'fixtures' } }, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); | ||
assert.strictEqual(pkg.main, './main.js'); | ||
done(); | ||
@@ -28,5 +30,6 @@ }); | ||
test('string browser field as main', function(done) { | ||
resolve('module-c', { paths: [ fixtures_dir ] }, function(err, path) { | ||
resolve('module-c', { paths: [ fixtures_dir ], package: { main: 'fixtures' } }, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser')); | ||
assert.equal(pkg.main, './browser.js'); | ||
done(); | ||
@@ -40,8 +43,10 @@ }); | ||
filename: fixtures_dir + '/module-c/browser.js', | ||
paths: [ fixtures_dir + '/module-c/node_modules' ] | ||
paths: [ fixtures_dir + '/module-c/node_modules' ], | ||
package: { main: './browser.js' } | ||
}; | ||
resolve('./bar', parent, function(err, path) { | ||
resolve('./bar', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/bar')); | ||
assert.equal(pkg.main, './browser.js'); | ||
done(); | ||
@@ -55,5 +60,6 @@ }); | ||
test('object browser field as main', function(done) { | ||
resolve('module-d', { paths: [ fixtures_dir ] }, function(err, path) { | ||
resolve('module-d', { paths: [ fixtures_dir ], package: { main: 'fixtures' } }, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-d/browser')); | ||
assert.equal(pkg.main, './browser.js'); | ||
done(); | ||
@@ -68,8 +74,10 @@ }); | ||
var parent = { | ||
filename: fixtures_dir + '/module-e/main.js' | ||
filename: fixtures_dir + '/module-e/main.js', | ||
package: { main: './main.js' } | ||
}; | ||
resolve('./foo', parent, function(err, path) { | ||
resolve('./foo', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-e/browser')); | ||
assert.equal(pkg.main, './main.js'); | ||
done(); | ||
@@ -79,2 +87,63 @@ }); | ||
// browser field in package.json maps "module" -> "alternate module" | ||
test('test foobar -> module-b replacement', function(done) { | ||
var parent = { | ||
filename: fixtures_dir + '/module-h/index.js', | ||
package: { main: './index.js' } | ||
}; | ||
resolve('foobar', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); | ||
assert.equal(pkg.main, './main.js'); | ||
done(); | ||
}); | ||
}); | ||
// same as above but replacing core | ||
test('test core -> module-c replacement', function(done) { | ||
var parent = { | ||
filename: fixtures_dir + '/module-h/index.js', | ||
package: { main: './index.js' } | ||
}; | ||
resolve('querystring', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser')); | ||
assert.equal(pkg.main, './browser.js'); | ||
done(); | ||
}); | ||
}); | ||
// browser field in package.json maps "module" -> "alternate module" | ||
test('test foobar -> module-b replacement with transform', function(done) { | ||
var parent = { | ||
filename: fixtures_dir + '/module-i/index.js', | ||
package: { main: './index.js' } | ||
}; | ||
resolve('foobar', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); | ||
assert.equal(pkg.main, './main.js'); | ||
done(); | ||
}); | ||
}); | ||
test('test foobar -> module-i replacement with transform in replacement', function(done) { | ||
var parent = { | ||
filename: fixtures_dir + '/module-j/index.js', | ||
package: { main: './index.js' } | ||
}; | ||
resolve('foobar', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-i/index')); | ||
assert.equal(pkg.main, './index.js'); | ||
assert.equal(pkg.browser['foobar'], 'module-b'); | ||
assert.equal(pkg.browserify.transform, 'deamdify'); | ||
done(); | ||
}); | ||
}); | ||
// same as above, but without a paths field in parent | ||
@@ -84,8 +153,10 @@ // should still checks paths on the filename of parent | ||
var parent = { | ||
filename: fixtures_dir + '/module-f/lib/main.js' | ||
filename: fixtures_dir + '/module-f/lib/main.js', | ||
package: { main: './lib/main.js' } | ||
}; | ||
resolve('./foo', parent, function(err, path) { | ||
resolve('./foo', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-f/lib/browser')); | ||
assert.equal(pkg.main, './lib/main.js'); | ||
done(); | ||
@@ -97,8 +168,10 @@ }); | ||
var parent = { | ||
filename: fixtures_dir + '/module-g/index.js' | ||
filename: fixtures_dir + '/module-g/index.js', | ||
package: { main: './index.js' } | ||
}; | ||
resolve('foobar', parent, function(err, path) { | ||
resolve('foobar', parent, function(err, path, pkg) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('./fixtures/node_modules/module-g/foobar-browser')); | ||
assert.equal(pkg.main, './index.js'); | ||
done(); | ||
@@ -105,0 +178,0 @@ }); |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
22504
52.74%24
14.29%499
63.07%2
100%+ Added
- Removed
Updated