File tree 3 files changed +266
-116
lines changed
3 files changed +266
-116
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,31 @@ module.exports = {
87
87
}
88
88
```
89
89
90
+ ### Strict mode replacement:
91
+
92
+ You can set strict mode to ensure that the replacement was done:
93
+
94
+ In your ` webpack.config.js ` :
95
+
96
+ ``` javascript
97
+ module .exports = {
98
+ // ...
99
+ module: {
100
+ loaders: [
101
+ {
102
+ test: / fileInWhichJQueryIsUndefined\. js$ / ,
103
+ loader: ' string-replace' ,
104
+ query: {
105
+ search: ' jQuery' ,
106
+ replace: ' window.$' ,
107
+ strict: true
108
+ }
109
+ }
110
+ ]
111
+ }
112
+ }
113
+ ```
114
+
90
115
## Contributing:
91
116
92
117
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.
Original file line number Diff line number Diff line change @@ -7,10 +7,21 @@ function processOptions(source, options) {
7
7
options . search = new RegExp ( options . search , options . flags ) ;
8
8
}
9
9
10
- source = source . replace ( options . search , options . replace ) ;
10
+ newSource = source . replace ( options . search , options . replace ) ;
11
+ if ( options . strict === true && newSource === source ) {
12
+ throw new Error ( 'Cannot replace ' + options . search + ' → ' + options . replace ) ;
13
+ }
11
14
}
12
15
13
- return source ;
16
+ if ( options . strict === true && _ . isUndefined ( options . search ) ) {
17
+ throw new Error ( 'Cannot replace: search option is not defined → ' + JSON . stringify ( options ) ) ;
18
+ }
19
+
20
+ if ( options . strict === true && _ . isUndefined ( options . replace ) ) {
21
+ throw new Error ( 'Cannot replace: replace option is not defined → ' + JSON . stringify ( options ) ) ;
22
+ }
23
+
24
+ return newSource ;
14
25
}
15
26
16
27
module . exports = function ( source ) {
@@ -20,6 +31,7 @@ module.exports = function (source) {
20
31
21
32
if ( _ . isArray ( options . multiple ) ) {
22
33
options . multiple . forEach ( function ( suboptions ) {
34
+ suboptions . strict = ! _ . isUndefined ( suboptions . strict ) ? suboptions . strict : options . strict ;
23
35
source = processOptions ( source , suboptions ) ;
24
36
} ) ;
25
37
} else {
You can’t perform that action at this time.
0 commit comments