Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browser-resolve

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-resolve - npm Package Compare versions

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();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc