-
Notifications
You must be signed in to change notification settings - Fork 14
/
build-svg-sprite.js
79 lines (68 loc) · 1.75 KB
/
build-svg-sprite.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
import fs from 'fs';
import path from 'path';
import SVGSpriter from 'svg-sprite';
import url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const DATA = [
{
input: path.resolve(__dirname, './blocks/CloudImageEditor/src/icons/'),
output: path.resolve(__dirname, './blocks/CloudImageEditor/src/svg-sprite.js'),
},
{
input: path.resolve(__dirname, './blocks/themes/uc-basic/icons/'),
output: path.resolve(__dirname, './blocks/themes/uc-basic/svg-sprite.js'),
},
];
const config = {
mode: {
symbol: {
inline: true,
},
},
shape: {
id: {
generator: (name) => `uc-icon-${name.replace(/\.svg$/, '')}`,
},
transform: [
{
svgo: {
plugins: [
{
name: 'preset-default',
},
{
name: 'prefixIds',
params: {
prefix: 'uc-icon-id',
},
},
],
},
},
],
},
};
console.log('Generating SVG sprite...');
DATA.forEach((item) => {
const spriter = new SVGSpriter(config);
fs.readdir(item.input, (err, files) => {
if (err) {
throw err;
}
console.log(`Processing ${item.input}...`);
files.forEach((file) => {
const filePath = path.resolve(item.input, file);
console.log(`Icon processed: ${filePath}`);
spriter.add(filePath, null, fs.readFileSync(filePath, { encoding: 'utf-8' }));
});
spriter.compile((error, result) => {
if (error) {
throw error;
}
const jsTemplate = `export default "${result.symbol.sprite.contents.toString().replace(/\"/g, "'")}";`
.trim()
.concat('\n');
fs.writeFileSync(item.output, jsTemplate);
});
});
});