-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (59 loc) · 2.03 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
'use strict';
const SVGSpriter = require('svg-sprite'),
path = require('path'),
mkdirp = require('mkdirp'),
fs = require('fs');
const ROOT_SVG_DIR = __dirname + '/node_modules/@material-design-icons/svg/';
const TYPES = [
'filled',
'outlined',
'round',
'sharp',
'two-tone'
];
TYPES.forEach(function(type){
var config = {
"svg": {
"xmlDeclaration": false,
"doctypeDeclaration": false,
"namespaceIDs": false,
"namespaceClassnames": false
},
"mode": {
"symbol": {
"dest": ".",
"sprite": "mdi-" + type + ".svg"
}
}
};
var spriter = new SVGSpriter(config);
var files = fs.readdirSync(ROOT_SVG_DIR + type);
// Register SVG files with the spriter
files.forEach(function(file){
spriter.add(
path.resolve(ROOT_SVG_DIR + type + '/' + file),
file,
fs.readFileSync(path.resolve(ROOT_SVG_DIR + type + '/' + file), { encoding: 'utf-8' })
);
})
// Compile the sprites
spriter.compile(function(error, result) {
// Run through all configured output modes
for (var mode in result) {
// Run through all created resources and write them to disk
for (var type in result[mode]) {
mkdirp.sync(path.dirname(result[mode][type].path));
fs.writeFileSync(result[mode][type].path, result[mode][type].contents);
cleanupSprite(result[mode][type].path);
}
}
});
});
function cleanupSprite (spriteFile)
{
var fileContent = fs.readFileSync(spriteFile, 'utf8');
var result = fileContent.replaceAll(' xmlns="http://www.w3.org/2000/svg"', '')
.replaceAll(' xmlns:xlink="http://www.w3.org/1999/xlink"', '')
.replace('<svg>', '<svg xmlns="http://www.w3.org/2000/svg">');
fs.writeFileSync(spriteFile, result, 'utf8');
}