Skip to content

Commit 2dd9941

Browse files
committed
some improvements to binding context
1 parent 437cf61 commit 2dd9941

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ module.exports = {
120120
### Callback replacement
121121

122122
You can specify a callback function to have dynamic replacement values.
123+
The context of this function will be the context of the loader.
123124

124125
In your `webpack.config.js`:
125126

lib/replace.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
function replace (source, options, context) {
3-
const { replace, flags, strict } = options
3+
const { flags, strict } = options
4+
45
let search
56
if (options.search instanceof RegExp) {
67
// if the `search` type is RegExp, we ignore `flags`
@@ -11,11 +12,18 @@ function replace (source, options, context) {
1112
search = options.search
1213
}
1314

15+
let replace
16+
if (typeof options.replace === 'function') {
17+
replace = options.replace.bind(context)
18+
} else {
19+
replace = options.replace
20+
}
21+
1422
if (strict && (typeof search === 'undefined' || search === null || typeof replace === 'undefined' || replace === null)) {
1523
throw new Error('Replace failed (strict mode) : options.search and options.replace are required')
1624
}
1725

18-
const newSource = source.replace(search, typeof replace === 'function' ? replace.bind(context) : replace)
26+
const newSource = source.replace(search, replace)
1927

2028
if (strict && (newSource === source)) {
2129
throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace)

0 commit comments

Comments
 (0)