Skip to content

docs(plugins) IgnorePlugin new syntax and options #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/content/plugins/ignore-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@ contributors:
- EugeneHlushko
---

Prevent generation of modules for `import` or `require` calls matching the following regular expressions:
IgnorePlugin prevents generation of modules for `import` or `require` calls matching the regular expressions or filter functions:

- `requestRegExp` A RegExp to test the request against.
- `contextRegExp` (optional) A RegExp to test the context (directory) against.
## Using regular expressions

``` js
- `requestRegExp`: A RegExp to test the request against.
- `contextRegExp`: (optional) A RegExp to test the context (directory) against.

```javascript
new webpack.IgnorePlugin({requestRegExp, contextRegExp});
// old way, deprecated in webpack v5
new webpack.IgnorePlugin(requestRegExp, [contextRegExp]);
```

The following examples demonstrate a few ways this plugin can be used.
## Using filter functions

- `checkContext(context)` A Filter function that receives context as the argument, must return boolean.
- `checkResource(resource)` A Filter function that receives resource as the argument, must return boolean.

```javascript
new webpack.IgnorePlugin({
checkContext (context) {
// do something with context
return true|false;
},
checkResource (resource) {
// do something with resource
return true|false;
}
});
```

## Ignore Moment Locales
## Example of ignoring Moment Locales

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)).

Expand All @@ -37,8 +56,11 @@ require('./locale/' + name);

...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:

```js
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/);
```javascript
new webpack.IgnorePlugin({
requestRegExp: /^\.\/locale$/,
contextRegExp: /moment$/
});
```

...which means "any require statement matching `'./locale'` from any directories ending with `'moment'` will be ignored.