Skip to content

Instantly share code, notes, and snippets.

@mazdel
Created November 25, 2023 22:34
Show Gist options
  • Save mazdel/ede64d8cbb7e5106adeae7ffffe21bbd to your computer and use it in GitHub Desktop.
Save mazdel/ede64d8cbb7e5106adeae7ffffe21bbd to your computer and use it in GitHub Desktop.
FontAwesome dynamic import on NextJS without Babel
/*
* 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