forked from imagemin/imagemin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (134 loc) · 2.83 KB
/
index.js
File metadata and controls
169 lines (134 loc) · 2.83 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
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
161
162
163
164
165
166
167
168
169
'use strict';
var fs = require('fs-extra');
var mode = require('stat-mode');
var Ware = require('ware');
/**
* Initialize Imagemin
*
* @api public
*/
function Imagemin() {
this.ware = new Ware();
}
/**
* Add a plugin to the middleware stack
*
* @param {Function} plugin
* @api public
*/
Imagemin.prototype.use = function (plugin) {
this.ware.use(plugin);
return this;
};
/**
* Get or set the source file
*
* @param {String|Buffer} file
* @api public
*/
Imagemin.prototype.src = function (file) {
if (!arguments.length) {
return this._src;
}
this._src = file;
return this;
};
/**
* Get or set the destination file
*
* @param {String} file
* @api public
*/
Imagemin.prototype.dest = function (file) {
if (!arguments.length) {
return this._dest;
}
this._dest = file;
return this;
};
/**
* Optimize file
*
* @param {Function} cb
* @api public
*/
Imagemin.prototype.optimize = function (cb) {
cb = cb || function () {};
var self = this;
this.read(function (err, file) {
var buf = file.contents;
self.run(file, function (err, file) {
if (err) {
return cb(err);
}
if (file.contents.length >= buf.length) {
file.contents = buf;
}
self.write(file, function (err) {
cb(err, file);
});
});
});
};
/**
* Run a file through the middleware
*
* @param {Array} file
* @param {Function} cb
* @api public
*/
Imagemin.prototype.run = function (file, cb) {
this.ware.run(file, this, cb);
};
/**
* Read the source file
*
* @param {Function} cb
* @api public
*/
Imagemin.prototype.read = function (cb) {
var file = {};
var src = this.src();
if (Buffer.isBuffer(src)) {
file.contents = src;
return cb(null, file);
}
fs.readFile(src, function (err, buf) {
if (err) {
return cb(err);
}
fs.stat(src, function (err, stats) {
if (err) {
return cb(err);
}
file.contents = buf;
file.mode = mode(stats).toOctal();
cb(null, file);
});
});
};
/**
* Write file to destination
*
* @param {Object} file
* @param {Function} cb
* @api public
*/
Imagemin.prototype.write = function (file, cb) {
var dest = this.dest();
if (!dest) {
return cb();
}
fs.outputFile(dest, file.contents, function (err) {
cb(err);
});
};
/**
* Module exports
*/
module.exports = Imagemin;
module.exports.gifsicle = require('imagemin-gifsicle');
module.exports.jpegtran = require('imagemin-jpegtran');
module.exports.optipng = require('imagemin-optipng');
module.exports.pngquant = require('imagemin-pngquant');
module.exports.svgo = require('imagemin-svgo');