Skip to content

Commit

Permalink
version 2.3.0; support for RegExp search option
Browse files Browse the repository at this point in the history
  • Loading branch information
Va1 committed Apr 8, 2020
1 parent 002a0ae commit 33f683f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3,724 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ npm install --save-dev string-replace-loader
```

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

## Usage:

Expand Down Expand Up @@ -42,13 +42,33 @@ module.exports = {

### RegEx replacement:

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

In your `webpack.config.js`:

```javascript
module.exports = {
// ...
module: {
rules: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace-loader',
options: {
search: /\$/i,
replace: 'window.jQuery'
}
}
]
}
}
```
or
```javascript
module.exports = {
// ...
Expand Down Expand Up @@ -117,7 +137,7 @@ module.exports = {
]
}
}
```
```

### Strict mode replacement:

Expand Down
11 changes: 9 additions & 2 deletions lib/getOptionsArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ const optionsSchema = {
type: 'object',
properties: {
search: {
type: 'string'
anyOf: [
{
instanceof: 'RegExp'
},
{
type: 'string'
}
]
},
replace: {
anyOf: [
{
typeof: 'function'
instanceof: 'Function'
},
{
type: 'string'
Expand Down
16 changes: 10 additions & 6 deletions lib/replace.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

function replace (source, options) {
const { replace, flags, strict } = options
const search = (
flags === null
? options.search
: new RegExp(options.search, flags)
)
let search
if (options.search instanceof RegExp) {
// if the `search` type is RegExp, we ignore `flags`
search = options.search
} else if (flags !== null) {
search = new RegExp(options.search, flags)
} else {
search = options.search
}

if (strict && (search === null || replace === null)) {
if (strict && (typeof search === 'undefined' || search === null || typeof replace === 'undefined' || replace === null)) {
throw new Error('Replace failed (strict mode) : options.search and options.replace are required')
}

Expand Down
Loading

0 comments on commit 33f683f

Please sign in to comment.