-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (33 loc) · 1 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
const { parse, stringify } = require("svgson");
const loaderUtils = require("loader-utils");
const fs = require("fs");
const path = require("path");
module.exports = function (source) {
debugger;
this.cacheable(true);
const callback = this.callback;
const asyncCallback = this.async();
const config = loaderUtils.getOptions(this) || {};
const handler = config.handler;
try {
const recursive = function (json, playload={}) {
let newPlayload = handler(json, playload) || playload;
if (json.children.length) {
json.children.forEach((sub) => recursive(sub, newPlayload));
}
};
if (typeof handler === "function") {
parse(source)
.then(
(json) => {
recursive(json);
return json;
},
(err) => asyncCallback(err instanceof Error ? err : new Error(err))
)
.then((json) => asyncCallback(null, stringify(json)));
}
} catch (err) {
callback(err instanceof Error ? err : new Error(err));
}
};