Socket
Socket
Sign inDemoInstall

browser-resolve

Package Overview
Dependencies
Maintainers
2
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 1.3.2 to 1.4.0

test/core-sync.js

223

index.js

@@ -28,2 +28,54 @@ // builtin

function find_shims_in_package(pkgJson, cur_path, shims) {
try {
var info = JSON.parse(pkgJson);
}
catch (err) {
err.message = pkg_path + ' : ' + err.message
throw err;
}
// support legacy browserify field for easier migration from legacy
// many packages used this field historically
if (typeof info.browserify === 'string' && !info.browser) {
info.browser = info.browserify;
}
// no replacements, skip shims
if (!info.browser) {
return;
}
// if browser field is a string
// then it just replaces the main entry point
if (typeof info.browser === 'string') {
var key = path.resolve(cur_path, info.main || 'index.js');
shims[key] = path.resolve(cur_path, info.browser);
return;
}
// http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
Object.keys(info.browser).forEach(function(key) {
if (info.browser[key] === false) {
return shims[key] = __dirname + '/empty.js';
}
var val = info.browser[key];
// if target is a relative path, then resolve
// otherwise we assume target is a module
if (val[0] === '.') {
val = path.resolve(cur_path, val);
}
// if does not begin with / ../ or ./ then it is a module
if (key[0] !== '/' && key[0] !== '.') {
return shims[key] = val;
}
key = path.resolve(cur_path, key);
shims[key] = val;
});
}
// paths is mutated

@@ -54,13 +106,52 @@ // load shims from first package.json file found

}
try {
var info = JSON.parse(data);
find_shims_in_package(data, cur_path, shims);
return cb(null, shims);
}
catch (err) {
err.message = pkg_path + ' : ' + err.message
return cb(err);
}
});
})();
};
// support legacy browserify field for easier migration from legacy
// many packages used this field historically
// paths is mutated
// synchronously load shims from first package.json file found
function load_shims_sync(paths) {
// identify if our file should be replaced per the browser field
// original filename|id -> replacement
var shims = {};
var cur_path;
while (cur_path = paths.shift()) {
var pkg_path = path.join(cur_path, 'package.json');
try {
var data = fs.readFileSync(pkg_path, 'utf8');
find_shims_in_package(data, cur_path, shims);
return shims;
}
catch (err) {
// ignore paths we can't open
// avoids an exists check
if (err.code === 'ENOENT') {
continue;
}
throw err;
}
}
return shims;
}
function build_resolve_opts(opts, base) {
return {
paths: opts.paths,
extensions: opts.extensions,
basedir: base,
package: opts.package,
packageFilter: function (info, pkgdir) {
if (opts.packageFilter) info = opts.packageFilter(info, pkgdir);
// support legacy browserify field
if (typeof info.browserify === 'string' && !info.browser) {

@@ -70,42 +161,22 @@ info.browser = info.browserify;

// no replacements, skip shims
// no browser field, keep info unchanged
if (!info.browser) {
return cb(null, shims);
return info;
}
// if browser field is a string
// then it just replaces the main entry point
// replace main
if (typeof info.browser === 'string') {
var key = path.resolve(cur_path, info.main || 'index.js');
shims[key] = path.resolve(cur_path, info.browser);
return cb(null, shims);
info.main = info.browser;
return info;
}
// http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
Object.keys(info.browser).forEach(function(key) {
if (info.browser[key] === false) {
return shims[key] = __dirname + '/empty.js';
}
var replace_main = info.browser[info.main || './index.js'] ||
info.browser['./' + info.main || './index.js'];
var val = info.browser[key];
info.main = replace_main || info.main;
return info;
}
};
}
// if target is a relative path, then resolve
// otherwise we assume target is a module
if (val[0] === '.') {
val = path.resolve(cur_path, val);
}
// if does not begin with / ../ or ./ then it is a module
if (key[0] !== '/' && key[0] !== '.') {
return shims[key] = val;
}
var key = path.resolve(cur_path, key);
shims[key] = val;
});
return cb(null, shims);
});
})();
};
function resolve(id, opts, cb) {

@@ -155,33 +226,3 @@

// if browser field is an object tho?
var full = resv(id, {
paths: opts.paths,
extensions: opts.extensions,
basedir: base,
package: opts.package,
packageFilter: function(info, pkgdir) {
if (opts.packageFilter) info = opts.packageFilter(info, pkgdir);
// support legacy browserify field
if (typeof info.browserify === 'string' && !info.browser) {
info.browser = info.browserify;
}
// no browser field, keep info unchanged
if (!info.browser) {
return info;
}
// replace main
if (typeof info.browser === 'string') {
info.main = info.browser;
return info;
}
var replace_main = info.browser[info.main || './index.js'] ||
info.browser['./' + info.main || './index.js'];
info.main = replace_main || info.main;
return info;
}
}, function(err, full, pkg) {
var full = resv(id, build_resolve_opts(opts, base), function(err, full, pkg) {
if (err) {

@@ -197,3 +238,49 @@ return cb(err);

resolve.sync = function (id, opts) {
// opts.filename
// opts.paths
// opts.modules
// opts.packageFilter
opts = opts || {};
var base = path.dirname(opts.filename);
var paths = nodeModulesPaths(base);
if (opts.paths) {
paths.push.apply(paths, opts.paths);
}
paths = paths.map(function(p) {
return path.dirname(p);
});
// we must always load shims because the browser field could shim out a module
var shims = load_shims_sync(paths);
if (shims[id]) {
// if the shim was is an absolute path, it was fully resolved
if (shims[id][0] === '/') {
return shims[id];
}
// module -> alt-module shims
id = shims[id];
}
var modules = opts.modules || {};
var shim_path = modules[id];
if (shim_path) {
return shim_path;
}
// our browser field resolver
// if browser field is an object tho?
var full = resv.sync(id, build_resolve_opts(opts, base));
return (shims) ? shims[full] || full : full;
};
module.exports = resolve;
{
"name": "browser-resolve",
"version": "1.3.2",
"version": "1.4.0",
"description": "resolve which handles browser field support in package.json",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -18,2 +18,6 @@ # browser-resolve [![Build Status](https://travis-ci.org/defunctzombie/node-browser-resolve.png?branch=master)](https://travis-ci.org/defunctzombie/node-browser-resolve)

### resolve.sync(pkg, opts={})
Same as the async resolve, just uses sync methods.
## basic usage

@@ -20,0 +24,0 @@

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