-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (111 loc) · 3.15 KB
/
index.js
File metadata and controls
130 lines (111 loc) · 3.15 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var path = require('path')
var thr = require('through2')
var mix = require('mixy')
var util = require('util')
var merge = require('merge-stream')
var spritesmith = require('gulp.spritesmith')
var builtin = require('./lib/builtin')
exports = module.exports = gulpSpritesmith
exports.util = require('./lib/util')
exports.builtin = builtin
function gulpSpritesmith(opts) {
opts = opts || {}
var groups = {}
var output = thr.obj(write, end)
var wait = thr.obj()
var imgStream = output.img = merge(wait)
var cssStream = output.css = merge(wait)
function to(file) {
if (typeof opts.to === 'string') {
return opts.to
}
if (typeof opts.to === 'function') {
return opts.to(file)
}
return path.basename(
path.dirname(file)
)
}
function write(file, _, next) {
var sprite = to(file.path)
groups[sprite] = groups[sprite] || []
groups[sprite].push(file)
next()
}
function end(done) {
wait.end()
var sprites = Object.keys(groups)
if (sprites.length === 0) {
return done()
}
sprites.forEach(function (sprite) {
var icons = groups[sprite]
var options = filterSmithOption(
createSmithOption(sprite, icons),
opts.spritesmith, sprite, icons
)
var gulpSpriteStream = spritesmith(options)
imgStream.add(gulpSpriteStream.img)
cssStream.add(gulpSpriteStream.css)
gulpSpriteStream.on('error', errorHandler(output, sprite))
gulpSpriteStream.img.on('error', errorHandler(imgStream, sprite))
gulpSpriteStream.css.on('error', errorHandler(cssStream, sprite))
icons.forEach(function (icon) {
gulpSpriteStream.write(icon)
})
gulpSpriteStream.end()
})
merge(imgStream, cssStream)
.on('error', done)
.on('data', function (file) {
output.push(file)
})
.on('end', done)
}
return output
}
function errorHandler(target, sprite) {
return function (err) {
if (!/Retina settings detected/.test(err.message)) {
return target.emit('error', err)
}
var message = util.format(
'Unmatched retina images found when creating sprite `%s`. %d retina images expected to line up against %d normal images.',
sprite,
err.retinaImages.length,
err.images.length
)
var error = new Error(message)
error['normal images'] = err.images.map(function (img) {
return img.path
})
error['retina images'] = err.retinaImages.map(function (img) {
return img.path
})
target.emit('error', error)
}
}
function hasRetinaIcon(icons) {
return icons.some(function (icon) {
return /@2x.png/.test(icon.path)
})
}
function createSmithOption(sprite, icons) {
var options = {
imgName: sprite + '.png',
cssName: sprite + '.css',
cssTemplate: builtin.css,
cssSpritesheetName: 'sp-' + sprite,
}
if (hasRetinaIcon(icons)) {
options.retinaSrcFilter = '**/*@2x.png'
options.retinaImgName = sprite + '@2x.png'
}
return options
}
function filterSmithOption(options, filter, sprite, icons) {
if (typeof filter === 'function') {
return filter(options, sprite, icons) || options
}
return mix(options, filter)
}