5402f42af0b82ae6255d9383f4904990419e9f0ae0cdb442e88bbd7a0c422a8e5f21f99b5efac12b2d3287141a90063e0d047d947f120fa381b6493eeed9a4 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # enhanced-resolve
  2. Offers an async require.resolve function. It's highly configurable.
  3. ## Features
  4. * plugin system
  5. * provide a custom filesystem
  6. * sync and async node.js filesystems included
  7. ## Getting Started
  8. ### Install
  9. ```sh
  10. # npm
  11. npm install enhanced-resolve
  12. # or Yarn
  13. yarn add enhanced-resolve
  14. ```
  15. ### Creating a Resolver
  16. The easiest way to create a resolver is to use the `createResolver` function on `ResolveFactory`, along with one of the supplied File System implementations.
  17. ```js
  18. const {
  19. NodeJsInputFileSystem,
  20. CachedInputFileSystem,
  21. ResolverFactory
  22. } = require('enhanced-resolve');
  23. // create a resolver
  24. const myResolver = ResolverFactory.createResolver({
  25. // Typical usage will consume the `NodeJsInputFileSystem` + `CachedInputFileSystem`, which wraps the Node.js `fs` wrapper to add resilience + caching.
  26. fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
  27. extensions: ['.js', '.json']
  28. /* any other resolver options here. Options/defaults can be seen below */
  29. });
  30. // resolve a file with the new resolver
  31. const context = {};
  32. const resolveContext = {};
  33. const lookupStartPath = '/Users/webpack/some/root/dir';
  34. const request = './path/to-look-up.js';
  35. myResolver.resolve({}, lookupStartPath, request, resolveContext, (err/*Error*/, filepath/*string*/) => {
  36. // Do something with the path
  37. });
  38. ```
  39. For more examples creating different types resolvers (sync/async, context, etc) see `lib/node.js`.
  40. #### Resolver Options
  41. | Field | Default | Description |
  42. | ------------------------ | --------------------------- | ---------------------------------------------------------------------------------- |
  43. | alias | [] | A list of module alias configurations or an object which maps key to value |
  44. | aliasFields | [] | A list of alias fields in description files |
  45. | cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
  46. | descriptionFiles | ["package.json"] | A list of description files to read from |
  47. | enforceExtension | false | Enforce that a extension from extensions must be used |
  48. | enforceModuleExtension | false | Enforce that a extension from moduleExtensions must be used |
  49. | extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
  50. | mainFields | ["main"] | A list of main fields in description files |
  51. | mainFiles | ["index"] | A list of main files in directories |
  52. | modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
  53. | roots | [] | A list of directories to resolve request starting with `/` from |
  54. | ignoreRootsErrors | false | Ignore fatal errors happening during handling of `roots` (allows to add `roots` without a breaking change) |
  55. | preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
  56. | unsafeCache | false | Use this cache object to unsafely cache the successful requests |
  57. | plugins | [] | A list of additional resolve plugins which should be applied |
  58. | symlinks | true | Whether to resolve symlinks to their symlinked location |
  59. | cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
  60. | moduleExtensions | [] | A list of module extensions which should be tried for modules |
  61. | resolveToContext | false | Resolve to a context instead of a file |
  62. | restrictions | [] | A list of resolve restrictions |
  63. | fileSystem | | The file system which should be used |
  64. | resolver | undefined | A prepared Resolver to which the plugins are attached |
  65. ## Plugins
  66. Similar to `webpack`, the core of `enhanced-resolve` functionality is implemented as individual plugins that are executed using [`Tapable`](https://github.com/webpack/tapable). These plugins can extend the functionality of the library, adding other ways for files/contexts to be resolved.
  67. A plugin should be a `class` (or its ES5 equivalent) with an `apply` method. The `apply` method will receive a `resolver` instance, that can be used to hook in to the event system.
  68. ### Plugin Boilerplate
  69. ```js
  70. class MyResolverPlugin {
  71. constructor(source, target) {
  72. this.source = source;
  73. this.target = target;
  74. }
  75. apply(resolver) {
  76. const target = resolver.ensureHook(this.target);
  77. resolver.getHook(this.source).tapAsync("MyResolverPlugin", (request, resolveContext, callback) => {
  78. // Any logic you need to create a new `request` can go here
  79. resolver.doResolve(target, request, null, resolveContext, callback);
  80. });
  81. }
  82. }
  83. ```
  84. Plugins are executed in a pipeline, and register which event they should be executed before/after. In the example above, `source` is the name of the event that starts the pipeline, and `target` is what event this plugin should fire, which is what continues the execution of the pipeline. For an example of how these different plugin events create a chain, see `lib/ResolverFactory.js`, in the `//// pipeline ////` section.
  85. ## Tests
  86. ``` javascript
  87. npm test
  88. ```
  89. [![Build Status](https://secure.travis-ci.org/webpack/enhanced-resolve.png?branch=master)](http://travis-ci.org/webpack/enhanced-resolve)
  90. ## Passing options from webpack
  91. If you are using `webpack`, and you want to pass custom options to `enhanced-resolve`, the options are passed from the `resolve` key of your webpack configuration e.g.:
  92. ```
  93. resolve: {
  94. extensions: ['', '.js', '.jsx'],
  95. modules: ['src', 'node_modules'],
  96. plugins: [new DirectoryNamedWebpackPlugin()]
  97. ...
  98. },
  99. ```
  100. ## License
  101. Copyright (c) 2012-2016 Tobias Koppers
  102. MIT (http://www.opensource.org/licenses/mit-license.php)