google-fonts-plugin
Version:
Webpack plugin that downloads fonts from Google Fonts and encodes them to base64.
76 lines • 3.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const pkgUp = require("pkg-up");
const Plugin_1 = require("../Plugin/Plugin");
const FontDisplay_1 = require("./FontDisplay");
class Options {
constructor(input, initialize = true) {
this.fonts = [
{
family: 'Roboto',
variants: ['400', '400i', '700', '700i'],
subsets: ['latin', 'latin-ext'],
text: ''
}
];
this.formats = ['woff', 'woff2'];
this.formatAgents = {
eot: 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)',
ttf: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.8 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.8',
woff: 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko',
woff2: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393'
};
this.chunkName = 'google-fonts';
this.filename = '[name].css';
this.fontDisplay = FontDisplay_1.default.SWAP;
this.encode = true;
this.cache = true;
this.file = '';
this.stats = true;
this.get = () => {
let parsedOptions = {};
if (typeof this.input === 'object') {
parsedOptions = this.input;
}
else if (typeof this.input === 'string' && this.input.length !== 0) {
parsedOptions = this.getFromFile(this.input);
}
else {
parsedOptions = this.getFromPackage();
}
Object.assign(this, parsedOptions);
return parsedOptions;
};
this.getFromFile = (input) => {
this.file = input;
const fileContents = fs_1.readFileSync(this.file, 'utf8');
return this.crawl(JSON.parse(fileContents));
};
this.getFromPackage = () => this.getFromFile(pkgUp.sync() || '');
this.crawl = (options) => {
let result = {};
for (const key of Object.keys(options)) {
if (key === Plugin_1.default.getPluginName()) {
return options[key];
}
else if (options[key] instanceof Object && Object.keys(options[key]).length !== 0) {
result = this.crawl(options[key]);
if (Object.entries(result).length > 0) {
break;
}
}
}
return result;
};
this.input = input;
if (initialize) {
this.get();
}
}
getAgent(format) {
return this.formatAgents[format];
}
}
exports.default = Options;
//# sourceMappingURL=Options.js.map