Skip to content

Commit 3631b27

Browse files
Darren Hallblai
authored andcommitted
Add .editorconfig file
1 parent 2db160d commit 3631b27

File tree

4 files changed

+89
-79
lines changed

4 files changed

+89
-79
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# 4 space indentation
12+
[*]
13+
indent_style = space
14+
indent_size = 4

index.js

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
var 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';
1615
var 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
5757
var 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;

package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,27 @@
44
"version": "0.0.2",
55
"author": "Darren Hall <[email protected]>",
66
"license": "MIT",
7-
87
"repository": {
98
"type": "git",
109
"url": "https://github.com/iamdarrenhall/gulp-svg-spritesheet.git"
1110
},
12-
1311
"homepage": "https://github.com/iamdarrenhall/gulp-svg-spritesheet",
1412
"bugs": {
1513
"url": "https://github.com/iamdarrenhall/gulp-svg-spritesheet/issues"
1614
},
17-
1815
"keywords": [
1916
"gulpplugin",
2017
"sprite",
2118
"svg"
2219
],
23-
2420
"engines": {
2521
"node": ">=0.10.28"
2622
},
27-
2823
"devDependencies": {
2924
"gulp": "^3.8.9",
3025
"gulp-jshint": "^1.9.0",
3126
"gulp-svgmin": "^0.4.7"
3227
},
33-
3428
"dependencies": {
3529
"cheerio": "^0.17.0",
3630
"gulp-util": "^3.0.1",

readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ gulp.task('default', function () {
2020
templateSrc: 'sass.tpl',
2121
templateDest: 'sass/sprite.scss'
2222
}))
23-
.pipe(gulp.dest('images/sprite.svg'));
23+
.pipe(gulp.dest('images'));
24+
// this will write the following files
25+
// images/sprite.scss
26+
// images/sprint.svg
2427
});
2528
```
2629

@@ -35,6 +38,7 @@ gulp.task('default', function () {
3538
padding: 10,
3639
pixelBase: 16,
3740
positioning: 'diagonal',
41+
svgDest: './sprinte.svg',
3842
templateSrc: 'sass.tpl',
3943
templateDest: 'sass/sprite.scss',
4044
units: 'em'
@@ -62,6 +66,12 @@ Default: `./test.svg`
6266

6367
CSS `background-image` path which will be used in the `templateDest` file. The path should be relative to that final destination file.
6468

69+
####svgDest
70+
Type: `string`
71+
Default: `./sprite.svg`
72+
73+
The destination of the generated .svg file. If relative path is used, it will be set to `path.resolve('./some/relative/path.svg')`.
74+
6575
####demoDest
6676
Type: `string`
6777
Default: `<empty>`

0 commit comments

Comments
 (0)