Skip to content

Commit 074eafa

Browse files
authored
Merge pull request #183 from webpack/next
Readme and bugfix
2 parents 2091c42 + 358cf00 commit 074eafa

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ npm install enhanced-resolve
1919
yarn add enhanced-resolve
2020
```
2121

22+
### Resolve
23+
24+
There is a Node.js API which allows to resolve requests according to the Node.js resolving rules.
25+
Sync and async APIs are offered. A `create` method allows to create a custom resolve function.
26+
27+
```js
28+
const resolve = require("enhanced-resolve");
29+
30+
resolve("/some/path/to/folder", "module/dir", (err, result) => {
31+
result; // === "/some/path/node_modules/module/dir/index.js"
32+
});
33+
34+
resolve.sync("/some/path/to/folder", "../../dir");
35+
// === "/some/path/dir/index.js"
36+
37+
const myResolve = resolve.create({
38+
// or resolve.create.sync
39+
extensions: [".ts", ".js"]
40+
// see more options below
41+
});
42+
43+
myResolve("/some/path/to/folder", "ts-module", (err, result) => {
44+
result; // === "/some/node_modules/ts-module/index.ts"
45+
});
46+
```
47+
2248
### Creating a Resolver
2349

2450
The easiest way to create a resolver is to use the `createResolver` function on `ResolveFactory`, along with one of the supplied File System implementations.
@@ -48,8 +74,6 @@ myResolver.resolve({}, lookupStartPath, request, resolveContext, (
4874
});
4975
```
5076

51-
For more examples creating different types resolvers (sync/async, context, etc) see `lib/node.js`.
52-
5377
#### Resolver Options
5478

5579
| Field | Default | Description |
@@ -73,7 +97,8 @@ For more examples creating different types resolvers (sync/async, context, etc)
7397

7498
## Plugins
7599

76-
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.
100+
Similar to `webpack`, the core of `enhanced-resolve` functionality is implemented as individual plugins that are executed using [`tapable`](https://github.com/webpack/tapable).
101+
These plugins can extend the functionality of the library, adding other ways for files/contexts to be resolved.
77102

78103
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.
79104

@@ -114,15 +139,15 @@ If you are using `webpack`, and you want to pass custom options to `enhanced-res
114139

115140
```
116141
resolve: {
117-
extensions: ['', '.js', '.jsx'],
118-
modules: ['src', 'node_modules'],
142+
extensions: ['.js', '.jsx'],
143+
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
119144
plugins: [new DirectoryNamedWebpackPlugin()]
120145
...
121146
},
122147
```
123148

124149
## License
125150

126-
Copyright (c) 2012-2016 Tobias Koppers
151+
Copyright (c) 2012-2019 JS Foundation and other contributors
127152

128153
MIT (http://www.opensource.org/licenses/mit-license.php)

lib/MainFieldPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ module.exports = class MainFieldPlugin {
4242
mainModule = content[field];
4343
}
4444
}
45-
if (!mainModule) return callback();
45+
if (!mainModule || mainModule === "." || mainModule === "./") {
46+
return callback();
47+
}
4648
if (this.options.forceRelative && !/^\.\.?\//.test(mainModule))
4749
mainModule = "./" + mainModule;
4850
const obj = {

test/fixtures/main-field-self2/index.js

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "."
3+
}

test/resolve.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,11 @@ describe("resolve", function() {
188188
"./main-field-self",
189189
path.join(fixtures, "main-field-self", "index.js")
190190
);
191+
192+
testResolve(
193+
"don't crash on main field pointing to self",
194+
fixtures,
195+
"./main-field-self2",
196+
path.join(fixtures, "main-field-self2", "index.js")
197+
);
191198
});

0 commit comments

Comments
 (0)