-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
42 lines (32 loc) · 1.04 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
39
40
41
42
const utils = require('loader-utils')
function performReplacement(source, options) {
if (!options) return source
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
}