1+ // @ts -check
12'use strict' ;
23const postcss = require ( 'postcss' ) ;
34const path = require ( 'path' ) ;
4- const _ = require ( 'lodash' ) ;
55const crypto = require ( 'crypto' ) ;
66
77const urlRegexp = new RegExp ( 'url\\s*\\((\\s*"([^"]+)"|\'([^\']+)\'|([^\'")]+))\\)' ) ;
88
99/**
1010 * Turn `url("demo.svg")` into `demo.svg`
11+ * @param value {string}
1112 */
1213function 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+ */
2027function 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 */
4251function 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 */
141151function 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