GithubHelp home page GithubHelp logo

matthewp / node-browser-resolve Goto Github PK

View Code? Open in Web Editor NEW

This project forked from browserify/browser-resolve

0.0 3.0 0.0 174 KB

resolve function which support the browser field in package.json

JavaScript 99.96% CoffeeScript 0.04%

node-browser-resolve's Introduction

browser-resolve Build Status

node.js resolve algorithm with browser field support.

api

resolve(id, opts={}, cb)

Resolve a module path and call cb(err, path [, pkg])

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)
  • browser - the 'browser' property to use from package.json (defaults to 'browser')

resolve.sync(id, opts={})

Same as the async resolve, just uses sync methods.

basic usage

you can resolve files like require.resolve():

var resolve = require('browser-resolve');
resolve('../', { filename: __filename }, function(err, path) {
    console.log(path);
});
$ node example/resolve.js
/home/substack/projects/node-browser-resolve/index.js

core modules

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.

var shims = {
    http: '/your/path/to/http.js'
};

var resolve = require('browser-resolve');
resolve('fs', { modules: shims }, function(err, path) {
    console.log(path);
});
$ node example/builtin.js
/home/substack/projects/node-browser-resolve/builtin/fs.js

browser field

browser-specific versions of modules

{
  "name": "custom",
  "version": "0.0.0",
  "browser": {
    "./main.js": "custom.js"
  },
  "chromeapp": {
    "./main.js": "custom-chromeapp.js"
  }
}
var resolve = require('browser-resolve');
var parent = { filename: __dirname + '/custom/file.js' /*, browser: 'chromeapp' */ };
resolve('./main.js', parent, function(err, path) {
    console.log(path);
});
$ node example/custom.js
/home/substack/projects/node-browser-resolve/example/custom/custom.js

skip

You can skip over dependencies by setting a browser field value to false:

{
  "name": "skip",
  "version": "0.0.0",
  "browser": {
    "tar": false
  }
}

This is handy if you have code like:

var tar = require('tar');

exports.add = function (a, b) {
    return a + b;
};

exports.parse = function () {
    return tar.Parse();
};

so that require('tar') will just return {} in the browser because you don't intend to support the .parse() export in a browser environment.

var resolve = require('browser-resolve');
var parent = { filename: __dirname + '/skip/main.js' };
resolve('tar', parent, function(err, path) {
    console.log(path);
});
$ node example/skip.js
/home/substack/projects/node-browser-resolve/empty.js

license

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.

node-browser-resolve's People

Contributors

airportyh avatar andreypopp avatar defunctzombie avatar f5io avatar forbeslindesay avatar jameskyburz avatar jaredhanson avatar jmm avatar juliangruber avatar justinbmeyer avatar kriskowal avatar mvayngrib avatar roncli avatar securingsincity avatar thlorenz avatar timoxley avatar tonistiigi avatar tootallnate avatar wilsonjackson avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.