Created
November 25, 2023 22:34
-
-
Save mazdel/ede64d8cbb7e5106adeae7ffffe21bbd to your computer and use it in GitHub Desktop.
FontAwesome dynamic import on NextJS without Babel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* this is an example workaround of FontAwesome dynamic import without using babel on NextJS. | |
* because of this error https://nextjs.org/docs/messages/babel-font-loader-conflict | |
* and this error https://nextjs.org/docs/messages/swc-disabled | |
* tested on dev mode of NextJS v14.0.1 | |
* | |
* feel free to discuss this workaround | |
*/ | |
'use client' | |
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | |
import { library } from "@fortawesome/fontawesome-svg-core"; | |
import * as Icons from "@fortawesome/free-brands-svg-icons"; | |
const AComponent = ()=>{ | |
const iconList = ['php','node-js','js','react','css3','css3-alt']; | |
const importIcons = Object.entries(Icons) | |
.filter(([, value]) => iconList.includes(value.iconName)) | |
.map(([key]) => Icons[key]); | |
library.add(...importIcons); | |
return ({ | |
iconList.map((icon)=> <FontAwesomeIcon icon={{ prefix: "fab", iconName: icon }} />) | |
}) | |
} | |
export {AComponent}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment