-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
79 lines (66 loc) · 2.07 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const path = require('path');
const fs = require('fs');
const SVGO = require('svgo');
const parser = require('posthtml-parser');
const match = require('posthtml/lib/api').match;
const defaultSvgoOptions = {
plugins: [
{ removeXMLNS: true },
{ removeViewBox: false },
{ removeDimensions: true },
],
};
const cache = {};
module.exports = function postHtmlInlineSvg(options = {}) {
const cwd = options.cwd || process.cwd();
const tag = options.tag || 'icon';
const attr = options.attr || 'src';
const svgo = new SVGO(options.svgo || defaultSvgoOptions);
const localCache = {};
const getSVG = (filePath) => {
if (localCache[filePath]) {
return localCache[filePath];
}
const stats = fs.statSync(filePath);
const modifiedAt = stats.mtimeMs;
if (cache[filePath] && cache[filePath].modifiedAt === modifiedAt) {
localCache[filePath] = cache[filePath].promise;
return cache[filePath].promise;
}
const promise = new Promise((resolve) => {
const data = fs.readFileSync(filePath);
return svgo.optimize(data.toString()).then(result => resolve(result.data));
});
cache[filePath] = { promise, modifiedAt };
localCache[filePath] = promise;
return promise;
};
return tree => new Promise((resolve, reject) => {
const promises = [];
if (!tree.parser) tree.parser = parser;
if (!tree.match) tree.match = match;
tree.match({ tag }, (node) => {
promises.push(new Promise(async (resolve, reject) => {
try {
const src = node.attrs[attr];
const svg = await getSVG(path.resolve(cwd, src));
const nodes = parser(svg);
const attrs = node.attrs;
delete attrs[attr];
Object.assign(nodes[0].attrs, attrs);
node.tag = false;
node.content = options.comment
? [`<!-- ${src} -->`, ...nodes]
: nodes;
resolve();
} catch (err) {
reject(err);
}
}));
return node;
});
Promise.all(promises)
.then(() => resolve(tree))
.catch(reject);
});
};