forked from aganglada/pattern-replace-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (31 loc) · 1.11 KB
/
index.js
File metadata and controls
39 lines (31 loc) · 1.11 KB
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
39
const utils = require('loader-utils');
function performReplacement(source, options) {
const searchDefined = Boolean(options.search) || options.search === '';
const replaceDefined = Boolean(options.replace) || options.replace === '';
if (searchDefined && replaceDefined) {
if (options.flags) {
options.search = new RegExp(options.search, options.flags);
}
source = source.replace(options.search, options.replace);
}
return source;
}
module.exports = function (source) {
if (this.cacheable) {
this.cacheable();
}
const optionsConfig = this.options || this.query;
const options = typeof optionsConfig === 'object' ?
utils.getOptions(this) : utils.parseQuery(this.query);
if (options && options.verbose) {
console.log('\nReplacing in file:', this.resourcePath);
}
if (Array.isArray(options.multiple)) {
options.multiple.forEach(function (opt) {
source = performReplacement(source, opt);
});
} else {
source = performReplacement(source, options);
}
return source;
};