Skip to content

Commit c200a9f

Browse files
authored
Merge pull request #41 from balping/master
Replacement callbacks
2 parents 37b60c9 + cf6772d commit c200a9f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ module.exports = {
9494
}
9595
```
9696

97+
### Replacement with callback
98+
99+
You can specify a callback function to have dynamic replacement values.
100+
101+
In your `webpack.config.js`:
102+
103+
```javascript
104+
module.exports = {
105+
// ...
106+
module: {
107+
rules: [
108+
{
109+
test: /\.js$/,
110+
loader: 'string-replace-loader',
111+
options: {
112+
search: '^Hello, (.*)!$',
113+
replace: function(match, p1, offset, string){
114+
return 'Bonjour, ' + p1.toUpperCase() + '!!!';
115+
},
116+
flags: 'gi'
117+
}
118+
}
119+
]
120+
}
121+
}
122+
```
123+
97124
### Strict mode replacement:
98125

99126
You can enable strict mode to ensure that the replacement was performed.

lib/getOptionsArray.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const optionsSchema = {
1010
type: 'string'
1111
},
1212
replace: {
13-
type: 'string'
13+
'anyOf': [
14+
{ 'typeof': 'function' },
15+
{ 'type': 'string' }
16+
]
1417
},
1518
flags: {
1619
type: 'string',

0 commit comments

Comments
 (0)