UNPKG

gulp-font2style

Version:

Encode font files as stylesheet using Gulp.

50 lines (49 loc) 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var path_1 = __importDefault(require("path")); var through2_1 = __importDefault(require("through2")); var plugin_error_1 = __importDefault(require("plugin-error")); var util_1 = require("./util"); var DEFAULT_OPTS = { fontWeight: 'normal', fontStyle: 'normal', }; function font2style(opts) { if (opts === void 0) { opts = {}; } var options = Object.assign({}, DEFAULT_OPTS, opts); return through2_1.default.obj(function (file, enc, callback) { if (file.isNull()) { this.push(file); return callback(); } if (file.isStream()) { this.emit('error', new plugin_error_1.default({ plugin: 'gulp-font2style', message: 'Streaming is not supported', })); return callback(); } if (file.isBuffer()) { if (!options.basename) { options.basename = path_1.default.basename(file.path); if (!options.fontFamily) { options.fontFamily = path_1.default.basename(file.path, path_1.default.extname(file.path)); } } var attributes = [ util_1.getFontFamily(options.fontFamily), util_1.getFontWeight(options.fontWeight), util_1.getFontStyle(options.fontStyle), util_1.getSrc(file), ]; var contents = "@font-face{" + attributes.join('') + "}"; file.contents = Buffer.from(contents); file.basename = options.basename; return callback(null, file); } }); } exports.default = font2style;