- Notifications
You must be signed in to change notification settings - Fork68
resolve function which support the browser field in package.json
License
browserify/browser-resolve
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
node.js resolve algorithm withbrowser field support.
Resolve a module path and callcb(err, path [, pkg])
Options:
basedir
- directory to begin resolving frombrowser
- the 'browser' property to use from package.json (defaults to 'browser')filename
- the calling filename where therequire()
call originated (in the source)modules
- object with module id/name -> path mappings to consult before doing manual resolution (use to provide core modules)packageFilter
- transform the parsedpackage.json
contents before looking at themain
fieldpaths
-require.paths
array to use if nothing is found on the normalnode_modules
recursive walk
Additionally, options supported bynode-resolve can be used.
Same as the async resolve, just uses sync methods.
Additionally, options supported bynode-resolve can be used.
you can resolve files likerequire.resolve()
:
varbresolve=require('browser-resolve');bresolve('../',{filename:__filename},function(err,path){console.log(path);});
$ node example/resolve.js/home/substack/projects/browser-resolve/index.js
By default, core modules (http, dgram, etc) will return their same name as the path. If you want to have specific paths returned, specify amodules
property in the options object.
varshims={http:'/your/path/to/http.js'};varbresolve=require('browser-resolve');bresolve('http',{modules:shims},function(err,path){console.log(path);});
$ node example/builtin.js/home/substack/projects/browser-resolve/builtin/http.js
browser-specific versions of modules
{"name":"custom","version":"0.0.0","browser": {"./main.js":"custom.js" }}
varbresolve=require('browser-resolve');varparent={filename:__dirname+'/custom/file.js'};bresolve('./main.js',parent,function(err,path){console.log(path);});
$ node example/custom.js/home/substack/projects/browser-resolve/example/custom/custom.js
You can use different package.json properties for the resolution, if you want to allow packages to target different environments for example:
{"browser": {"./main.js":"custom.js" },"chromeapp": {"./main.js":"custom-chromeapp.js" }}
varbresolve=require('browser-resolve');varparent={filename:__dirname+'/custom/file.js',browser:'chromeapp'};bresolve('./main.js',parent,function(err,path){console.log(path);});
$ node example/custom.js/home/substack/projects/browser-resolve/example/custom/custom-chromeapp.js
You can skip over dependencies by setting abrowser fieldvalue tofalse
:
{"name":"skip","version":"0.0.0","browser": {"tar":false }}
This is handy if you have code like:
vartar=require('tar');exports.add=function(a,b){returna+b;};exports.parse=function(){returntar.Parse();};
so thatrequire('tar')
will just return{}
in the browser because you don'tintend to support the.parse()
export in a browser environment.
varbresolve=require('browser-resolve');varparent={filename:__dirname+'/skip/main.js'};bresolve('tar',parent,function(err,path){console.log(path);});
$ node example/skip.js/home/substack/projects/browser-resolve/empty.js
MIT
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 themodules
option when callingbresolve()
.
This was done to allow package managers to choose which shims they want to use without browser-resolve being the central point of update.
About
resolve function which support the browser field in package.json