@sctg/fontminify
Version:
Minify font seamlessly, font subsetter, webfont (eot, woff, svg) converter.
151 lines (150 loc) • 4.16 kB
JavaScript
/**
* @file fontmin
* @author junmer eltorio
*/
import combine from "stream-combiner";
import concat from "concat-stream";
import { EventEmitter } from "events";
import bufferToVinyl from "buffer-to-vinyl";
import * as Vfs from "vinyl-fs";
import _css from "./plugins/css.js";
import _glyph from "./plugins/glyph.js";
import _otf2ttf from "./plugins/otf2ttf.js";
import _svg2ttf from "./plugins/svg2ttf.js";
import _svgs2ttf from "./plugins/svgs2ttf.js";
import _ttf2eot from "./plugins/ttf2eot.js";
import _ttf2svg from "./plugins/ttf2svg.js";
import _ttf2woff from "./plugins/ttf2woff.js";
import _ttf2woff2 from "./plugins/ttf2woff2.js";
// import * as File from 'vinyl'
import { getFontFolder as _getFontFolder, getFonts as _getFonts, getPureText as _getPureText, getUniqText as _getUniqText, getSubsetText as _getSubsetText, string2unicodes as _string2unicodes, } from "./lib/util.js";
const vfs = Vfs.default;
;
/**
* Initialize Fontminify
*/
class Fontminify extends EventEmitter {
constructor() {
super();
if (!(this instanceof Fontminify)) {
return new Fontminify();
}
this.streams = [];
}
/**
* Get or set the source files
*/
src(...file) {
if (!file.length) {
return this._src;
}
this._src = file;
return this;
}
/**
* Get or set the destination folder
*/
dest(...dir) {
if (!arguments.length) {
return this._dest;
}
this._dest = dir;
return this;
}
/**
* Add a plugin to the middleware stack
*/
use(plugin) {
this.streams.push(typeof plugin === "function" ? plugin() : plugin);
return this;
}
/**
* Optimize files
*/
run(cb) {
cb =
cb ||
(() => {
return;
});
const stream = this.createStream();
stream.on("error", cb);
stream.pipe(concat(cb.bind(null, null)));
return stream;
}
/**
* Create stream
*
* @return {Stream} file stream
* @api private
*/
createStream() {
this.streams.unshift(this.getFiles());
if (this.streams.length === 1) {
this.use(Fontminify.otf2ttf());
this.use(Fontminify.ttf2eot());
this.use(Fontminify.ttf2woff());
this.use(Fontminify.ttf2woff2());
this.use(Fontminify.ttf2svg());
this.use(Fontminify.css());
}
if (this.dest()) {
this.streams.push(vfs.dest(...this.dest()));
}
return combine(this.streams);
}
/**
* Get files
*
* @return {Stream} file stream
* @api private
*/
getFiles() {
if (Buffer.isBuffer(this._src[0])) {
return bufferToVinyl.stream(this._src[0]);
}
return vfs.src(...this.src());
}
}
Fontminify.glyph = (opts) => _glyph(opts);
Fontminify.ttf2eot = (opts) => _ttf2eot(opts);
Fontminify.ttf2woff = (opts) => _ttf2woff(opts);
Fontminify.ttf2woff2 = (opts) => _ttf2woff2(opts);
Fontminify.ttf2svg = (opts) => _ttf2svg(opts);
Fontminify.css = (opts) => _css(opts);
Fontminify.svg2ttf = (opts) => _svg2ttf(opts);
Fontminify.svgs2ttf = (file, opts) => _svgs2ttf(file, opts);
Fontminify.otf2ttf = (opts) => _otf2ttf(opts);
Fontminify.util = {
getFontFolder: () => _getFontFolder(),
getFonts: () => _getFonts(),
getPureText: (str) => _getPureText(str),
getUniqText: (str) => _getUniqText(str),
getSubsetText: (opts) => _getSubsetText(opts),
string2unicodes: (str) => _string2unicodes(str),
};
Fontminify.plugins = [
"glyph",
"ttf2eot",
"ttf2woff",
"ttf2woff2",
"ttf2svg",
"css",
"svg2ttf",
"svgs2ttf",
"otf2ttf",
];
Fontminify.mime = {
".*": "application/octet-stream",
ttf: "application/font-sfnt",
otf: "application/font-sfnt",
woff: "application/font-woff",
woff2: "application/font-woff2",
eot: "application/octet-stream",
svg: "image/svg+xml",
svgz: "image/svg+xml",
};
/**
* Module exports
*/
export default Fontminify;