Skip to content

Commit 002a0ae

Browse files
authored
Merge pull request #52 from Va1/develop
merge develop into master
2 parents 37b60c9 + 6f2dcef commit 002a0ae

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed

README.md

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

97+
### Callback replacement
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: (match, p1, offset, string) => `Bonjour, ${p1.toUpperCase()}!`,
114+
flags: 'g'
115+
}
116+
}
117+
]
118+
}
119+
}
120+
```
121+
97122
### Strict mode replacement:
98123

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

lib/getOptionsArray.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ const optionsSchema = {
1010
type: 'string'
1111
},
1212
replace: {
13-
type: 'string'
13+
anyOf: [
14+
{
15+
typeof: 'function'
16+
},
17+
{
18+
type: 'string'
19+
}
20+
]
1421
},
1522
flags: {
1623
type: 'string',

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "string-replace-loader",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "Replace loader for Webpack",
55
"keywords": [
66
"webpack",

test/index.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ describe('Webpack replace loader ...', () => {
5555
test: /\.js$/,
5656
loader: '__this-loader',
5757
options: {
58-
search: 'var VALUE = \'\.*\'',
59-
replace: 'var a = \'\'',
60-
flags: 'i'
58+
search: `var VALUE = '.*'`,
59+
replace: `var a = ''`,
60+
flags: 'ig'
6161
}
6262
}),
6363
(error, stats) => {
@@ -67,7 +67,7 @@ describe('Webpack replace loader ...', () => {
6767
expect(error).to.equal(null)
6868
expect(contents).to.be.a('string')
6969
expect(contents).to.not.include('var value')
70-
expect(contents).to.include('var a = \'\'')
70+
expect(contents).to.include(`var a = ''`)
7171
done()
7272
})
7373
}
@@ -107,6 +107,30 @@ describe('Webpack replace loader ...', () => {
107107
)
108108
})
109109

110+
it('should replace with function replace', done => {
111+
webpack(getTestWebPackConfig(
112+
{
113+
test: /\.js$/,
114+
loader: '__this-loader',
115+
options: {
116+
search: `var value = '(baz)'`,
117+
replace: (match, p1, offset, string) => `var a = '${p1.toUpperCase()}'`,
118+
flags: 'g'
119+
}
120+
}),
121+
(error, stats) => {
122+
expect(error).to.equal(null)
123+
124+
fs.readFile(outputFilePath, 'utf8', (error, contents) => {
125+
expect(error).to.equal(null)
126+
expect(contents).to.be.a('string')
127+
expect(contents).to.include(`var a = 'BAZ'`)
128+
done()
129+
})
130+
}
131+
)
132+
})
133+
110134
it('should replace using multiple queries', done => {
111135
webpack(getTestWebPackConfig(
112136
{

0 commit comments

Comments
 (0)