Skip to content

Commit 7cbf85d

Browse files
author
Valentyn
committed
Merge pull request #4 from easybiblabs/topics/support-option-list
Topics/support option list
2 parents f61fb28 + 47b9740 commit 7cbf85d

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,32 @@ module.exports = {
4949
test: /fileInWhichJQueryIsUndefined\.js$/,
5050
loader: 'string-replace',
5151
query: {
52-
search: 'jquery',
53-
replace: 'window.$',
54-
flags: 'i'
52+
search: /jquery/i,
53+
replace: 'window.$'
54+
}
55+
}
56+
]
57+
}
58+
}
59+
```
60+
61+
### Array replacement:
62+
63+
In your `webpack.config.js`:
64+
65+
```javascript
66+
module.exports = {
67+
// ...
68+
module: {
69+
loaders: [
70+
{
71+
test: /\.js$/,
72+
loader: 'string-replace',
73+
query: {
74+
multiple: [
75+
{search: 'framework', replace: 'flamewar'},
76+
{search: 'ants', replace: 'super ants'},
77+
]
5578
}
5679
}
5780
]

index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
var utils = require('loader-utils');
22

3-
module.exports = function (source) {
3+
function processQuery(source, option) {
4+
if (typeof option.search !== 'undefined' && typeof option.replace !== 'undefined') {
5+
return source.split(option.search).join(option.replace);
6+
}
7+
8+
return source;
9+
}
10+
11+
module.exports = function(source) {
412
this.cacheable();
13+
514
var query = utils.parseQuery(this.query);
615

7-
if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') {
8-
if (typeof query.flags !== 'undefined') {
9-
query.search = new RegExp(query.search, query.flags);
16+
if (Array.isArray(query.multiple)) {
17+
var length = query.multiple.length;
18+
19+
for (var i = 0; i < length; i++) {
20+
var option = query.multiple[i];
21+
source = processQuery(source, option);
1022
}
1123

12-
source = source.replace(query.search, query.replace);
24+
return source;
1325
}
1426

15-
return source;
27+
return processQuery(source, query);
1628
};

0 commit comments

Comments
 (0)