forked from svg-sprite/svg-sprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
86 lines (79 loc) · 1.77 KB
/
example.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
80
81
82
83
84
85
86
'use strict';
const path = require('node:path');
const fs = require('node:fs');
const glob = require('glob');
const SVGSpriter = require('./lib/svg-sprite.js');
const cwd = path.join(__dirname, 'test/fixture/svg/single');
const dest = path.join(__dirname, 'tmp');
const files = glob.sync('**/weather*.svg', { cwd });
const svgoConfig = {
multipass: true,
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeUnknownsAndDefaults: {
keepRoleAttr: true
},
removeViewBox: false
}
}
},
'cleanupListOfValues',
'convertStyleToAttrs',
'sortAttrs',
{
name: 'removeAttrs',
params: {
attrs: [
'clip-rule',
'data-name'
]
}
}
]
};
const spriter = new SVGSpriter({
dest,
log: 'debug',
svg: {
doctypeDeclaration: false,
xmlDeclaration: false
},
shape: {
transform: [{
svgo: svgoConfig
}]
}
});
/**
* Add a bunch of SVG files
*
* @param {SVGSpriter} spriter Spriter instance
* @param {Array} files SVG files
* @returns {SVGSpriter} Spriter instance
*/
function addFixtureFiles(spriter, files) {
for (const file of files) {
const filePath = path.join(cwd, file);
spriter.add(path.resolve(filePath), file, fs.readFileSync(filePath, 'utf8'));
}
return spriter;
}
addFixtureFiles(spriter, files).compile({
css: {
sprite: 'svg/sprite.vertical.svg',
layout: 'vertical',
dimensions: true,
render: {
css: true,
scss: true
}
}
}, (error, result) => {
for (const type of Object.values(result.css)) {
fs.mkdirSync(path.dirname(type.path), { recursive: true });
fs.writeFileSync(type.path, type.contents);
}
});