Skip to content

Commit 33f683f

Browse files
committed
version 2.3.0; support for RegExp search option
1 parent 002a0ae commit 33f683f

File tree

6 files changed

+71
-3724
lines changed

6 files changed

+71
-3724
lines changed

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $ npm install --save-dev string-replace-loader
99
```
1010

1111
With release of 2.0.0 the loader is expected to be used in Node v4+ environment.
12-
Support for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments.
12+
Support for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments.
1313

1414
## Usage:
1515

@@ -42,13 +42,33 @@ module.exports = {
4242

4343
### RegEx replacement:
4444

45-
To achieve regular expression replacement you should specify the `flags` option
46-
(as an empty string if you do not want any flags). In this case, `search` and `flags` are being
47-
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor
45+
To achieve regular expression replacement you should either specify the `search` option as
46+
[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance,
47+
either specify it as string and add the `flags` option (as an empty string if you do not want any flags).
48+
In the latter case, `search` and `flags` are being passed to the
49+
[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor
4850
and this means that you should escape RegEx special characters in `search` if you want it to be replaced as a string.
4951

5052
In your `webpack.config.js`:
5153

54+
```javascript
55+
module.exports = {
56+
// ...
57+
module: {
58+
rules: [
59+
{
60+
test: /fileInWhichJQueryIsUndefined\.js$/,
61+
loader: 'string-replace-loader',
62+
options: {
63+
search: /\$/i,
64+
replace: 'window.jQuery'
65+
}
66+
}
67+
]
68+
}
69+
}
70+
```
71+
or
5272
```javascript
5373
module.exports = {
5474
// ...
@@ -117,7 +137,7 @@ module.exports = {
117137
]
118138
}
119139
}
120-
```
140+
```
121141

122142
### Strict mode replacement:
123143

lib/getOptionsArray.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ const optionsSchema = {
77
type: 'object',
88
properties: {
99
search: {
10-
type: 'string'
10+
anyOf: [
11+
{
12+
instanceof: 'RegExp'
13+
},
14+
{
15+
type: 'string'
16+
}
17+
]
1118
},
1219
replace: {
1320
anyOf: [
1421
{
15-
typeof: 'function'
22+
instanceof: 'Function'
1623
},
1724
{
1825
type: 'string'

lib/replace.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11

22
function replace (source, options) {
33
const { replace, flags, strict } = options
4-
const search = (
5-
flags === null
6-
? options.search
7-
: new RegExp(options.search, flags)
8-
)
4+
let search
5+
if (options.search instanceof RegExp) {
6+
// if the `search` type is RegExp, we ignore `flags`
7+
search = options.search
8+
} else if (flags !== null) {
9+
search = new RegExp(options.search, flags)
10+
} else {
11+
search = options.search
12+
}
913

10-
if (strict && (search === null || replace === null)) {
14+
if (strict && (typeof search === 'undefined' || search === null || typeof replace === 'undefined' || replace === null)) {
1115
throw new Error('Replace failed (strict mode) : options.search and options.replace are required')
1216
}
1317

0 commit comments

Comments
 (0)