-
Notifications
You must be signed in to change notification settings - Fork 317
/
index.js
186 lines (158 loc) · 3.44 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @file fontmin
* @author junmer
*/
/* eslint-env node */
var combine = require('stream-combiner');
var concat = require('concat-stream');
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var bufferToVinyl = require('buffer-to-vinyl');
var vfs = require('vinyl-fs');
/**
* Initialize Fontmin
*
* @constructor
* @api public
*/
function Fontmin() {
if (!(this instanceof Fontmin)) {
return new Fontmin();
}
EventEmitter.call(this);
this.streams = [];
}
/**
* Inherit from `EventEmitter`
* @type {Class}
*/
inherits(Fontmin, EventEmitter);
/**
* Get or set the source files
*
* @param {Array|Buffer|string} file files to be optimized
* @return {Object} fontmin
* @api public
*/
Fontmin.prototype.src = function (file) {
if (!arguments.length) {
return this._src;
}
this._src = arguments;
return this;
};
/**
* Get or set the destination folder
*
* @param {string} dir folder to written
* @return {Object} fontmin
* @api public
*/
Fontmin.prototype.dest = function (dir) {
if (!arguments.length) {
return this._dest;
}
this._dest = arguments;
return this;
};
/**
* Add a plugin to the middleware stack
*
* @param {Function} plugin plugin
* @return {Object} fontmin
* @api public
*/
Fontmin.prototype.use = function (plugin) {
this.streams.push(typeof plugin === 'function' ? plugin() : plugin);
return this;
};
/**
* Optimize files
*
* @param {Function} cb callback
* @return {Stream} file stream
* @api public
*/
Fontmin.prototype.run = function (cb) {
cb = cb || function () {};
var stream = this.createStream();
stream.on('error', cb);
stream.pipe(concat(cb.bind(null, null)));
return stream;
};
/**
* run Optimize files with return Promise
*
* @return {Array<Buffer>} file result
* @api public
*/
Fontmin.prototype.runAsync = function () {
return new Promise((resolve, reject) => {
var stream = this.createStream();
stream.on('error', reject);
stream.pipe(concat(resolve));
});
};
/**
* Create stream
*
* @return {Stream} file stream
* @api private
*/
Fontmin.prototype.createStream = function () {
this.streams.unshift(this.getFiles());
if (this.streams.length === 1) {
this.use(Fontmin.otf2ttf());
this.use(Fontmin.ttf2eot());
this.use(Fontmin.ttf2woff());
this.use(Fontmin.ttf2woff2());
this.use(Fontmin.ttf2svg());
this.use(Fontmin.css());
}
if (this.dest()) {
this.streams.push(
vfs.dest.apply(vfs, this.dest())
);
}
return combine(this.streams);
};
/**
* Get files
*
* @return {Stream} file stream
* @api private
*/
Fontmin.prototype.getFiles = function () {
if (Buffer.isBuffer(this._src[0])) {
return bufferToVinyl.stream(this._src[0]);
}
return vfs.src.apply(vfs, this.src());
};
/**
* plugins
*
* @type {Array}
*/
Fontmin.plugins = [
'glyph',
'ttf2eot',
'ttf2woff',
'ttf2woff2',
'ttf2svg',
'css',
'svg2ttf',
'svgs2ttf',
'otf2ttf'
];
// export pkged plugins
Fontmin.plugins.forEach(function (plugin) {
Fontmin[plugin] = require('./plugins/' + plugin);
});
/**
* Module exports
*/
module.exports = Fontmin;
// exports util, mime
module.exports.default = Fontmin;
module.exports.util = exports.util = require('./lib/util');
module.exports.mime = exports.mime = require('./lib/mime-types');