3
3
import async from 'async' ;
4
4
import fs from 'fs-extra' ;
5
5
import path from 'path' ;
6
- import parser from 'xml2json ' ;
6
+ import xml2js from 'xml2js ' ;
7
7
import omit from 'lodash.omit' ;
8
8
9
9
console . log ( '# Building the inline SVG icons' ) ;
@@ -14,6 +14,18 @@ const outputFile = (filename, data, done) => {
14
14
fs . outputFile ( outputPath , data . join ( '\n' ) , done ) ;
15
15
} ;
16
16
17
+ const parseXml = ( string , done ) => {
18
+ xml2js . parseString (
19
+ string ,
20
+ {
21
+ emptyTag : { } ,
22
+ explicitArray : false ,
23
+ mergeAttrs : true ,
24
+ } ,
25
+ done
26
+ ) ;
27
+ } ;
28
+
17
29
const inlineIcons = ( spriteType , done ) => {
18
30
const inputPath = path . join (
19
31
__dirname ,
@@ -35,32 +47,34 @@ const inlineIcons = (spriteType, done) => {
35
47
"let icons = {}; if ('__EXCLUDE_SLDS_ICONS__' === '__INCLUDE_SLDS_ICONS__') { icons = {" ,
36
48
] ;
37
49
38
- const sprite = JSON . parse ( parser . toJson ( text ) ) ;
39
-
40
- let viewBox ;
41
- async . each (
42
- sprite . svg . symbol ,
43
- ( symbol , iconDone ) => {
44
- let data = omit ( symbol , [ 'id' ] ) ;
45
- const iconName = symbol . id . toLowerCase ( ) ;
46
-
47
- const icon = [ license , `export default ${ JSON . stringify ( data ) } ;` , '' ] ;
48
-
49
- outputFile ( `${ spriteType } /${ iconName } ` , icon , iconDone ) ;
50
-
51
- if ( ! viewBox ) viewBox = data . viewBox ;
52
- data = omit ( data , [ 'viewBox' ] ) ;
53
- index . push ( `${ iconName } :${ JSON . stringify ( data ) } ,` ) ;
54
- } ,
55
- ( err ) => {
56
- if ( err ) console . error ( err ) ;
57
- }
58
- ) ;
59
-
60
- index . push ( `viewBox:'${ viewBox } '` ) ;
61
- index . push ( '}; } export default icons;' ) ;
62
- index . push ( '' ) ;
63
- outputFile ( `${ spriteType } /index` , index , done ) ;
50
+ parseXml ( text , ( error , sprite ) => {
51
+ if ( error ) throw error ;
52
+
53
+ let viewBox ;
54
+ async . each (
55
+ sprite . svg . symbol ,
56
+ ( symbol , iconDone ) => {
57
+ let data = omit ( symbol , [ 'id' ] ) ;
58
+ const iconName = symbol . id . toLowerCase ( ) ;
59
+
60
+ const icon = [ license , `export default ${ JSON . stringify ( data ) } ;` , '' ] ;
61
+
62
+ outputFile ( `${ spriteType } /${ iconName } ` , icon , iconDone ) ;
63
+
64
+ if ( ! viewBox ) viewBox = data . viewBox ;
65
+ data = omit ( data , [ 'viewBox' ] ) ;
66
+ index . push ( `${ iconName } :${ JSON . stringify ( data ) } ,` ) ;
67
+ } ,
68
+ ( err ) => {
69
+ if ( err ) console . error ( err ) ;
70
+ }
71
+ ) ;
72
+
73
+ index . push ( `viewBox:'${ viewBox } '` ) ;
74
+ index . push ( '}; } export default icons;' ) ;
75
+ index . push ( '' ) ;
76
+ outputFile ( `${ spriteType } /index` , index , done ) ;
77
+ } ) ;
64
78
} ;
65
79
66
80
async . each (
0 commit comments