To incorporate font-awesome with webpack and scss, follow these steps:
Begin by installing font-awesome via npm using the guidelines provided at https://fontawesome.com/how-to-use
npm install @fortawesome/fontawesome-free
Then, utilize the copy-webpack-plugin to transfer the webfonts directory from node_modules to your project's dist folder during the webpack building process. (https://github.com/webpack-contrib/copy-webpack-plugin)
npm install copy-webpack-plugin
In your webpack.config.js, set up the copy-webpack-plugin. It's important to note that the default webpack 4 dist folder is "dist", so ensure the webfonts folder is being copied correctly.
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new CopyWebpackPlugin([
{ from: './node_modules/@fortawesome/fontawesome-free/webfonts', to: './webfonts'}
])
]
}
Lastly, in your main.scss file, specify where fontawesome can locate the copied webfonts folder and import the desired SCSS files from node_modules.
$fa-font-path: "/webfonts"; // destination folder in dist
//Adjust the path relative to main.scss file
@import "../node_modules/@fortawesome/fontawesome-free/scss/fontawesome";
//Include at least one of the below based on the required icons.
//Adjust the path relative to main.scss file
@import "../node_modules/@fortawesome/fontawesome-free/scss/brands";
@import "../node_modules/@fortawesome/fontawesome-free/scss/regular";
@import "../node_modules/@fortawesome/fontawesome-free/scss/solid";
@import "../node_modules/@fortawesome/fontawesome-free/scss/v4-shims"; // if utilizing `fa v4` such as: `fa fa-address-book-o`
Don't forget to assign the following font-family
to specific section(s) in your HTML document where you wish to display fontawesome icons.
Example:
body {
font-family: 'Font Awesome 5 Free'; // for fa v5 (regular/solid)
// font-family: 'Font Awesome 5 Brands'; // for fa v5 (brands)
}