forked from svg-sprite/svg-sprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
88 lines (83 loc) · 2.42 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
87
88
'use strict';
const path = require('path');
const fs = require('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 spriter = new SVGSpriter({
dest,
log: 'debug',
svg: {
doctypeDeclaration: false,
xmlDeclaration: false
},
shape: {
transform: [{
svgo: {
multipass: true,
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeUnknownsAndDefaults: {
keepRoleAttr: true
},
removeViewBox: false
}
}
},
'cleanupListOfValues',
'convertStyleToAttrs',
'sortAttrs',
{
name: 'removeAttrs',
params: {
attrs: [
'clip-rule',
'data-name'
]
}
}
]
}
}]
}
});
/**
* Add a bunch of SVG files
*
* @param {SVGSpriter} spriter Spriter instance
* @param {Array} files SVG files
* @returns {SVGSpriter} Spriter instance
*/
function addFixtureFiles(spriter, files) {
files.forEach(file => {
spriter.add(
path.resolve(path.join(cwd, file)),
file,
fs.readFileSync(path.join(cwd, file), '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 in result.css) {
if (Object.prototype.hasOwnProperty.call(result.css, type)) {
fs.mkdirSync(path.dirname(result.css[type].path), { recursive: true });
fs.writeFileSync(result.css[type].path, result.css[type].contents);
}
}
});