Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ module.exports = {
}
```

### Callback replacement

You can specify a callback function to have dynamic replacement values.

In your `webpack.config.js`:

```javascript
module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
loader: 'string-replace-loader',
options: {
search: '^Hello, (.*)!$',
replace: (match, p1, offset, string) => `Bonjour, ${p1.toUpperCase()}!`,
flags: 'g'
}
}
]
}
}
```

### Strict mode replacement:

You can enable strict mode to ensure that the replacement was performed.
Expand Down
9 changes: 8 additions & 1 deletion lib/getOptionsArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const optionsSchema = {
type: 'string'
},
replace: {
type: 'string'
anyOf: [
{
typeof: 'function'
},
{
type: 'string'
}
]
},
flags: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-replace-loader",
"version": "2.1.1",
"version": "2.2.0",
"description": "Replace loader for Webpack",
"keywords": [
"webpack",
Expand Down
32 changes: 28 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe('Webpack replace loader ...', () => {
test: /\.js$/,
loader: '__this-loader',
options: {
search: 'var VALUE = \'\.*\'',
replace: 'var a = \'\'',
flags: 'i'
search: `var VALUE = '.*'`,
replace: `var a = ''`,
flags: 'ig'
}
}),
(error, stats) => {
Expand All @@ -67,7 +67,7 @@ describe('Webpack replace loader ...', () => {
expect(error).to.equal(null)
expect(contents).to.be.a('string')
expect(contents).to.not.include('var value')
expect(contents).to.include('var a = \'\'')
expect(contents).to.include(`var a = ''`)
done()
})
}
Expand Down Expand Up @@ -107,6 +107,30 @@ describe('Webpack replace loader ...', () => {
)
})

it('should replace with function replace', done => {
webpack(getTestWebPackConfig(
{
test: /\.js$/,
loader: '__this-loader',
options: {
search: `var value = '(baz)'`,
replace: (match, p1, offset, string) => `var a = '${p1.toUpperCase()}'`,
flags: 'g'
}
}),
(error, stats) => {
expect(error).to.equal(null)

fs.readFile(outputFilePath, 'utf8', (error, contents) => {
expect(error).to.equal(null)
expect(contents).to.be.a('string')
expect(contents).to.include(`var a = 'BAZ'`)
done()
})
}
)
})

it('should replace using multiple queries', done => {
webpack(getTestWebPackConfig(
{
Expand Down