Skip to content

Commit

Permalink
fix: Remove lodash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jun 9, 2019
1 parent 59d1b5b commit ae9a50b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17,337 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check
'use strict';
let _ = require('lodash');
let assert = require('assert');
const assert = require('assert');

module.exports = function IconfontWebpackPlugin (userOptions) {
// Default options
const options = _.extend({
const options = Object.assign({

// allows to prefix the font name to prevent collisions
fontNamePrefix: '',
Expand Down
25 changes: 13 additions & 12 deletions lib/icons-to-woff.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
'use strict';
const assert = require('assert');
const path = require('path');
Expand All @@ -7,7 +8,7 @@ const ttf2woff = require('ttf2woff');
const Readable = require('stream').Readable;

/**
* @param {InputFileSystem} fs An input file system
* @param {typeof import("fs")} fs An input file system
* @param {String[]} icons Array of icon file paths
* @param {Object} options SVG-Font options
* @return {Promise<String>} Base64 encoded font
Expand Down Expand Up @@ -37,18 +38,18 @@ module.exports = function createIconFont (fs, icons, options) {
reject(err);
});
icons.forEach((filename, i) => {
const glyph = new Readable();
glyph._read = function noop () {};
glyph.metadata = {
unicode: [String.fromCodePoint('\ue000'.charCodeAt(0) + i)],
name: 'i' + i
};
const glyph = Object.assign(new Readable(), {
_read: function noop () {},
metadata: {
unicode: [String.fromCodePoint('\ue000'.charCodeAt(0) + i)],
name: 'i' + i
}
});
fontStream.write(glyph);
fs.readFile(filename, function (err, result) {
fs.readFile(filename, 'utf-8', function (err, result) {
if (err) {
return reject(err);
}
result = result.toString();
const iconHeight = getSvgHeight(result, filename);
if (!iconHeight) {
return reject(new Error(`SVG font generation failed as icon "${filename}" does not have a height.`));
Expand All @@ -69,9 +70,9 @@ module.exports = function createIconFont (fs, icons, options) {
/**
* Reads the height of the svg
*
* @param {String} svg the svg content
* @param {String} filename the file name for error reporting
* @return {Number} height
* @param {string} svg the svg content
* @param {string} filename the file name for error reporting
* @return {string} height
*/
function getSvgHeight (svg, filename) {
const parseSvg = /<svg[^>]+height\s*=\s*["']?(\d+)\s*(pt|px|)["']?/i.exec(svg);
Expand Down
32 changes: 21 additions & 11 deletions lib/postcss-plugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @ts-check
'use strict';
const postcss = require('postcss');
const path = require('path');
const _ = require('lodash');
const crypto = require('crypto');

const urlRegexp = new RegExp('url\\s*\\((\\s*"([^"]+)"|\'([^\']+)\'|([^\'")]+))\\)');

/**
* Turn `url("demo.svg")` into `demo.svg`
* @param value {string}
*/
function getUnresolvedIconPath (value) {
const relativePathResult = urlRegexp.exec(value);
Expand All @@ -17,6 +18,12 @@ function getUnresolvedIconPath (value) {
return relativePathResult[2] || relativePathResult[3] || relativePathResult[4];
}

/**
* Parses a `font-icon: url('./demo.svg')` expression
*
* @param value {string}
* @returns {{url: string, size?: string}}
*/
function parseFontIconValue (value) {
const valueParts = value.trim().split(' ');
const result = {};
Expand All @@ -35,21 +42,23 @@ function parseFontIconValue (value) {
/**
* Returns a promise with the result of all `icon-font:url(...)` svg paths of the given file
*
* @param fontName {string} The name of the font (font-family)
* @param resolve {function} The webpack resolve helper
* @param resolvedSvgs {string} The css loader path context to resolve relative urls
* @param postCssRoot {postcss.Root} The name of the font (font-family)
* @param webpackResolve {function} The webpack resolve helper
* @param context {string} The css loader path context to resolve relative urls
*
* @returns {Promise<{resolved: string[], unresolved: string[], relative: string[]}>}
*/
function getSvgPaths (postCssRoot, webpackResolve, context) {
// Gather all font-icon urls:
let unresolvedPaths = [];
const unresolvedPathsSet = new Set();
postCssRoot.walkDecls((decl) => {
if (decl.prop === 'font-icon' || decl.prop === 'font-icon-glyph') {
const fontIcon = parseFontIconValue(decl.value);
unresolvedPaths.push(fontIcon.url);
unresolvedPathsSet.add(fontIcon.url);
}
});
// Remove duplicates
unresolvedPaths = _.uniq(unresolvedPaths);
const unresolvedPaths = Array.from(unresolvedPathsSet);
// Resolve the urls to the absolute url
return Promise.all(unresolvedPaths.map((unresolvedPath) =>
new Promise((resolve, reject) => {
Expand Down Expand Up @@ -124,7 +133,8 @@ function replaceIconFontDeclarations (fontName, postCssRoot, svgPaths) {
}
// Look up the index of the svg in the array to generate the unicode char position
const iconCharCode = svgPaths.unresolved.indexOf(getUnresolvedIconPath(decl.value));
decl.value = '\'\\e' + _.padStart(iconCharCode.toString(16), 3, '0') + '\'';
const iconCharCodeHex = iconCharCode.toString(16);
decl.value = '\'\\e' + '0'.repeat(Math.max(0, 3 - iconCharCodeHex.length)) + iconCharCodeHex + '\'';
// Turn `font-icon:` into `content:`
decl.prop = 'content';
}
Expand All @@ -135,8 +145,8 @@ function replaceIconFontDeclarations (fontName, postCssRoot, svgPaths) {
* @param fontName {string} The name of the font (font-family)
* @param postCssRoot {object} The postCss root object
* @param useCssModules {boolean} wether the css loader is using css-modules or not
* @param useCssModules {number} the enforced height of the svg font
* @param resolvedRelativeSvgs {object} The svg path information
* @param enforcedSvgHeight {number} the enforced height of the svg font
* @param svgPaths {object} The svg path information
*/
function addFontDeclaration (fontName, postCssRoot, useCssModules, enforcedSvgHeight, svgPaths) {
// The options are passed as a query string so we use the relative svg paths to reduce the path length per file
Expand Down Expand Up @@ -174,7 +184,7 @@ module.exports = postcss.plugin('iconfont-webpack', config => function (root, re
}
// Generate a font icon name
const md5sum = crypto.createHash('md5');
md5sum.update(JSON.stringify(_.values(svgPaths.relative)));
md5sum.update(JSON.stringify(svgPaths.relative));
let fontName = md5sum.digest('hex').substr(0, 6);
// Prefix the fontname with a letter as fonts with a leading number are not allowed
fontName = config.fontNamePrefix + String.fromCharCode(fontName.charCodeAt(0) + 20) + fontName.substr(1);
Expand Down
Loading

0 comments on commit ae9a50b

Please sign in to comment.