UNPKG

9.51 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function (initialOptions) {
8 if (!initialOptions || !initialOptions.files) {
9 throw new Error("You must pass webfont a `files` glob");
10 }
11
12 let options = Object.assign({}, {
13 ascent: undefined, // eslint-disable-line no-undefined
14 centerHorizontally: false,
15 descent: 0,
16 fixedWidth: false,
17 fontHeight: null,
18 fontId: null,
19 fontName: "webfont",
20 fontStyle: "",
21 fontWeight: "",
22 formats: ["svg", "ttf", "eot", "woff", "woff2"],
23 formatsOptions: {
24 ttf: {
25 copyright: null,
26 ts: null,
27 version: null
28 }
29 },
30 glyphTransformFn: null,
31 // Maybe allow setup from CLI
32 maxConcurrency: _os2.default.cpus().length,
33 metadata: null,
34 metadataProvider: null,
35 normalize: false,
36 prependUnicode: false,
37 round: 10e12,
38 startUnicode: 0xea01,
39 template: null,
40 templateClassName: null,
41 templateFontName: null,
42 templateFontPath: "./",
43 verbose: false
44 }, initialOptions);
45
46 let glyphsData = [];
47
48 return buildConfig({
49 configFile: options.configFile
50 }).then(loadedConfig => {
51 if (Object.keys(loadedConfig).length > 0) {
52 options = (0, _mergeDeep2.default)({}, options, loadedConfig.config);
53 options.filePath = loadedConfig.filepath;
54 }
55
56 return (0, _globby2.default)([].concat(options.files)).then(foundFiles => {
57 const filteredFiles = foundFiles.filter(foundFile => _path2.default.extname(foundFile) === ".svg");
58
59 if (filteredFiles.length === 0) {
60 throw new Error("Files glob patterns specified did not match any files");
61 }
62
63 return getGlyphsData(foundFiles, options);
64 }).then(returnedGlyphsData => {
65 glyphsData = returnedGlyphsData;
66
67 return svgIcons2svgFont(returnedGlyphsData, options);
68 })
69 // Maybe add ttfautohint
70 .then(svgFont => {
71 const result = {};
72
73 result.svg = svgFont;
74 result.glyphsData = glyphsData;
75
76 result.ttf = Buffer.from((0, _svg2ttf2.default)(result.svg.toString(), options.formatsOptions && options.formatsOptions.ttf ? options.formatsOptions.ttf : {}).buffer);
77
78 if (options.formats.indexOf("eot") !== -1) {
79 result.eot = Buffer.from((0, _ttf2eot2.default)(result.ttf).buffer);
80 }
81
82 if (options.formats.indexOf("woff") !== -1) {
83 result.woff = Buffer.from((0, _ttf2woff2.default)(result.ttf, {
84 metadata: options.metadata
85 }).buffer);
86 }
87
88 if (options.formats.indexOf("woff2") !== -1) {
89 result.woff2 = (0, _ttf2woff4.default)(result.ttf);
90 }
91
92 return result;
93 }).then(result => {
94 if (!options.template) {
95 return result;
96 }
97
98 const buildInTemplateDirectory = _path2.default.join(__dirname, "../templates");
99 const buildInTemplates = {
100 css: {
101 path: _path2.default.join(buildInTemplateDirectory, "template.css.njk")
102 },
103 html: {
104 path: _path2.default.join(buildInTemplateDirectory, "template.preview-html.njk")
105 },
106 scss: {
107 path: _path2.default.join(buildInTemplateDirectory, "template.scss.njk")
108 }
109 };
110
111 let templateFilePath = null;
112
113 if (Object.keys(buildInTemplates).includes(options.template)) {
114 result.usedBuildInTemplate = true;
115
116 _nunjucks2.default.configure(_path2.default.join(__dirname, "../"));
117
118 templateFilePath = `${buildInTemplateDirectory}/template.${options.template}.njk`;
119 } else {
120 const resolvedTemplateFilePath = _path2.default.resolve(options.template);
121
122 _nunjucks2.default.configure(_path2.default.dirname(resolvedTemplateFilePath));
123
124 templateFilePath = _path2.default.resolve(resolvedTemplateFilePath);
125 }
126
127 const nunjucksOptions = (0, _mergeDeep2.default)({}, {
128 glyphs: glyphsData.map(glyphData => {
129 if (typeof options.glyphTransformFn === "function") {
130 glyphData.metadata = options.glyphTransformFn(glyphData.metadata);
131 }
132
133 return glyphData.metadata;
134 })
135 }, options, {
136 className: options.templateClassName ? options.templateClassName : options.fontName,
137 fontName: options.templateFontName ? options.templateFontName : options.fontName,
138 fontPath: options.templateFontPath.replace(/\/?$/, "/")
139 });
140
141 result.template = _nunjucks2.default.render(templateFilePath, nunjucksOptions);
142
143 return result;
144 }).then(result => {
145 if (options.formats.indexOf("svg") === -1) {
146 delete result.svg;
147 }
148
149 if (options.formats.indexOf("ttf") === -1) {
150 delete result.ttf;
151 }
152
153 result.config = options;
154
155 return result;
156 });
157 });
158};
159
160var _stream = require("stream");
161
162var _svgicons2svgfont = require("svgicons2svgfont");
163
164var _svgicons2svgfont2 = _interopRequireDefault(_svgicons2svgfont);
165
166var _cosmiconfig = require("cosmiconfig");
167
168var _cosmiconfig2 = _interopRequireDefault(_cosmiconfig);
169
170var _asyncThrottle = require("async-throttle");
171
172var _asyncThrottle2 = _interopRequireDefault(_asyncThrottle);
173
174var _metadata = require("svgicons2svgfont/src/metadata");
175
176var _metadata2 = _interopRequireDefault(_metadata);
177
178var _filesorter = require("svgicons2svgfont/src/filesorter");
179
180var _filesorter2 = _interopRequireDefault(_filesorter);
181
182var _fs = require("fs");
183
184var _fs2 = _interopRequireDefault(_fs);
185
186var _globby = require("globby");
187
188var _globby2 = _interopRequireDefault(_globby);
189
190var _mergeDeep = require("merge-deep");
191
192var _mergeDeep2 = _interopRequireDefault(_mergeDeep);
193
194var _nunjucks = require("nunjucks");
195
196var _nunjucks2 = _interopRequireDefault(_nunjucks);
197
198var _os = require("os");
199
200var _os2 = _interopRequireDefault(_os);
201
202var _path = require("path");
203
204var _path2 = _interopRequireDefault(_path);
205
206var _svg2ttf = require("svg2ttf");
207
208var _svg2ttf2 = _interopRequireDefault(_svg2ttf);
209
210var _ttf2eot = require("ttf2eot");
211
212var _ttf2eot2 = _interopRequireDefault(_ttf2eot);
213
214var _ttf2woff = require("ttf2woff");
215
216var _ttf2woff2 = _interopRequireDefault(_ttf2woff);
217
218var _ttf2woff3 = require("ttf2woff2");
219
220var _ttf2woff4 = _interopRequireDefault(_ttf2woff3);
221
222var _xml2js = require("xml2js");
223
224var _xml2js2 = _interopRequireDefault(_xml2js);
225
226function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
227
228function getGlyphsData(files, options) {
229 const metadataProvider = options.metadataProvider || (0, _metadata2.default)({
230 prependUnicode: options.prependUnicode,
231 startUnicode: options.startUnicode
232 });
233
234 const sortedFiles = files.sort((fileA, fileB) => (0, _filesorter2.default)(fileA, fileB));
235 const xmlParser = new _xml2js2.default.Parser();
236 const throttle = (0, _asyncThrottle2.default)(options.maxConcurrency);
237
238 return Promise.all(sortedFiles.map(srcPath => throttle(() => new Promise((resolve, reject) => {
239 const glyph = _fs2.default.createReadStream(srcPath);
240 let glyphContents = "";
241
242 return glyph.on("error", glyphError => reject(glyphError)).on("data", data => {
243 glyphContents += data.toString();
244 }).on("end", () => {
245 // Maybe bug in xml2js
246 if (glyphContents.length === 0) {
247 return reject(new Error(`Empty file ${srcPath}`));
248 }
249
250 return xmlParser.parseString(glyphContents, error => {
251 if (error) {
252 return reject(error);
253 }
254
255 const glyphData = {
256 contents: glyphContents,
257 srcPath
258 };
259
260 return resolve(glyphData);
261 });
262 });
263 })).then(glyphData => new Promise((resolve, reject) => {
264 metadataProvider(glyphData.srcPath, (error, metadata) => {
265 if (error) {
266 return reject(error);
267 }
268
269 glyphData.metadata = metadata;
270
271 return resolve(glyphData);
272 });
273 }))));
274}
275
276function svgIcons2svgFont(glyphsData, options) {
277 let result = "";
278
279 return new Promise((resolve, reject) => {
280 const fontStream = new _svgicons2svgfont2.default({
281 ascent: options.ascent,
282 centerHorizontally: options.centerHorizontally,
283 descent: options.descent,
284 fixedWidth: options.fixedWidth,
285 fontHeight: options.fontHeight,
286 fontId: options.fontId,
287 fontName: options.fontName,
288 fontStyle: options.fontStyle,
289 fontWeight: options.fontWeight,
290 // eslint-disable-next-line no-console, no-empty-function
291 log: options.vebose ? console.log.bind(console) : () => {},
292 metadata: options.metadata,
293 normalize: options.normalize,
294 round: options.round
295 }).on("finish", () => resolve(result)).on("data", data => {
296 result += data;
297 }).on("error", error => reject(error));
298
299 glyphsData.forEach(glyphData => {
300 const glyphStream = new _stream.Readable();
301
302 glyphStream.push(glyphData.contents);
303 glyphStream.push(null);
304
305 glyphStream.metadata = glyphData.metadata;
306
307 fontStream.write(glyphStream);
308 });
309
310 fontStream.end();
311 });
312}
313
314function buildConfig(options) {
315 const cosmiconfigOptions = {
316 argv: true,
317 // Allow extensions on rc filenames
318 rcExtensions: true
319 };
320
321 let searchPath = process.cwd();
322 let configPath = null;
323
324 if (options.configFile) {
325 searchPath = null;
326 configPath = _path2.default.resolve(process.cwd(), options.configFile);
327 }
328
329 return (0, _cosmiconfig2.default)("webfont", cosmiconfigOptions).load(searchPath, configPath).then(result => {
330 if (!result) {
331 return {};
332 }
333
334 return result;
335 });
336}
\No newline at end of file