webfont 8.1.3 → 9.0.0

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