@@ -6,6 +6,16 @@ const buffer = require('vinyl-buffer')
66const concat = require ( 'gulp-concat' )
77const cssnano = require ( 'cssnano' )
88const fs = require ( 'fs-extra' )
9+ const iconPacks = {
10+ fa : require ( '@fortawesome/free-solid-svg-icons' ) ,
11+ fas : require ( '@fortawesome/free-solid-svg-icons' ) ,
12+ far : require ( '@fortawesome/free-regular-svg-icons' ) ,
13+ fab : require ( '@fortawesome/free-brands-svg-icons' ) ,
14+ __v4__ : require ( '@fortawesome/fontawesome-free/js/v4-shims' ) . reduce (
15+ ( accum , it ) => accum . set ( `fa-${ it [ 0 ] } ` , [ it [ 1 ] || 'fas' , `fa-${ it [ 2 ] || it [ 0 ] } ` ] ) ,
16+ new Map ( )
17+ ) ,
18+ }
919const imagemin = require ( 'gulp-imagemin' )
1020const merge = require ( 'merge-stream' )
1121const ospath = require ( 'path' )
@@ -84,6 +94,9 @@ module.exports = (src, dest, preview) => () => {
8494 next ( bundleError , file )
8595 } )
8696 )
97+ } else if ( file . relative === 'js/vendor/fontawesome-icon-defs.js' ) {
98+ file . contents = Buffer . from ( populateIconDefs ( require ( file . path ) ) )
99+ next ( null , file )
87100 } else {
88101 fs . readFile ( file . path , 'UTF-8' ) . then ( ( contents ) => {
89102 file . contents = Buffer . from ( contents )
@@ -132,3 +145,32 @@ function postcssPseudoElementFixer (css, result) {
132145 rule . selector = rule . selectors . map ( ( it ) => it . replace ( / ( ^ | [ ^ : ] ) : ( b e f o r e | a f t e r ) $ / , '$1::$2' ) ) . join ( ',' )
133146 } )
134147}
148+
149+ function populateIconDefs ( { FontAwesomeIconDefs : { includes = [ ] , admonitionIcons = { } } } ) {
150+ const iconDefs = [ ...new Set ( includes ) ] . reduce ( ( accum , iconKey ) => {
151+ if ( accum . has ( iconKey ) ) return accum
152+ const [ iconPrefix , iconName ] = iconKey . split ( ' ' ) . slice ( 0 , 2 )
153+ let iconDef = ( iconPacks [ iconPrefix ] || { } ) [ camelCase ( iconName ) ]
154+ if ( iconDef ) {
155+ return accum . set ( iconKey , { ...iconDef , prefix : iconPrefix } )
156+ } else if ( iconPrefix === 'fa' ) {
157+ const [ realIconPrefix , realIconName ] = iconPacks . __v4__ . get ( iconName ) || [ ]
158+ if (
159+ realIconName &&
160+ ! accum . has ( ( iconKey = `${ realIconPrefix } ${ realIconName } ` ) ) &&
161+ ( iconDef = ( iconPacks [ realIconPrefix ] || { } ) [ camelCase ( realIconName ) ] )
162+ ) {
163+ return accum . set ( iconKey , { ...iconDef , prefix : realIconPrefix } )
164+ }
165+ }
166+ return accum
167+ } , new Map ( ) )
168+ return [
169+ `window.FontAwesomeIconDefs = ${ JSON . stringify ( [ ...iconDefs . values ( ) ] ) } \n` ,
170+ `window.FontAwesomeIconDefs.admonitionIcons = ${ JSON . stringify ( admonitionIcons ) } \n` ,
171+ ] . join ( )
172+ }
173+
174+ function camelCase ( str ) {
175+ return str . replace ( / - ( .) / g, ( _ , l ) => l . toUpperCase ( ) )
176+ }
0 commit comments