Skip to content

Commit 4ea6f0a

Browse files
authored
Merge pull request #388 from webpack/improve-resolve-docs
Improve resolve docs
2 parents aec2199 + 758a44a commit 4ea6f0a

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

content/configuration/resolve.md

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ contributors:
66
- skipjack
77
- SpaceK33z
88
- pksjce
9+
- sebastiandeutsch
910
---
1011

1112
These options change how modules are resolved. webpack provides reasonable defaults, but it is possible to change the resolving in detail.
@@ -57,6 +58,8 @@ import Test1 from 'xyz'; // Success, file.js is resolved and imported
5758
import Test2 from 'xyz/file.js'; // Error, /path/to/file.js/file.js is invalid
5859
```
5960

61+
The following table explains a lot more cases:
62+
6063
| `alias:` | `import "xyz"` | `import "xyz/file.js"` |
6164
| -------- | ---------------- | -------------------------|
6265
| `{}` | `/abc/node_modules/xyz/index.js` | `/abc/node_modules/xyz/file.js` |
@@ -94,27 +97,33 @@ aliasFields: ["browser"]
9497

9598
`array`
9699

97-
Default: `["package.json"]`
100+
The JSON files to use for descriptions. Default:
98101

99-
The JSON files to use for descriptions.
102+
```js
103+
descriptionFiles: ["package.json"]
104+
```
100105

101106

102107
## `resolve.enforceExtension`
103108

104-
`bool`
109+
`boolean`
105110

106-
Default: `false`
111+
If `true`, it will not allow extension-less files. So by default `require('./foo')` works if `./foo` has a `.js` extension, but with this enabled only `require('./foo.js')` will work. Default:
107112

108-
If false it will also try to use no extension from above.
113+
```js
114+
enforceExtension: false
115+
```
109116

110117

111118
## `resolve.enforceModuleExtension`
112119

113-
`bool`
120+
`boolean`
114121

115-
Default: `false`
122+
Whether to require to use an extension for modules (e.g. loaders). Default:
116123

117-
If false it's also try to use no module extension from above.
124+
```js
125+
enforceModuleExtension: false
126+
```
118127

119128

120129
## `resolve.extensions`
@@ -124,7 +133,7 @@ If false it's also try to use no module extension from above.
124133
Automatically resolve certain extensions. This defaults to:
125134

126135
```js
127-
extensions: [ ".js", ".json" ]
136+
extensions: [".js", ".json"]
128137
```
129138

130139
which is what enables users to leave off the extension when importing:
@@ -174,9 +183,11 @@ This means that when we `import * as D3 from "d3"` this will really resolve to t
174183

175184
`array`
176185

177-
Default: `["index"]`
186+
The filename to be used while resolving directories. Default:
178187

179-
The filename to be used while resolving directories.
188+
```js
189+
mainFiles: ["index"]
190+
```
180191

181192

182193
## `resolve.modules`
@@ -206,18 +217,26 @@ modules: [path.resolve(__dirname, "src"), "node_modules"]
206217

207218
## `resolve.resolveToContext`
208219

209-
`bool`
220+
`boolean`
210221

211-
Default: `false`
222+
If `true`, trying to resolve a context to its absolute path ends when a directory is found. Default:
212223

213-
If true, trying to resolve a context to its absolute path ends when a directory is found.
224+
```js
225+
resolveToContext: false
226+
```
214227

215228

216229
## `resolve.unsafeCache`
217230

218231
`regex` `array` `boolean`
219232

220-
Enable aggressive, but **unsafe**, caching of modules. Passing `true` will cache everything. A regular expression, or an array of regular expressions, can be used to test file paths and only cache certain modules. For example, to only cache utilities:
233+
Enable aggressive, but **unsafe**, caching of modules. Passing `true` will cache everything. Default:
234+
235+
```js
236+
unsafeCache: true
237+
```
238+
239+
A regular expression, or an array of regular expressions, can be used to test file paths and only cache certain modules. For example, to only cache utilities:
221240

222241
```js
223242
unsafeCache: /src\/utilities/
@@ -230,9 +249,7 @@ W> Changes to cached paths may cause failure in rare cases.
230249

231250
`object`
232251

233-
This set of options is identical to the `resolve` set above, but is used only to resolve webpack's [loader](/concepts/loaders) packages.
234-
235-
Default:
252+
This set of options is identical to the `resolve` set above, but is used only to resolve webpack's [loader](/concepts/loaders) packages. Default:
236253

237254
```js
238255
{
@@ -244,32 +261,46 @@ Default:
244261

245262
T> Note that you can use alias here and other features familiar from resolve. For example `{ txt: 'raw-loader' }` would shim `txt!templates/demo.txt` to use `raw-loader`.
246263

247-
## `resolveLoader.moduleTemplates`
264+
265+
## `resolveLoader.moduleExtensions`
248266

249267
`array`
250268

251-
That's a `resolveLoader` only property.It describes alternatives for the module name that are tried.
269+
The extensions which are tried when resolving a module (e.g. loaders). By default this is an empty array.
252270

253-
Default: `["*-webpack-loader", "*-web-loader", "*-loader", "*"]`
271+
If you want to use loaders without the `-loader` suffix, you can use this:
254272

273+
```js
274+
moduleExtensions: ['-loaders']
275+
```
255276

256-
## `resolveLoader.moduleExtensions`
257277

258-
`array`
278+
## `resolve.plugins`
259279

260-
Example: `['-loaders']`
280+
A list of additional resolve plugins which should be applied. It allows plugins such as [`DirectoryNamedWebpackPlugin`](https://www.npmjs.com/package/directory-named-webpack-plugin).
261281

262-
These extensions which are tried when resolving a module.
282+
```js
283+
plugins: [new DirectoryNamedWebpackPlugin()]
284+
```
263285

264286

265-
## `resolve.plugins`
287+
## `resolve.symlinks`
266288

267-
?> Description
289+
`boolean`
268290

269-
## `resolve.symlinks`
291+
Whether to resolve symlinks to their symlinked location. Default:
292+
293+
```js
294+
symlinks: true
295+
```
270296

271-
?> Description
272297

273298
## `resolve.cachePredicate`
274299

275-
?> Description
300+
`function`
301+
302+
A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. Default:
303+
304+
```js
305+
cachePredicate: function() { return true }
306+
```

0 commit comments

Comments
 (0)