Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (132 loc) · 2.86 KB
/
Copy pathindex.js
File metadata and controls
155 lines (132 loc) · 2.86 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
/**
* @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 = file;
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 = dir;
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;
};
/**
* 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.ttf2eot());
this.use(Fontmin.ttf2woff());
this.use(Fontmin.ttf2svg());
this.use(Fontmin.css());
}
if (this.dest()) {
this.streams.push(vfs.dest(this.dest()));
}
return combine(this.streams);
};
/**
* Get files
*
* @return {Stream} file stream
* @api private
*/
Fontmin.prototype.getFiles = function () {
if (Buffer.isBuffer(this.src())) {
return bufferToVinyl.stream(this.src());
}
return vfs.src(this.src());
};
/**
* Module exports
*/
module.exports = Fontmin;
// export pkged plugins
[
'glyph',
'ttf2eot',
'ttf2woff',
'ttf2svg',
'css',
'svg2ttf',
'svgs2ttf'
].forEach(function (plugin) {
module.exports[plugin] = require('./plugins/' + plugin);
});
// exports util, mime
module.exports.util = exports.util = require('./lib/util');
module.exports.mime = exports.mime = require('./lib/mime-types');