11'use strict' ; 
22
33var  cheerio  =  require ( 'cheerio' ) , 
4-     events  =  require ( 'events' ) , 
54    fs  =  require ( 'fs' ) , 
65    gutil  =  require ( 'gulp-util' ) , 
76    mustache  =  require ( 'mustache' ) , 
@@ -16,13 +15,14 @@ var PLUGIN_NAME = 'gulp-svg-spritesheet';
1615var  defaults  =  { 
1716    cssPathNoSvg : '' ,  // Leave blank if you dont want to specify a fallback 
1817    cssPathSvg : './test.svg' ,  // CSS path to generated SVG 
19-     demoDest : '' ,  // Leave blank if you don't want a demo file 
20-     demoSrc : '../ demo.tpl',  // The souce or the demo template 
18+     demoDest : './sprite.html ' ,  // Leave blank if you don't want a demo file 
19+     demoSrc : path . resolve ( __dirname ,   ' demo.tpl') ,  // The souce or the demo template 
2120    padding : 0 ,  // Add some padding between sprites 
2221    pixelBase : 16 ,  // Used to calculate em/rem values 
2322    positioning : 'vertical' ,  // vertical, horizontal, diagonal or packed 
24-     templateSrc : '../template.tpl' ,  // The source of the CSS template 
25-     templateDest : './sprite.scss' ,  
23+     templateSrc : path . resolve ( __dirname ,  'template.tpl' ) ,  // The source of the CSS template 
24+     templateDest : './sprite.scss' , 
25+     svgDest : './sprite.svg' , 
2626    units : 'px' ,  // px, em or rem 
2727    x : 0 ,  // Starting X position 
2828    y : 0  // Starting Y position 
@@ -55,7 +55,7 @@ var sort = {
5555
5656// This is where the magic happens 
5757var  spriteSVG  =  function ( options )  { 
58-      
58+ 
5959    options  =  options  ||  { } ; 
6060
6161    // Extend our defaults with any passed options 
@@ -74,45 +74,43 @@ var spriteSVG = function(options) {
7474            units : options . units , 
7575            width : 0 
7676        } , 
77-         eventEmitter  =  new  events . EventEmitter ( ) , 
78-         self , 
7977        x  =  options . x , 
8078        y  =  options . y ; 
8179
82-     // When a template file is loaded, render it 
83-     eventEmitter . on ( "loadedTemplate" ,  renderTemplate ) ; 
84- 
8580    // Generate relative em/rem untis from pixels 
8681    function  pxToRelative ( value )  { 
8782        return  value  /  options . pixelBase ; 
8883    } 
8984
9085    // Load a template file and then render it 
9186    function  loadTemplate ( src ,  dest )  { 
92-         fs . readFile ( src ,  function ( err ,  contents )  { 
93-             if ( err )  { 
94-                 new  gutil . PluginError ( PLUGIN_NAME ,  err ) ; 
95-             } 
96- 
97-             var  file  =  { 
98-                 contents : contents . toString ( ) , 
99-                 data : data , 
100-                 dest : dest 
101-             } ; 
102- 
103-             eventEmitter . emit ( "loadedTemplate" ,  file ) ; 
104-         } ) ; 
87+         var  self  =  this ; 
88+         var  src  =  path . resolve ( src ) ; 
89+         var  dest  =  path . resolve ( dest ) ; 
90+ 
91+         try  { 
92+             var  contents  =  fs . readFileSync ( src ,  'utf8' ) ; 
93+             var  compiled  =  mustache . render ( contents ,  data ) ; 
94+             var  file  =  new  gutil . File ( { 
95+                 path : dest , 
96+                 contents : new  Buffer ( compiled ) 
97+             } ) ; 
98+ 
99+             self . push ( file ) ; 
100+         }  catch  ( err )  { 
101+             throw  new  gutil . PluginError ( PLUGIN_NAME ,  err ) ; 
102+         } 
105103    } 
106104
107-     // Position sprites using Jake Gordon's bin packing algorithm   
105+     // Position sprites using Jake Gordon's bin packing algorithm 
108106    // https://github.com/jakesgordon/bin-packing 
109-     function  packSprites ( cb )  { 
107+     function  packSprites ( )  { 
110108        var  packer  =  new  GrowingPacker ( ) ; 
111109
112110        // Get coordinates of sprites 
113111        packer . fit ( data . sprites ) ; 
114112
115-         // For each sprite   
113+         // For each sprite 
116114        for  ( var  i  in  data . sprites )  { 
117115            var  sprite  =  data . sprites [ i ] , 
118116                // Create, initialise and populate an SVG 
@@ -152,14 +150,10 @@ var spriteSVG = function(options) {
152150
153151            // Add the SVG to the sprite sheet 
154152            $sprite . append ( $svg ) ; 
155- 
156153        } 
157- 
158-         // Save the sprite sheet 
159-         saveSpriteSheet ( cb ) ; 
160154    } 
161155
162-     function  positionSprites ( cb )  { 
156+     function  positionSprites ( )  { 
163157        // For each sprite  
164158        for  ( var  i  in  data . sprites )  { 
165159
@@ -212,14 +206,10 @@ var spriteSVG = function(options) {
212206                sprite . x  =  pxToRelative ( sprite . x ) ; 
213207                sprite . y  =  pxToRelative ( sprite . y ) ; 
214208            } 
215-              
209+ 
216210            // Add the SVG to the sprite sheet 
217211            $sprite . append ( $svg ) ; 
218- 
219212        } 
220- 
221-         // Save the sprite sheet 
222-         saveSpriteSheet ( cb ) ; 
223213    } 
224214
225215    function  processSVG ( file ,  encoding ,  cb )  { 
@@ -232,56 +222,55 @@ var spriteSVG = function(options) {
232222        if  ( file . isStream ( ) )  { 
233223            return  cb ( new  gutil . PluginError ( PLUGIN_NAME ,  'Streams are not supported' ) ) ; 
234224        } 
225+         console . log ( file . path ) ; 
235226            // We're using the filename as the CSS class name 
236227        var  filename  =  path . basename ( file . relative ,  path . extname ( file . relative ) ) , 
237228            // Load the file contents 
238229            $file  =  cheerio . load ( file . contents . toString ( 'utf8' ) ,  { xmlMode : true } ) ( 'svg' ) , 
239-             viewBox  =  $file . attr ( 'viewBox' ) , 
240-             coords  =  viewBox . split ( " " ) ; 
241- 
242-         // Set sprite data to be used by the positioning function 
243-         var  sprite  =  { 
244-                 fileName : filename , 
245-                 file : $file . contents ( ) , 
246-                 h : parseFloat ( coords [ 3 ] ) , 
247-                 padding : options . padding , 
248-                 // Round up coordinates to avoid chopping off edges 
249-                 viewBox : Math . ceil ( coords [ 0 ] ) + " " + Math . ceil ( coords [ 1 ] ) + " " + Math . ceil ( coords [ 2 ] ) + " " + Math . ceil ( coords [ 3 ] ) , 
250-                 w : parseFloat ( coords [ 2 ] ) 
251-             } ; 
252- 
253-         // Add the sprite to our array 
254-         data . sprites . push ( sprite ) ; 
230+             viewBox  =  $file . attr ( 'viewBox' ) ; 
231+ 
232+         if  ( viewBox )  { 
233+             var  coords  =  viewBox . split ( " " ) ; 
234+ 
235+             // Set sprite data to be used by the positioning function 
236+             var  sprite  =  { 
237+                     fileName : filename , 
238+                     file : $file . contents ( ) , 
239+                     h : parseFloat ( coords [ 3 ] ) , 
240+                     padding : options . padding , 
241+                     // Round up coordinates to avoid chopping off edges 
242+                     viewBox : Math . ceil ( coords [ 0 ] ) + " " + Math . ceil ( coords [ 1 ] ) + " " + Math . ceil ( coords [ 2 ] ) + " " + Math . ceil ( coords [ 3 ] ) , 
243+                     w : parseFloat ( coords [ 2 ] ) 
244+                 } ; 
245+ 
246+             // Add the sprite to our array 
247+             data . sprites . push ( sprite ) ; 
248+         }  else  { 
249+            gutil . log ( 'Warning: svg "'  +  file . path  +  '" has no viewBox, not adding it to sprite' ) ;     
250+         } 
255251
256252        // Move on to processSprites() 
257253        cb ( ) ; 
258254    } 
259255
260256    function  processSprites ( cb )  { 
261-         // Save this for referencing in positioning functions 
262-         self  =  this ; 
263257        // Sort the sprites so the biggest are first to avoid this issue: 
264258        // https://github.com/jakesgordon/bin-packing/blob/master/js/packer.growing.js#L10 
265259        data . sprites . sort ( sort . maxside ) ; 
266- 
267-         // Lay out the sprites  
260+         // Lay out the sprites 
268261        if ( options . positioning === 'packed' )  { 
269262            packSprites ( cb ) ; 
270263        }  else  { 
271264            positionSprites ( cb ) ; 
272265        } 
273-     } 
274- 
275-     // Render our template and then save the file 
276-     function  renderTemplate ( file )  { 
277-         var  compiled  =  mustache . render ( file . contents ,  file . data ) ; 
278266
279-         fs . writeFile ( file . dest ,  compiled ) ; 
267+         // Save the sprite sheet 
268+         saveSpriteSheet . call ( this ,  cb ) ; 
280269    } 
281270
282-     // Final processing of sprite sheet then we return file to gulp pipe   
271+     // Final processing of sprite sheet then we return file to gulp pipe 
283272    function  saveSpriteSheet ( cb )  { 
284-         // Add padding to even edges up   
273+         // Add padding to even edges up 
285274        data . height += options . padding ; 
286275        data . width += options . padding ; 
287276
@@ -302,23 +291,26 @@ var spriteSVG = function(options) {
302291            data . height  =  pxToRelative ( data . height ) ; 
303292            data . width  =  pxToRelative ( data . width ) ; 
304293        } 
305- 
306-         // Save our CSS template file    
307-         loadTemplate ( options . templateSrc ,  options . templateDest ) ; 
294+         // Save our CSS template file 
295+         loadTemplate . call ( this ,  options . templateSrc ,  options . templateDest ) ; 
308296
309297        // If a demo file is required, save that too 
310298        if ( options . demoDest )  { 
311-             loadTemplate ( options . demoSrc ,  options . demoDest ) ; 
299+             loadTemplate . call ( this ,   options . demoSrc ,  options . demoDest ) ; 
312300        } 
313301
314302        // Create a file to pipe back to gulp 
315-         var  file  =  new  gutil . File ( { path : './' ,  contents : new  Buffer ( $ . xml ( ) ) } ) ; 
303+         var  file  =  new  gutil . File ( { 
304+             path : path . resolve ( options . svgDest ) , 
305+             contents : new  Buffer ( $ . xml ( ) ) 
306+         } ) ; 
316307
317308        // Pipe it baby! 
318-         self . push ( file ) ; 
309+         this . push ( file ) ; 
310+         cb ( ) ; 
319311    } 
320312
321313    return  through2 . obj ( processSVG ,  processSprites ) ; 
322314} ; 
323315
324- module . exports  =  spriteSVG ; 
316+ module . exports  =  spriteSVG ; 
0 commit comments