You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # browser-resolve [![Build Status](https://travis-ci.org/defunctzombie/node-browser-resolve.png?branch=master)](https://travis-ci.org/defunctzombie/node-browser-resolve)
  2. node.js resolve algorithm with [browser field](https://github.com/defunctzombie/package-browser-field-spec) support.
  3. ## api
  4. ### resolve(id, opts={}, cb)
  5. Resolve a module path and call `cb(err, path [, pkg])`
  6. Options:
  7. * `basedir` - directory to begin resolving from
  8. * `browser` - the 'browser' property to use from package.json (defaults to 'browser')
  9. * `filename` - the calling filename where the `require()` call originated (in the source)
  10. * `modules` - object with module id/name -> path mappings to consult before doing manual resolution (use to provide core modules)
  11. * `packageFilter` - transform the parsed `package.json` contents before looking at the `main` field
  12. * `paths` - `require.paths` array to use if nothing is found on the normal `node_modules` recursive walk
  13. Options supported by [node-resolve](https://github.com/substack/node-resolve#resolveid-opts-cb) can be used.
  14. ### resolve.sync(id, opts={})
  15. Same as the async resolve, just uses sync methods.
  16. Options supported by [node-resolve](https://github.com/substack/node-resolve#resolvesyncid-opts) `sync` can be used.
  17. ## basic usage
  18. you can resolve files like `require.resolve()`:
  19. ``` js
  20. var resolve = require('browser-resolve');
  21. resolve('../', { filename: __filename }, function(err, path) {
  22. console.log(path);
  23. });
  24. ```
  25. ```
  26. $ node example/resolve.js
  27. /home/substack/projects/node-browser-resolve/index.js
  28. ```
  29. ## core modules
  30. 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.
  31. ``` js
  32. var shims = {
  33. http: '/your/path/to/http.js'
  34. };
  35. var resolve = require('browser-resolve');
  36. resolve('fs', { modules: shims }, function(err, path) {
  37. console.log(path);
  38. });
  39. ```
  40. ```
  41. $ node example/builtin.js
  42. /home/substack/projects/node-browser-resolve/builtin/fs.js
  43. ```
  44. ## browser field
  45. browser-specific versions of modules
  46. ``` js
  47. {
  48. "name": "custom",
  49. "version": "0.0.0",
  50. "browser": {
  51. "./main.js": "custom.js"
  52. },
  53. "chromeapp": {
  54. "./main.js": "custom-chromeapp.js"
  55. }
  56. }
  57. ```
  58. ``` js
  59. var resolve = require('browser-resolve');
  60. var parent = { filename: __dirname + '/custom/file.js' /*, browser: 'chromeapp' */ };
  61. resolve('./main.js', parent, function(err, path) {
  62. console.log(path);
  63. });
  64. ```
  65. ```
  66. $ node example/custom.js
  67. /home/substack/projects/node-browser-resolve/example/custom/custom.js
  68. ```
  69. ## skip
  70. You can skip over dependencies by setting a
  71. [browser field](https://gist.github.com/defunctzombie/4339901)
  72. value to `false`:
  73. ``` json
  74. {
  75. "name": "skip",
  76. "version": "0.0.0",
  77. "browser": {
  78. "tar": false
  79. }
  80. }
  81. ```
  82. This is handy if you have code like:
  83. ``` js
  84. var tar = require('tar');
  85. exports.add = function (a, b) {
  86. return a + b;
  87. };
  88. exports.parse = function () {
  89. return tar.Parse();
  90. };
  91. ```
  92. so that `require('tar')` will just return `{}` in the browser because you don't
  93. intend to support the `.parse()` export in a browser environment.
  94. ``` js
  95. var resolve = require('browser-resolve');
  96. var parent = { filename: __dirname + '/skip/main.js' };
  97. resolve('tar', parent, function(err, path) {
  98. console.log(path);
  99. });
  100. ```
  101. ```
  102. $ node example/skip.js
  103. /home/substack/projects/node-browser-resolve/empty.js
  104. ```
  105. # license
  106. MIT
  107. # upgrade notes
  108. 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.
  109. This was done to allow package managers to choose which shims they want to use without browser-resolve being the central point of update.