-
Notifications
You must be signed in to change notification settings - Fork 1
/
Minifier.js
161 lines (133 loc) · 5.18 KB
/
Minifier.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var glob = require('glob');
var DataHolder = require('raptor-async/DataHolder');
var series = require('raptor-async/series');
var vinylToStream = require('vinyl-to-stream');
var Imagemin = require('imagemin');
var nodePath = require('path');
function streamToBuffer(stream, callback) {
var chunks = [];
stream
.on('data', function(chunk) {
chunks.push(chunk);
})
.on('error', function(err) {
return callback(err);
})
.on('end', function() {
var buffer = Buffer.concat(chunks); // Create a buffer from all the received chunks
return callback(null, buffer);
});
}
var builtins = {
'imagemin-gifsicle': Imagemin.gifsicle,
'imagemin-jpegtran': Imagemin.jpegtran,
'imagemin-optipng': Imagemin.optipng,
'imagemin-pngquant': Imagemin.pngquant,
'imagemin-svgo': Imagemin.svgo,
'gifsicle': Imagemin.gifsicle,
'jpegtran': Imagemin.jpegtran,
'optipng': Imagemin.optipng,
'pngquant': Imagemin.pngquant,
'svgo': Imagemin.svgo
};
function Minifier(pluginConfig) {
var initDataHolder = new DataHolder();
var pluginsByPath = {};
var basedir = process.cwd();
function init() {
var useArray = pluginConfig.use;
if (Array.isArray(useArray)) {
var tasks = useArray.map(function(use) {
return function(callback) {
var plugin = use.plugin;
var pluginName;
if (typeof plugin === 'string') {
pluginName = plugin;
plugin = builtins[plugin] || require(plugin);
}
if (!pluginName) {
pluginName = plugin.name;
}
var options = use.options || {};
var patterns = use.paths;
if (typeof patterns === 'string') {
patterns = [patterns];
}
var paths = {};
series(patterns.map(function(pattern) {
return function(callback) {
glob(pattern, { cwd: basedir}, function (err, files) {
if (err) {
return callback(err);
}
for (var i=0, len=files.length; i<len; i++) {
var path = files[i];
path = nodePath.join(basedir, path);
paths[path] = true;
}
callback();
});
};
}),
function(err) {
if (err) {
return initDataHolder.reject(err);
}
Object.keys(paths).forEach(function(path) {
var pluginsForPath = pluginsByPath[path] ||
(pluginsByPath[path] = []);
pluginsForPath.push({
plugin: plugin,
options: options
});
});
callback();
});
};
});
series(tasks, function(err) {
if (err) {
return initDataHolder.reject(err);
}
initDataHolder.resolve();
});
} else {
initDataHolder.resolve();
}
}
init();
this.minify = function(inStream, path, lassoContext) {
var readable = lassoContext.deferredStream(function() {
initDataHolder.done(function() {
var pluginsForPath = pluginsByPath[path];
if (!pluginsForPath || pluginsForPath.length === 0) {
readable.setWrappedStream(inStream);
return;
}
streamToBuffer(inStream, function(err, buffer) {
if (err) {
readable.emit('error', err);
return;
}
var imageReadable = vinylToStream();
var imagemin = new Imagemin()
.src(buffer);
pluginsForPath.forEach(function(pluginConfig) {
imagemin.use(pluginConfig.plugin(pluginConfig.options));
});
imagemin.use(imageReadable);
imagemin.run(function(err) {
if (err) {
readable.emit('error', err);
imageReadable.push(null);
return;
}
});
readable.setWrappedStream(imageReadable);
});
});
});
return readable;
};
}
module.exports = Minifier;