Skip to content

Commit fb9c9b4

Browse files
jpage-godaddymontogeek
authored andcommitted
docs(plugins) Try to clarify how ignore-plugin works (webpack#2130)
* Try to clarify how ignore-plugin works The way `ignore-plugin` works wasn't very intuitive to me, so I'd like to try to clarify exactly what the regexes mean in case anyone else is similarly confused. * Show broken code in 'diff' format
1 parent 3963d8c commit fb9c9b4

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/content/plugins/ignore-plugin.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: IgnorePlugin
33
contributors:
44
- simon04
55
- byzyk
6+
- DullReferenceException
67
---
78

89
Prevent generation of modules for `import` or `require` calls matching the following regular expressions:
@@ -19,8 +20,24 @@ The following examples demonstrate a few ways this plugin can be used.
1920

2021
## Ignore Moment Locales
2122

22-
As of [moment](https://momentjs.com/) 2.18, all locales are bundled together with the core library (see [this GitHub issue](https://github.com/moment/moment/issues/2373)). You can use the `IgnorePlugin` to stop any locale being bundled with moment:
23+
As of [moment](https://momentjs.com/) 2.18, all locales are bundled together with the core library (see [this GitHub issue](https://github.com/moment/moment/issues/2373)).
24+
25+
The `requestRegExp` parameter passed to `IgnorePlugin` is not tested against the resolved file names or absolute module names being imported or required, but rather against the _string_ passed to `require` or `import` _within the source code where the import is taking place_. For example, if you're trying to exclude `node_modules/moment/locale/*.js`, this won't work:
26+
27+
```diff
28+
-new webpack.IgnorePlugin(/moment\/locale\//);
29+
```
30+
31+
Rather, because `moment` imports with this code:
32+
33+
```js
34+
require('./locale/' + name);
35+
```
36+
37+
...your first regexp must match that `'./locale/'` string. The second `contextRegExp` parameter is then used to select specific directories from where the import took place. The following will cause those locale files to be ignored:
2338

2439
```js
2540
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/);
2641
```
42+
43+
...which means "any require statement matching `'./locale'` from any directories ending with `'moment'` will be ignored.

0 commit comments

Comments
 (0)