Skip to content

Commit 5861ac6

Browse files
authored
docs(plugins): update context-replacement-plugin (#1551)
Make some minor grammatical improvements and elaborate on certain sections of this plugin's usage.
1 parent 597b211 commit 5861ac6

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/content/plugins/context-replacement-plugin.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ related:
1212
The `ContextReplacementPlugin` allows you to override the inferred information. There are various ways to configure the plugin:
1313

1414

15-
## `newContentResource`, `newContentRecursive`, `newContentRegExp`
15+
## Usage
1616

1717
```javascript
1818
new webpack.ContextReplacementPlugin(
@@ -25,16 +25,19 @@ new webpack.ContextReplacementPlugin(
2525

2626
If the resource (directory) matches `resourceRegExp`, the plugin replaces the default resource, recursive flag or generated regular expression with `newContentResource`, `newContentRecursive` or `newContextRegExp` respectively. If `newContentResource` is relative, it is resolved relative to the previous resource.
2727

28-
**Example**
28+
Here's a small example to restrict module usage:
2929

3030
```javascript
31-
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /de|fr|hu/)
31+
new webpack.ContextReplacementPlugin(
32+
/moment[\/\\]locale$/,
33+
/de|fr|hu/
34+
)
3235
```
3336

34-
The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see also [this GitHub issue](https://github.com/moment/moment/issues/2373)).
37+
The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see [this issue](https://github.com/moment/moment/issues/2373) for more information).
3538

3639

37-
## `newContentCallback`
40+
## Content Callback
3841

3942
```javascript
4043
new webpack.ContextReplacementPlugin(
@@ -43,22 +46,25 @@ new webpack.ContextReplacementPlugin(
4346
)
4447
```
4548

46-
The function `newContentCallback` is given a [`data` object of the `ContextModuleFactory`](/api/plugins/module-factories/) and it is expected to overwrite the `request` attribute of the supplied object.
49+
The `newContentCallback` function is given a [`data` object of the `ContextModuleFactory`](/api/plugins/module-factories/) and is expected to overwrite the `request` attribute of the supplied object.
4750

48-
**Example**
51+
Using this callback we can dynamically redirect requests to a new location:
4952

5053
```javascript
5154
new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
52-
if (!/\/moment\//.test(context.context)) { return; }
55+
if ( !/\/moment\//.test(context.context) ) return;
56+
5357
Object.assign(context, {
5458
regExp: /^\.\/\w+/,
55-
request: '../../locale', // resolved relatively
59+
request: '../../locale' // resolved relatively
5660
});
5761
}),
5862
```
5963

6064

61-
## `newContentResource`, `newContentCreateContextMap`
65+
## Other Options
66+
67+
The `newContentResource` and `newContentCreateContextMap` parameters are also available:
6268

6369
```javascript
6470
new webpack.ContextReplacementPlugin(
@@ -68,12 +74,11 @@ new webpack.ContextReplacementPlugin(
6874
)
6975
```
7076

71-
**Example**
77+
These two parameters can be used together to redirect requests in a more targeted way. The `newContentCreateContextMap` allows you to map runtime requests to compile requests in the form of an object:
7278

7379
```javascript
7480
new ContextReplacementPlugin(/selector/, './folder', {
7581
'./request': './request',
7682
'./other-request': './new-request'
77-
/* runtime-request: compile-time request */
7883
})
7984
```

0 commit comments

Comments
 (0)