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.

121 lines
5.8 KiB

4 years ago
  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. | unsafeCache | false | Use this cache object to unsafely cache the successful requests |
  54. | plugins | [] | A list of additional resolve plugins which should be applied |
  55. | symlinks | true | Whether to resolve symlinks to their symlinked location |
  56. | 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. |
  57. | moduleExtensions | [] | A list of module extensions which should be tried for modules |
  58. | resolveToContext | false | Resolve to a context instead of a file |
  59. | fileSystem | | The file system which should be used |
  60. | resolver | undefined | A prepared Resolver to which the plugins are attached |
  61. ## Plugins
  62. 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.
  63. 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.
  64. ### Plugin Boilerplate
  65. ```js
  66. class MyResolverPlugin {
  67. constructor(source, target) {
  68. this.source = source;
  69. this.target = target;
  70. }
  71. apply(resolver) {
  72. const target = resolver.ensureHook(this.target);
  73. resolver.getHook(this.source).tapAsync("MyResolverPlugin", (request, resolveContext, callback) => {
  74. // Any logic you need to create a new `request` can go here
  75. resolver.doResolve(target, request, null, resolveContext, callback);
  76. });
  77. }
  78. }
  79. ```
  80. 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.
  81. ## Tests
  82. ``` javascript
  83. npm test
  84. ```
  85. [![Build Status](https://secure.travis-ci.org/webpack/enhanced-resolve.png?branch=master)](http://travis-ci.org/webpack/enhanced-resolve)
  86. ## Passing options from webpack
  87. 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.:
  88. ```
  89. resolve: {
  90. extensions: ['', '.js', '.jsx'],
  91. modules: ['src', 'node_modules'],
  92. plugins: [new DirectoryNamedWebpackPlugin()]
  93. ...
  94. },
  95. ```
  96. ## License
  97. Copyright (c) 2012-2016 Tobias Koppers
  98. MIT (http://www.opensource.org/licenses/mit-license.php)