-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
136 lines (128 loc) · 3.85 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
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
130
131
132
133
134
135
136
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var path = require('path');
var layout = require('layout');
var _ = require('lodash');
var ejs = require('ejs');
var imageLib = require('./lib/image');
var layerLib = require('./lib/layer-info');
var autoloader = require('./lib/autoloader');
var extentions = [
'.png'
];
var PLUGIN_NAME = 'gulp-spritegen';
var templates = autoloader(path.join(__dirname, './lib/templates'));
module.exports = function (config) {
var defaultConfig = {
engine: 'json',
algorithm: 'binary-tree',
ratio: 1,
gutter: 0,
spriteImg: 'sprite',
spriteMeta: 'sprite'
};
var options = _.defaults(config, defaultConfig);
if (!_.isArray(options.ratio)) {
options.ratio = [options.ratio];
}
options.ratio = options.ratio.sort();
if (options.ratio[0]!==1) options.ratio.unshift(1);
var images = [];
var dir = '';
var cwd = '';
var stream = through.obj(
function (file, enc, cb) {
if (file.isNull()) return cb();
if (file.isStream()) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Streams are not supported')
);
return cb();
}
if (-1 == extentions.indexOf(path.extname(file.path))) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Invalid type of file: ' + file.path)
);
return cb();
}
cwd = file.cwd;
dir = file.base;
var image = imageLib.getImageObject(file);
if (_.isNull(image)) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Invalid file format: ' + file.path)
);
return cb();
}
images.push(imageLib.getImageObject(file));
cb();
},
function (cb) {
var self = this;
var parser = layerLib();
if (!images.length) return cb();
options.ratio.forEach(function (ratio) {
var layer = layout(options.algorithm);
images.forEach(function (image) {
var layerItem = {
meta: {
name: image.name,
content: image.content,
ratio: ratio,
gutter: options.gutter * ratio
},
width: (image.size.width + 2 * options.gutter) * ratio,
height: (image.size.height + 2 * options.gutter) * ratio
};
if ((layerItem.width - 2 * layerItem.meta.gutter) > image.content.width ||
(layerItem.height - 2 * layerItem.meta.gutter) > image.content.height) {
return;
}
layer.addItem(layerItem);
});
var layerInfo = layer['export']();
var outContent = imageLib.generateOutput(layerInfo);
if (outContent) {
parser.addLayerInfo(layerInfo, options.spriteImg);
self.push(new gutil.File({
cwd: cwd,
base: dir,
path: path.join(dir, options.spriteImg + '@' + ratio + 'x.png'),
contents: outContent
}));
}
});
if (_.isFunction(options.engine)) {
var engineRes = options.engine(parser.result);
if (engineRes) {
self.push(engineRes);
}
return cb();
} else {
var template;
if (templates[options.engine]) {
template = templates[options.engine];
} else {
// TODO check file path
template = options.engine;
}
var ext = path.extname(template);
ejs.renderFile(template, {result: parser.result}, function(err, content){
if (err) {
return cb(new PluginError(PLUGIN_NAME, err));
}
self.push(new gutil.File({
cwd: cwd,
base: dir,
path: path.join(dir, options.spriteMeta + ext),
contents: new Buffer(content)
}));
cb();
});
}
}
);
return stream;
};