-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
38 lines (30 loc) · 1006 Bytes
/
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
const loaderUtils = require("loader-utils");
const mkRegexp = fnName => {
const parts = [fnName, "\\(", "([^)]+)", "\\)"];
return new RegExp(parts.join("\\s*"), 'g');
};
const transform = objString => {
return objString.replace(/'[^']+\.svg'/g, "require($&)");
};
const loader = function(source, inputSourceMap) {
if (this.cacheable) this.cacheable();
const config = loaderUtils.getOptions(this) || {};
const packageName = config.package || "rnons/elm-svg-loader";
config.module = config.module || "InlineSvg";
config.tagger = config.tagger || "inline";
const taggerName =
"_" +
[
packageName.replace(/-/g, "_").replace(/\//g, "$"),
config.module.replace(/\./g, "_"),
config.tagger
].join("$");
const escapedTaggerName = taggerName.replace(/\$/g, "\\$");
const regexp = mkRegexp(escapedTaggerName);
source = source.replace(
regexp,
(match, p1) => taggerName + "(" + transform(p1) + ")"
);
return source;
};
module.exports = loader;