UNPKG

5.97 kBMarkdownView Raw
1# enhanced-resolve
2
3Offers an async require.resolve function. It's highly configurable.
4
5## Features
6
7* plugin system
8* provide a custom filesystem
9* sync and async node.js filesystems included
10
11
12## Getting Started
13### Install
14```sh
15# npm
16npm install enhanced-resolve
17# or Yarn
18yarn add enhanced-resolve
19```
20
21### Creating a Resolver
22The easiest way to create a resolver is to use the `createResolver` function on `ResolveFactory`, along with one of the supplied File System implementations.
23```js
24const {
25 NodeJsInputFileSystem,
26 CachedInputFileSystem,
27 ResolverFactory
28} = require('enhanced-resolve');
29
30// create a resolver
31const myResolver = ResolverFactory.createResolver({
32 // Typical usage will consume the `NodeJsInputFileSystem` + `CachedInputFileSystem`, which wraps the Node.js `fs` wrapper to add resilience + caching.
33 fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
34 extensions: ['.js', '.json']
35 /* any other resolver options here. Options/defaults can be seen below */
36});
37
38// resolve a file with the new resolver
39const context = {};
40const resolveContext = {};
41const lookupStartPath = '/Users/webpack/some/root/dir';
42const request = './path/to-look-up.js';
43myResolver.resolve({}, lookupStartPath, request, resolveContext, (err/*Error*/, filepath/*string*/) => {
44 // Do something with the path
45});
46```
47
48For more examples creating different types resolvers (sync/async, context, etc) see `lib/node.js`.
49#### Resolver Options
50| Field | Default | Description |
51| ------------------------ | --------------------------- | ---------------------------------------------------------------------------------- |
52| alias | [] | A list of module alias configurations or an object which maps key to value |
53| aliasFields | [] | A list of alias fields in description files |
54| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
55| descriptionFiles | ["package.json"] | A list of description files to read from |
56| enforceExtension | false | Enforce that a extension from extensions must be used |
57| enforceModuleExtension | false | Enforce that a extension from moduleExtensions must be used |
58| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
59| mainFields | ["main"] | A list of main fields in description files |
60| mainFiles | ["index"] | A list of main files in directories |
61| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
62| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
63| plugins | [] | A list of additional resolve plugins which should be applied |
64| symlinks | false | Whether to resolve symlinks to their symlinked location |
65| 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. |
66| moduleExtensions | [] | A list of module extensions which should be tried for modules |
67| resolveToContext | false | Resolve to a context instead of a file |
68| fileSystem | | The file system which should be used |
69| resolver | undefined | A prepared Resolver to which the plugins are attached |
70
71## Plugins
72Similar 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.
73
74A 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.
75
76### Plugin Boilerplate
77```js
78class MyResolverPlugin {
79 constructor(source, target) {
80 this.source = source;
81 this.target = target;
82 }
83
84 apply(resolver) {
85 resolver.plugin(this.source, (request, callback) => {
86 // Any logic you need to create a new `request` can go here
87 resolver.doResolve(this.target, request, null, callback);
88 });
89 }
90}
91```
92
93Plugins 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.
94
95## Tests
96
97``` javascript
98npm test
99```
100
101[![Build Status](https://secure.travis-ci.org/webpack/enhanced-resolve.png?branch=master)](http://travis-ci.org/webpack/enhanced-resolve)
102
103
104## Passing options from webpack
105If 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.:
106
107```
108resolve: {
109 extensions: ['', '.js', '.jsx'],
110 modules: ['src', 'node_modules'],
111 plugins: [new DirectoryNamedWebpackPlugin()]
112 ...
113},
114```
115
116## License
117
118Copyright (c) 2012-2016 Tobias Koppers
119
120MIT (http://www.opensource.org/licenses/mit-license.php)