Skip to content

Commit 4f86d27

Browse files
committed
support v4 shims
1 parent 8b8295b commit 4f86d27

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

gulp.d/tasks/build-preview-pages.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const iconPacks = {
1717
fab: require('@fortawesome/free-brands-svg-icons'),
1818
}
1919
iconPacks.fa = iconPacks.fas
20+
const iconShims = require('@fortawesome/fontawesome-free/js/v4-shims').reduce((accum, it) => {
21+
accum[it[0]] = [it[1] || 'fas', it[2] || it[0]]
22+
return accum
23+
}, {})
2024
const { obj: map } = require('through2')
2125
const merge = require('merge-stream')
2226
const ospath = require('path')
@@ -200,21 +204,27 @@ function findNavPath (currentUrl, node = [], current_path = [], root = true) {
200204
function injectIconDefs (file) {
201205
const contents = file.contents
202206
if (!contents.includes('<i class="fa')) return file
203-
const contentsString = contents.toString()
204-
const iconNames = contentsString.match(new RegExp('<i class="fa[brs]? fa-[^" ]+', 'g')).map((it) => {
205-
let [iconPrefix, iconName, ] = it.substr(10).split(' fa-')
206-
iconName = iconName.replace(/(?:^|-)(.)/g, function (_, l) { return l.toUpperCase() })
207-
return iconPrefix + '|' + iconName
207+
const stringContents = contents.toString()
208+
const iconNames = stringContents.match(new RegExp('<i class="fa[brs]? fa-[^" ]+', 'g')).map((it) => {
209+
return it.substr(10).replace(' fa-', ' ')
208210
})
209211
if (!iconNames.length) return file
210212
const iconDefs = [...new Set(iconNames)].reduce((accum, it) => {
211-
const [iconPrefix, iconName, ] = it.split('|')
212-
const iconDef = iconPacks[iconPrefix] && iconPacks[iconPrefix]['fa' + iconName]
213-
return iconDef ? accum.concat({ ...iconDef, prefix: iconPrefix }) : accum
213+
const [iconPrefix, iconName] = it.split(' ').slice(0, 2)
214+
let iconDef = (iconPacks[iconPrefix] || {})['fa' + camelCase(iconName)]
215+
if (iconDef) {
216+
return accum.concat({ ...iconDef, prefix: iconPrefix })
217+
} else if (iconPrefix === 'fa') {
218+
const [realIconPrefix, realIconName] = iconShims[iconName] || []
219+
if (realIconName && (iconDef = (iconPacks[realIconPrefix] || {})['fa' + camelCase(realIconName)])) {
220+
return accum.concat({ ...iconDef, prefix: realIconPrefix })
221+
}
222+
}
223+
return accum
214224
}, [])
215225
const iconData = `<script>\nwindow.FontAwesomeIconDefs = ${JSON.stringify(iconDefs)}\n</script>`
216226
file.contents = Buffer.from(
217-
contentsString.replace(new RegExp('<script async src="[^"]*_/js/vendor/fontawesome.js"></script>'), function (m) {
227+
stringContents.replace(new RegExp('<script async src="[^"]*_/js/vendor/fontawesome.js"></script>'), function (m) {
218228
return [iconData, m].join('\n')
219229
})
220230
)
@@ -249,3 +259,7 @@ function toPromise (stream) {
249259
.on('finish', () => resolve(data))
250260
)
251261
}
262+
263+
function camelCase (str) {
264+
return str.replace(/(?:^|-)(.)/g, function (_, l) { return l.toUpperCase() })
265+
}

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"devDependencies": {
1717
"@octokit/rest": "~18.0",
18+
"@fortawesome/fontawesome-free": "~5.13",
1819
"@fortawesome/fontawesome-svg-core": "~1.2",
1920
"@fortawesome/free-brands-svg-icons": "~5.13",
2021
"asciidoctor.js": "1.5.9",

src/js/vendor/fontawesome.bundle.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
el.classList.remove('fa')
66
})
77

8+
require('@fortawesome/fontawesome-free/js/v4-shims')
89
var fa = require('@fortawesome/fontawesome-svg-core')
910

1011
window.FontAwesomeIconDefs.forEach(function (faIconDef) {
1112
fa.library.add(faIconDef)
1213
})
1314

1415
fa.dom.i2svg()
16+
delete window.___FONT_AWESOME___
1517
})()

0 commit comments

Comments
 (0)