browser-resolve
Advanced tools
Comparing version 0.1.1 to 1.0.0
42
index.js
@@ -8,18 +8,2 @@ // builtin | ||
// core modules replaced by their browser capable counterparts | ||
var core = {}; | ||
// load core modules from builtin dir | ||
fs.readdirSync(__dirname + '/builtin/').forEach(function(file) { | ||
core[path.basename(file, '.js')] = path.join(__dirname, '/builtin/', file); | ||
}); | ||
// manually add core which are provided by modules | ||
core['http'] = require.resolve('http-browserify'); | ||
core['vm'] = require.resolve('vm-browserify'); | ||
core['crypto'] = require.resolve('crypto-browserify'); | ||
core['console'] = require.resolve('console-browserify'); | ||
core['zlib'] = require.resolve('zlib-browserify'); | ||
core['buffer'] = require.resolve('buffer-browserify'); | ||
// given a path, create an array of node_module paths for it | ||
@@ -112,14 +96,22 @@ // borrowed from substack/resolve | ||
function resolve(id, parent, cb) { | ||
function resolve(id, opts, cb) { | ||
if (resv.isCore(id)) { | ||
// return path to browser capable version if we have it | ||
return cb(null, core[id]); | ||
// opts.filename | ||
// opts.paths | ||
// opts.modules | ||
// opts.packageFilter | ||
opts = opts || {}; | ||
var modules = opts.modules || {}; | ||
var shim_path = modules[id]; | ||
if (shim_path) { | ||
return cb(null, shim_path); | ||
} | ||
var base = path.dirname(parent.filename); | ||
var base = path.dirname(opts.filename); | ||
var paths = nodeModulesPaths(base); | ||
if (parent && parent.paths) { | ||
paths.push.apply(paths, parent.paths); | ||
if (opts.paths) { | ||
paths.push.apply(paths, opts.paths); | ||
} | ||
@@ -144,6 +136,6 @@ | ||
var full = resv(id, { | ||
paths: parent.paths, | ||
paths: opts.paths, | ||
basedir: base, | ||
packageFilter: function(info) { | ||
if (parent.packageFilter) info = parent.packageFilter(info); | ||
if (opts.packageFilter) info = opts.packageFilter(info); | ||
@@ -150,0 +142,0 @@ // no browser field, keep info unchanged |
{ | ||
"name": "browser-resolve", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "resolve which handles browser field support in package.json", | ||
@@ -20,9 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"resolve": "0.3.1", | ||
"console-browserify": "0.1.6", | ||
"vm-browserify": "0.0.1", | ||
"crypto-browserify": "0.2.1", | ||
"http-browserify": "0.1.11", | ||
"buffer-browserify": "0.0.5", | ||
"zlib-browserify": "0.0.1" | ||
"resolve": "0.3.1" | ||
}, | ||
@@ -29,0 +23,0 @@ "devDependencies": { |
@@ -5,6 +5,17 @@ # browser-resolve [![Build Status](https://travis-ci.org/shtylman/node-browser-resolve.png?branch=master)](https://travis-ci.org/shtylman/node-browser-resolve) | ||
# example | ||
## api | ||
## relative | ||
### resolve(pkg, opts={}, cb) | ||
Resolve a module path and call `cb(err, path)` | ||
Options: | ||
* filename - the calling filename where the require call originated (in the source) | ||
* paths - require.paths array to use if nothing is found on the normal node_modules recursive walk | ||
* packageFilter - transform the parsed package.json contents before looking at the "main" field | ||
* modules - object with module id/name -> path mappings to consult before doing manual resolution (use to provide core modules) | ||
## basic usage | ||
you can resolve files like `require.resolve()`: | ||
@@ -23,9 +34,13 @@ ``` js | ||
## core | ||
## core modules | ||
or if you `require()` core modules you'll get a version that works in browsers: | ||
By default, core modules (http, dgram, etc) will return their same name as the path. If you want to have specific paths returned, specify a `modules` property in the options object. | ||
``` js | ||
var shims = { | ||
http: '/your/path/to/http.js' | ||
}; | ||
var resolve = require('browser-resolve'); | ||
resolve('fs', null, function(err, path) { | ||
resolve('fs', { modules: shims }, function(err, path) { | ||
console.log(path); | ||
@@ -40,8 +55,5 @@ }); | ||
## custom | ||
## browser field | ||
browser-specific versions of modules | ||
and you can use the | ||
[browser field](https://gist.github.com/shtylman/4339901) to load | ||
browser-specific versions of modules: | ||
``` js | ||
@@ -119,1 +131,7 @@ { | ||
MIT | ||
# upgrade notes | ||
Prior to v1.x this library provided shims for node core modules. These have since been removed. If you want to have alternative core modules provided, use the `modules` option when calling resolve. | ||
This was done to allow package managers to choose which shims they want to use without browser-resolve being the central point of update. |
@@ -5,6 +5,10 @@ // test loading core modules | ||
test('events', function(done) { | ||
resolve('events', {}, function(err, path) { | ||
var shims = { | ||
events: 'foo' | ||
}; | ||
test('shim found', function(done) { | ||
resolve('events', { modules: shims }, function(err, path) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('../builtin/events')); | ||
assert.equal(path, 'foo'); | ||
done(); | ||
@@ -14,6 +18,6 @@ }); | ||
test('http', function(done) { | ||
test('core shim not found', function(done) { | ||
resolve('http', {}, function(err, path) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('http-browserify')); | ||
assert.equal(path, 'http'); | ||
done(); | ||
@@ -23,9 +27,1 @@ }); | ||
test('dgram', function(done) { | ||
resolve('dgram', {}, function(err, path) { | ||
assert.ifError(err); | ||
assert.equal(path, require.resolve('../builtin/dgram')); | ||
done(); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
1
134
1
1
14337
21
297
- Removedbuffer-browserify@0.0.5
- Removedconsole-browserify@0.1.6
- Removedcrypto-browserify@0.2.1
- Removedhttp-browserify@0.1.11
- Removedvm-browserify@0.0.1
- Removedzlib-browserify@0.0.1
- Removedbase64-js@0.0.2(transitive)
- Removedbuffer-browserify@0.0.5(transitive)
- Removedconcat-stream@0.0.8(transitive)
- Removedconsole-browserify@0.1.6(transitive)
- Removedcrypto-browserify@0.2.1(transitive)
- Removedhttp-browserify@0.1.11(transitive)
- Removedvm-browserify@0.0.1(transitive)
- Removedzlib-browserify@0.0.1(transitive)