You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-17Lines changed: 21 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -13,24 +13,26 @@ Support for Node v3 and lower was dropped, but you can install and use the loade
13
13
14
14
## Usage:
15
15
16
-
In general, loader allows to perform replacements in a way [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) does (loader uses it internally).
17
-
For instance, it means that if you want to replace all occurrences, you should use RegExp-like string in `query.search` with `g` flag in `query.flags`, etc.
16
+
Loader allows to perform replacements in a way [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) does (loader uses it internally).
17
+
It means that if you want to replace all occurrences, you should use RegExp-like string in `options.search` with `g` flag in `options.flags`, etc.
18
18
19
19
### Plain replacement:
20
20
21
+
Plain string replacement, no need to escape RegEx special characters.
22
+
21
23
In your `webpack.config.js`:
22
24
23
25
```javascript
24
26
module.exports= {
25
27
// ...
26
28
module: {
27
-
loaders: [
29
+
rules: [
28
30
{
29
31
test:/fileInWhichJQueryIsUndefined\.js$/,
30
32
loader:'string-replace-loader',
31
-
query: {
32
-
search:'jQuery',
33
-
replace:'window.$'
33
+
options: {
34
+
search:'$',
35
+
replace:'window.jQuery',
34
36
}
35
37
}
36
38
]
@@ -40,23 +42,24 @@ module.exports = {
40
42
41
43
### RegEx replacement:
42
44
43
-
To achieve regular expression replacement you should specify the `flags`query param
45
+
To achieve regular expression replacement you should specify the `flags`option
44
46
(as an empty string if you do not want any flags). In this case, `search` and `flags` are being
45
-
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor.
47
+
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor
48
+
and this means that you should escape RegEx special characters in `search` if you want it to be replaced as a string.
46
49
47
50
In your `webpack.config.js`:
48
51
49
52
```javascript
50
53
module.exports= {
51
54
// ...
52
55
module: {
53
-
loaders: [
56
+
rules: [
54
57
{
55
58
test:/fileInWhichJQueryIsUndefined\.js$/,
56
59
loader:'string-replace-loader',
57
-
query: {
58
-
search:'jquery',
59
-
replace:'window.$',
60
+
options: {
61
+
search:'\$',
62
+
replace:'window.jQuery',
60
63
flags:'i'
61
64
}
62
65
}
@@ -75,11 +78,11 @@ In your `webpack.config.js`:
75
78
module.exports= {
76
79
// ...
77
80
module: {
78
-
loaders: [
81
+
rules: [
79
82
{
80
83
test:/\.js$/,
81
84
loader:'string-replace-loader',
82
-
query: {
85
+
options: {
83
86
multiple: [
84
87
{ search:'jQuery', replace:'window.$' },
85
88
{ search:'_', replace:'window.lodash' }
@@ -93,19 +96,20 @@ module.exports = {
93
96
94
97
### Strict mode replacement:
95
98
96
-
You can set strict mode to ensure that the replacement was done:
99
+
You can enable strict mode to ensure that the replacement was performed.
100
+
Loader will throw exception if nothing was replaced or if `search` or `replace` options were not specified.
0 commit comments