-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (41 loc) · 1.26 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
const ttf2woff2 = require("ttf2woff2");
const PluginError = require("plugin-error");
const Transform = require("stream").Transform;
const path = require("path");
const PLUGIN_NAME = "gulp-ttftowoff2";
module.exports = (options = {}) => {
return new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
// Return if empty or null
if (chunk._contents.length === 0 || chunk.isNull()) {
return callback(null, chunk);
}
// Return if not ttf extension
if (path.extname(chunk.path) !== ".ttf") {
return callback(null, chunk);
}
// Rename extension
chunk.path = path.join(
path.dirname(chunk.path),
path.basename(chunk.path, path.extname(chunk.path)) + ".woff2"
);
if (chunk.isBuffer()) {
try {
chunk._contents = Buffer.from(
ttf2woff2(chunk._contents, options).buffer
);
return callback(null, chunk);
} catch (error) {
return callback(
new PluginError(PLUGIN_NAME, error, { fileName: chunk.relative })
);
}
} else if (chunk.isStream()) {
return callback(
new PluginError(PLUGIN_NAME, "Streaming is not supported.")
);
}
},
});
};