Skip to content

Commit 08a9348

Browse files
committed
refactoring
1 parent 7db24d3 commit 08a9348

File tree

4 files changed

+42
-56
lines changed

4 files changed

+42
-56
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/engines/fontforge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const exec = require('child_process').exec;
1717
const chalk = require('chalk');
1818
const wf = require('../util/util');
1919

20-
module.exports = function(options, allDone) {
20+
module.exports = function fontForgeEngine(options, allDone) {
2121
const logger = options.logger;
2222

2323
// Copy source files to temporary directory

tasks/util/util.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
'use strict';
1212

13-
const [path, glob] = [require('path'), require('glob')];
13+
const glob = require('glob');
14+
const path = require('path');
1415

1516
/**
1617
* Unicode Private Use Area start.
@@ -31,6 +32,7 @@ const fontsSrcsMap = {
3132
{
3233
ext: '.eot?#iefix',
3334
format: 'embedded-opentype',
35+
embeddable: false,
3436
},
3537
],
3638
woff: [
@@ -62,6 +64,7 @@ const fontsSrcsMap = {
6264
{
6365
ext: '.svg#{fontBaseName}',
6466
format: 'svg',
67+
embeddable: false,
6568
},
6669
],
6770
};
@@ -74,6 +77,7 @@ const cssFilePrefixes = {
7477
_default: '',
7578
sass: '_',
7679
scss: '_',
80+
styl: '',
7781
};
7882

7983
/**
@@ -82,6 +86,8 @@ const cssFilePrefixes = {
8286
*/
8387
const fontSrcSeparators = {
8488
_default: ',\n\t\t',
89+
sass: ',\n\t\t',
90+
scss: ',\n\t\t',
8591
styl: ', ',
8692
};
8793

tasks/webfonts.js

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ const glob = require('glob');
1717
const chalk = require('chalk');
1818
const crypto = require('crypto');
1919
const ttf2woff2 = require('ttf2woff2');
20-
const _ = require('lodash');
20+
const buildTemplateFn = require('lodash/template');
2121

22+
/**
23+
* @param {IGrunt} grunt
24+
*/
2225
module.exports = function webFonts(grunt) {
2326
const wf = require('./util/util');
2427

@@ -76,7 +79,7 @@ module.exports = function webFonts(grunt) {
7679
/*
7780
* Check for `dest` param on either target config or global options object
7881
*/
79-
if (_.isNil(params.dest) && _.isNil(options.dest)) {
82+
if (params.dest == null && options.dest == null) {
8083
logger.warn('Required property ' + [this.name, this.target, 'dest'].join('.') +
8184
' or ' + [this.name, this.target, 'options.dest'].join('.') + ' missing.');
8285
}
@@ -87,11 +90,10 @@ module.exports = function webFonts(grunt) {
8790
}
8891

8992
// Source files
90-
let svgFiles = _(this.filesSrc).filter(isSvgFile);
93+
let files = this.filesSrc.filter(isSvgFile);
9194
if (options.skipLinks === true) {
92-
svgFiles = svgFiles.reject(isFsLink);
95+
files = files.filter((filepath) => !fs.lstatSync(filepath).isSymbolicLink());
9396
}
94-
const files = svgFiles.value();
9597

9698
if (!files.length) {
9799
logger.warn('Specified empty list of source SVG files.');
@@ -149,7 +151,7 @@ module.exports = function webFonts(grunt) {
149151
execMaxBuffer: options.execMaxBuffer || 1024 * 200,
150152
};
151153

152-
_.assignIn(defaultOptions, {
154+
Object.assign(defaultOptions, {
153155
fontName: defaultOptions.fontBaseName,
154156
destCssPaths: {
155157
css: defaultOptions.destCss,
@@ -160,9 +162,9 @@ module.exports = function webFonts(grunt) {
160162
},
161163
relativeFontPath: defaultOptions.relativeFontPath || path.relative(defaultOptions.destCss, defaultOptions.dest),
162164
destHtml: options.destHtml || defaultOptions.destCss,
163-
fontfaceStyles: has(defaultOptions.styles, 'font'),
164-
baseStyles: has(defaultOptions.styles, 'icon'),
165-
extraStyles: has(defaultOptions.styles, 'extra'),
165+
fontfaceStyles: defaultOptions.styles.includes('font'),
166+
baseStyles: defaultOptions.styles.includes('icon'),
167+
extraStyles: defaultOptions.styles.includes('extra'),
166168
files,
167169
glyphs: [],
168170
});
@@ -242,7 +244,7 @@ module.exports = function webFonts(grunt) {
242244
* Call callback function if it was specified in the options.
243245
*/
244246
function completeTask() {
245-
if (defaultOptions && _.isFunction(defaultOptions.callback)) {
247+
if (defaultOptions && defaultOptions.callback && 'call' in defaultOptions.callback) {
246248
defaultOptions.callback(defaultOptions.fontName, defaultOptions.types, defaultOptions.glyphs, defaultOptions.hash);
247249
}
248250
allDone();
@@ -319,7 +321,7 @@ module.exports = function webFonts(grunt) {
319321
}
320322

321323
if (result) {
322-
defaultOptions = _.assignIn(defaultOptions, result);
324+
defaultOptions = Object.assign(defaultOptions, result);
323325
}
324326

325327
done();
@@ -333,7 +335,7 @@ module.exports = function webFonts(grunt) {
333335
*/
334336
function generateWoff2Font(done) {
335337
if (
336-
!has(defaultOptions.types, 'woff2') ||
338+
!defaultOptions.types.includes('woff2') ||
337339
fs.existsSync(wf.getFontPath(defaultOptions, 'woff2'))
338340
) {
339341
done();
@@ -345,7 +347,7 @@ module.exports = function webFonts(grunt) {
345347
const ttfFont = fs.readFileSync(ttfFontPath);
346348

347349
// Remove TTF font if not needed
348-
if (!has(defaultOptions.types, 'ttf')) {
350+
if (!defaultOptions.types.includes('ttf')) {
349351
fs.unlinkSync(ttfFontPath);
350352
}
351353

@@ -391,7 +393,7 @@ module.exports = function webFonts(grunt) {
391393
const fontSrcs = [[], []];
392394

393395
defaultOptions.order.forEach((type) => {
394-
if (!has(defaultOptions.types, type)) return;
396+
if (!defaultOptions.types.includes(type)) return;
395397
wf.fontsSrcsMap[type].forEach((font, idx) => {
396398
if (font) {
397399
fontSrcs[idx].push(generateFontSrc(type, font, stylesheet));
@@ -409,26 +411,26 @@ module.exports = function webFonts(grunt) {
409411

410412
// Read JSON file corresponding to CSS template
411413
const templateJson = readTemplate(defaultOptions.template, defaultOptions.syntax, '.json', true);
412-
if (templateJson) defaultOptions = _.assignIn(defaultOptions, JSON.parse(templateJson.template));
414+
if (templateJson) defaultOptions = Object.assign(defaultOptions, JSON.parse(templateJson.template));
413415

414416
// Now override values with templateOptions
415-
if (defaultOptions.templateOptions) defaultOptions = _.assignIn(defaultOptions, defaultOptions.templateOptions);
417+
if (defaultOptions.templateOptions) defaultOptions = Object.assign(defaultOptions, defaultOptions.templateOptions);
416418

417419
// Generate CSS
418420
// Use extension of defaultOptions.template file if given, or default to .css
419421

420422
const ext = path.extname(defaultOptions.template) || '.css';
421423
defaultOptions.cssTemplate = readTemplate(defaultOptions.template, defaultOptions.syntax, ext);
422424

423-
const cssContext = _.assignIn(defaultOptions, {
425+
const cssContext = Object.assign(defaultOptions, {
424426
iconsStyles: true,
425427
stylesheet,
426428
});
427429

428430
let css = renderTemplate(defaultOptions.cssTemplate, cssContext);
429431

430432
// Fix CSS preprocessors comments: single line comments will be removed after compilation
431-
if (has(['sass', 'scss', 'less', 'styl'], stylesheet)) {
433+
if (['sass', 'scss', 'less', 'styl'].includes(stylesheet)) {
432434
css = css.replace(/\/\* *(.*?) *\*\//g, '// $1');
433435
}
434436

@@ -474,7 +476,7 @@ module.exports = function webFonts(grunt) {
474476
* @return {Object} Base template context
475477
*/
476478
function prepareBaseTemplateContext() {
477-
return _.assignIn({}, defaultOptions);
479+
return Object.assign({}, defaultOptions);
478480
}
479481

480482
/**
@@ -483,25 +485,25 @@ module.exports = function webFonts(grunt) {
483485
* @return {Object} HTML template context
484486
*/
485487
function prepareHtmlTemplateContext() {
486-
const context = _.assignIn({}, defaultOptions);
488+
const context = Object.assign({}, defaultOptions);
487489

488490
// Prepare relative font paths for injection into @font-face refs in HTML
489-
const relativeRe = new RegExp(_.escapeRegExp(defaultOptions.relativeFontPath), 'g');
491+
const relativeRe = new RegExp(defaultOptions.relativeFontPath, 'g');
490492
const htmlRelativeFontPath = normalizePath(path.relative(defaultOptions.destHtml, defaultOptions.dest));
491493

492494
let fontSrc1 = defaultOptions.fontSrc1.replace(relativeRe, htmlRelativeFontPath);
493495
let fontSrc2 = defaultOptions.fontSrc2.replace(relativeRe, htmlRelativeFontPath);
494496

495497
if (context.fontPathVariables) {
496498
const fontPathVariableName = context.fontFamilyName + '-font-path';
497-
const lessReplacer = new RegExp(_.escapeRegExp(`@{${fontPathVariableName}}`), 'g');
498-
const scssReplacer = new RegExp(_.escapeRegExp(`$${fontPathVariableName} + `), 'g');
499+
const lessReplacer = new RegExp(`@{${fontPathVariableName}}`, 'g');
500+
const scssReplacer = new RegExp(`$${fontPathVariableName} + `, 'g');
499501

500502
fontSrc1 = fontSrc1.replace(lessReplacer, '').replace(scssReplacer, '');
501503
fontSrc2 = fontSrc2.replace(lessReplacer, '').replace(scssReplacer, '');
502504
}
503505

504-
_.assignIn(context, {
506+
Object.assign(context, {
505507
fontSrc1,
506508
fontSrc2,
507509
fontfaceStyles: true,
@@ -511,9 +513,9 @@ module.exports = function webFonts(grunt) {
511513
stylesheet: 'css',
512514
});
513515

514-
return _(context).extend({
516+
return Object.assign(context, {
515517
styles: renderTemplate(defaultOptions.cssTemplate, context),
516-
}).value();
518+
});
517519
}
518520

519521
/**
@@ -526,7 +528,7 @@ module.exports = function webFonts(grunt) {
526528
function generateCustomOutput(outputConfig) {
527529
// Accesses context
528530
const context = prepareBaseTemplateContext();
529-
_.assignIn(context, outputConfig.context);
531+
Object.assign(context, outputConfig.context);
530532

531533
// Prepares config attributes related to template filepath
532534
const templatePath = outputConfig.template;
@@ -624,32 +626,21 @@ module.exports = function webFonts(grunt) {
624626
* @return {Array}
625627
*/
626628
function optionToArray(val, defVal) {
627-
if (_.isUndefined(val)) {
629+
if (val === undefined) {
628630
val = defVal;
629631
}
630632

631633
if (!val) {
632634
return [];
633635
}
634636

635-
if (!_.isString(val)) {
637+
if (typeof val !== 'string') {
636638
return val;
637639
}
638640

639641
return val.split(',').map((str) => str.trim());
640642
}
641643

642-
/**
643-
* Check if a value exists in an array
644-
*
645-
* @param {Array} haystack Array to find the needle in
646-
* @param {*} needle Value to find
647-
* @return {Boolean} Needle was found
648-
*/
649-
function has(haystack, needle) {
650-
return haystack.indexOf(needle) !== -1;
651-
}
652-
653644
/**
654645
* Return a specified option if it exists in an object or `_default` otherwise
655646
*
@@ -683,17 +674,6 @@ module.exports = function webFonts(grunt) {
683674
return path.extname(filepath).toLowerCase() === '.svg';
684675
}
685676

686-
/**
687-
* Check whether file is link or not
688-
*
689-
* @param {String} filepath File path
690-
* @return {Boolean}
691-
*/
692-
function isFsLink(filepath) {
693-
const stat = fs.lstatSync(filepath);
694-
return stat.isSymbolicLink();
695-
}
696-
697677
/**
698678
* Convert font file to data:uri and remove source file
699679
*
@@ -745,7 +725,7 @@ module.exports = function webFonts(grunt) {
745725
let fontPathVariableName = defaultOptions.fontFamilyName + '-font-path';
746726

747727
let url;
748-
if (font.embeddable && has(defaultOptions.embed, type)) {
728+
if (font.embeddable && defaultOptions.embed.includes(type)) {
749729
url = embedFont(path.join(defaultOptions.dest, filename));
750730
} else {
751731
if (defaultOptions.fontPathVariables && stylesheet !== 'css') {
@@ -816,7 +796,7 @@ module.exports = function webFonts(grunt) {
816796
*/
817797
function renderTemplate(template, context) {
818798
try {
819-
return _.template(template.template)(context);
799+
return buildTemplateFn(template.template)(context);
820800
} catch (error) {
821801
grunt.fail.fatal('Error while rendering template ' + template.filename + ': ' + error.message);
822802
}

0 commit comments

Comments
 (0)