forked from Va1/string-replace-loader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (30 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var _ = require('lodash');
var loaderUtils = require('loader-utils');
function processOptions(source, options) {
if (!_.isUndefined(options.search) && !_.isUndefined(options.replace)) {
var search = options.search;
if (!_.isUndefined(options.flags)) {
search = new RegExp(options.search, options.flags);
}
var newSource = source.replace(search, options.replace);
if (options.strict && (newSource === source)) {
throw new Error('Cannot replace ' + options.search + ' → ' + options.replace);
}
} else if (options.strict) {
throw new Error('Cannot replace: undefined search or/and option(s) → ' + JSON.stringify(options));
}
return newSource;
}
module.exports = function (source, map) {
this.cacheable();
var options = loaderUtils.getOptions(this);
if (_.isArray(options.multiple)) {
options.multiple.forEach(function (suboptions) {
suboptions.strict = (!_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict);
source = processOptions(source, suboptions);
});
} else {
source = processOptions(source, options);
}
this.callback(null, source, map);
};