Skip to content

Commit ae9a50b

Browse files
committed
fix: Remove lodash dependency
1 parent 59d1b5b commit ae9a50b

File tree

5 files changed

+37
-17337
lines changed

5 files changed

+37
-17337
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
// @ts-check
12
'use strict';
2-
let _ = require('lodash');
3-
let assert = require('assert');
3+
const assert = require('assert');
44

55
module.exports = function IconfontWebpackPlugin (userOptions) {
66
// Default options
7-
const options = _.extend({
7+
const options = Object.assign({
88

99
// allows to prefix the font name to prevent collisions
1010
fontNamePrefix: '',

lib/icons-to-woff.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
'use strict';
23
const assert = require('assert');
34
const path = require('path');
@@ -7,7 +8,7 @@ const ttf2woff = require('ttf2woff');
78
const Readable = require('stream').Readable;
89

910
/**
10-
* @param {InputFileSystem} fs An input file system
11+
* @param {typeof import("fs")} fs An input file system
1112
* @param {String[]} icons Array of icon file paths
1213
* @param {Object} options SVG-Font options
1314
* @return {Promise<String>} Base64 encoded font
@@ -37,18 +38,18 @@ module.exports = function createIconFont (fs, icons, options) {
3738
reject(err);
3839
});
3940
icons.forEach((filename, i) => {
40-
const glyph = new Readable();
41-
glyph._read = function noop () {};
42-
glyph.metadata = {
43-
unicode: [String.fromCodePoint('\ue000'.charCodeAt(0) + i)],
44-
name: 'i' + i
45-
};
41+
const glyph = Object.assign(new Readable(), {
42+
_read: function noop () {},
43+
metadata: {
44+
unicode: [String.fromCodePoint('\ue000'.charCodeAt(0) + i)],
45+
name: 'i' + i
46+
}
47+
});
4648
fontStream.write(glyph);
47-
fs.readFile(filename, function (err, result) {
49+
fs.readFile(filename, 'utf-8', function (err, result) {
4850
if (err) {
4951
return reject(err);
5052
}
51-
result = result.toString();
5253
const iconHeight = getSvgHeight(result, filename);
5354
if (!iconHeight) {
5455
return reject(new Error(`SVG font generation failed as icon "${filename}" does not have a height.`));
@@ -69,9 +70,9 @@ module.exports = function createIconFont (fs, icons, options) {
6970
/**
7071
* Reads the height of the svg
7172
*
72-
* @param {String} svg the svg content
73-
* @param {String} filename the file name for error reporting
74-
* @return {Number} height
73+
* @param {string} svg the svg content
74+
* @param {string} filename the file name for error reporting
75+
* @return {string} height
7576
*/
7677
function getSvgHeight (svg, filename) {
7778
const parseSvg = /<svg[^>]+height\s*=\s*["']?(\d+)\s*(pt|px|)["']?/i.exec(svg);

lib/postcss-plugin.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
// @ts-check
12
'use strict';
23
const postcss = require('postcss');
34
const path = require('path');
4-
const _ = require('lodash');
55
const crypto = require('crypto');
66

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

99
/**
1010
* Turn `url("demo.svg")` into `demo.svg`
11+
* @param value {string}
1112
*/
1213
function getUnresolvedIconPath (value) {
1314
const relativePathResult = urlRegexp.exec(value);
@@ -17,6 +18,12 @@ function getUnresolvedIconPath (value) {
1718
return relativePathResult[2] || relativePathResult[3] || relativePathResult[4];
1819
}
1920

21+
/**
22+
* Parses a `font-icon: url('./demo.svg')` expression
23+
*
24+
* @param value {string}
25+
* @returns {{url: string, size?: string}}
26+
*/
2027
function parseFontIconValue (value) {
2128
const valueParts = value.trim().split(' ');
2229
const result = {};
@@ -35,21 +42,23 @@ function parseFontIconValue (value) {
3542
/**
3643
* Returns a promise with the result of all `icon-font:url(...)` svg paths of the given file
3744
*
38-
* @param fontName {string} The name of the font (font-family)
39-
* @param resolve {function} The webpack resolve helper
40-
* @param resolvedSvgs {string} The css loader path context to resolve relative urls
45+
* @param postCssRoot {postcss.Root} The name of the font (font-family)
46+
* @param webpackResolve {function} The webpack resolve helper
47+
* @param context {string} The css loader path context to resolve relative urls
48+
*
49+
* @returns {Promise<{resolved: string[], unresolved: string[], relative: string[]}>}
4150
*/
4251
function getSvgPaths (postCssRoot, webpackResolve, context) {
4352
// Gather all font-icon urls:
44-
let unresolvedPaths = [];
53+
const unresolvedPathsSet = new Set();
4554
postCssRoot.walkDecls((decl) => {
4655
if (decl.prop === 'font-icon' || decl.prop === 'font-icon-glyph') {
4756
const fontIcon = parseFontIconValue(decl.value);
48-
unresolvedPaths.push(fontIcon.url);
57+
unresolvedPathsSet.add(fontIcon.url);
4958
}
5059
});
5160
// Remove duplicates
52-
unresolvedPaths = _.uniq(unresolvedPaths);
61+
const unresolvedPaths = Array.from(unresolvedPathsSet);
5362
// Resolve the urls to the absolute url
5463
return Promise.all(unresolvedPaths.map((unresolvedPath) =>
5564
new Promise((resolve, reject) => {
@@ -124,7 +133,8 @@ function replaceIconFontDeclarations (fontName, postCssRoot, svgPaths) {
124133
}
125134
// Look up the index of the svg in the array to generate the unicode char position
126135
const iconCharCode = svgPaths.unresolved.indexOf(getUnresolvedIconPath(decl.value));
127-
decl.value = '\'\\e' + _.padStart(iconCharCode.toString(16), 3, '0') + '\'';
136+
const iconCharCodeHex = iconCharCode.toString(16);
137+
decl.value = '\'\\e' + '0'.repeat(Math.max(0, 3 - iconCharCodeHex.length)) + iconCharCodeHex + '\'';
128138
// Turn `font-icon:` into `content:`
129139
decl.prop = 'content';
130140
}
@@ -135,8 +145,8 @@ function replaceIconFontDeclarations (fontName, postCssRoot, svgPaths) {
135145
* @param fontName {string} The name of the font (font-family)
136146
* @param postCssRoot {object} The postCss root object
137147
* @param useCssModules {boolean} wether the css loader is using css-modules or not
138-
* @param useCssModules {number} the enforced height of the svg font
139-
* @param resolvedRelativeSvgs {object} The svg path information
148+
* @param enforcedSvgHeight {number} the enforced height of the svg font
149+
* @param svgPaths {object} The svg path information
140150
*/
141151
function addFontDeclaration (fontName, postCssRoot, useCssModules, enforcedSvgHeight, svgPaths) {
142152
// The options are passed as a query string so we use the relative svg paths to reduce the path length per file
@@ -174,7 +184,7 @@ module.exports = postcss.plugin('iconfont-webpack', config => function (root, re
174184
}
175185
// Generate a font icon name
176186
const md5sum = crypto.createHash('md5');
177-
md5sum.update(JSON.stringify(_.values(svgPaths.relative)));
187+
md5sum.update(JSON.stringify(svgPaths.relative));
178188
let fontName = md5sum.digest('hex').substr(0, 6);
179189
// Prefix the fontname with a letter as fonts with a leading number are not allowed
180190
fontName = config.fontNamePrefix + String.fromCharCode(fontName.charCodeAt(0) + 20) + fontName.substr(1);

0 commit comments

Comments
 (0)