source: trip-planner-front/node_modules/enhanced-resolve/README.md@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.4 KB
Line 
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## Getting Started
12
13### Install
14
15```sh
16# npm
17npm install enhanced-resolve
18# or Yarn
19yarn add enhanced-resolve
20```
21
22### Resolve
23
24There is a Node.js API which allows to resolve requests according to the Node.js resolving rules.
25Sync and async APIs are offered. A `create` method allows to create a custom resolve function.
26
27```js
28const resolve = require("enhanced-resolve");
29
30resolve("/some/path/to/folder", "module/dir", (err, result) => {
31 result; // === "/some/path/node_modules/module/dir/index.js"
32});
33
34resolve.sync("/some/path/to/folder", "../../dir");
35// === "/some/path/dir/index.js"
36
37const myResolve = resolve.create({
38 // or resolve.create.sync
39 extensions: [".ts", ".js"]
40 // see more options below
41});
42
43myResolve("/some/path/to/folder", "ts-module", (err, result) => {
44 result; // === "/some/node_modules/ts-module/index.ts"
45});
46```
47
48### Creating a Resolver
49
50The easiest way to create a resolver is to use the `createResolver` function on `ResolveFactory`, along with one of the supplied File System implementations.
51
52```js
53const fs = require("fs");
54const { CachedInputFileSystem, ResolverFactory } = require("enhanced-resolve");
55
56// create a resolver
57const myResolver = ResolverFactory.createResolver({
58 // Typical usage will consume the `fs` + `CachedInputFileSystem`, which wraps Node.js `fs` to add caching.
59 fileSystem: new CachedInputFileSystem(fs, 4000),
60 extensions: [".js", ".json"]
61 /* any other resolver options here. Options/defaults can be seen below */
62});
63
64// resolve a file with the new resolver
65const context = {};
66const resolveContext = {};
67const lookupStartPath = "/Users/webpack/some/root/dir";
68const request = "./path/to-look-up.js";
69myResolver.resolve({}, lookupStartPath, request, resolveContext, (
70 err /*Error*/,
71 filepath /*string*/
72) => {
73 // Do something with the path
74});
75```
76
77#### Resolver Options
78
79| Field | Default | Description |
80| ---------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
81| alias | [] | A list of module alias configurations or an object which maps key to value |
82| aliasFields | [] | A list of alias fields in description files |
83| 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. |
84| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
85| conditionNames | ["node"] | A list of exports field condition names |
86| descriptionFiles | ["package.json"] | A list of description files to read from |
87| enforceExtension | false | Enforce that a extension from extensions must be used |
88| exportsFields | ["exports"] | A list of exports fields in description files |
89| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
90| fileSystem | | The file system which should be used |
91| fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
92| mainFields | ["main"] | A list of main fields in description files |
93| mainFiles | ["index"] | A list of main files in directories |
94| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
95| plugins | [] | A list of additional resolve plugins which should be applied |
96| resolver | undefined | A prepared Resolver to which the plugins are attached |
97| resolveToContext | false | Resolve to a context instead of a file |
98| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
99| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
100| restrictions | [] | A list of resolve restrictions |
101| roots | [] | A list of root paths |
102| symlinks | true | Whether to resolve symlinks to their symlinked location |
103| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
104
105## Plugins
106
107Similar to `webpack`, the core of `enhanced-resolve` functionality is implemented as individual plugins that are executed using [`tapable`](https://github.com/webpack/tapable).
108These plugins can extend the functionality of the library, adding other ways for files/contexts to be resolved.
109
110A 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.
111
112### Plugin Boilerplate
113
114```js
115class MyResolverPlugin {
116 constructor(source, target) {
117 this.source = source;
118 this.target = target;
119 }
120
121 apply(resolver) {
122 const target = resolver.ensureHook(this.target);
123 resolver
124 .getHook(this.source)
125 .tapAsync("MyResolverPlugin", (request, resolveContext, callback) => {
126 // Any logic you need to create a new `request` can go here
127 resolver.doResolve(target, request, null, resolveContext, callback);
128 });
129 }
130}
131```
132
133Plugins 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.
134
135## Escaping
136
137It's allowed to escape `#` as `\0#` to avoid parsing it as fragment.
138
139enhanced-resolve will try to resolve requests containing `#` as path and as fragment, so it will automatically figure out if `./some#thing` means `.../some.js#thing` or `.../some#thing.js`. When a `#` is resolved as path it will be escaped in the result. Here: `.../some\0#thing.js`.
140
141## Tests
142
143```javascript
144npm test
145```
146
147[![Build Status](https://secure.travis-ci.org/webpack/enhanced-resolve.png?branch=main)](http://travis-ci.org/webpack/enhanced-resolve)
148
149## Passing options from webpack
150
151If 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.:
152
153```
154resolve: {
155 extensions: ['.js', '.jsx'],
156 modules: [path.resolve(__dirname, 'src'), 'node_modules'],
157 plugins: [new DirectoryNamedWebpackPlugin()]
158 ...
159},
160```
161
162## License
163
164Copyright (c) 2012-2019 JS Foundation and other contributors
165
166MIT (http://www.opensource.org/licenses/mit-license.php)
Note: See TracBrowser for help on using the repository browser.