使用 webpack 和 scss:
使用 npm 安装 font-awesome(使用 https://fontawesome.com/how-to-use 上的设置说明)
npm install @fortawesome/fontawesome-free
接下来,使用 copy-webpack-plugin,将 webfonts 文件夹从 node_modules 复制到您的 dist webpack 构建过程中的文件夹。 (https://github.com/webpack-contrib/copy-webpack-plugin)
npm install copy-webpack-plugin
在 webpack.config.js 中,配置 copy-webpack-plugin。注意:默认的 webpack 4 dist 文件夹是“dist”,所以我们将 webfonts 文件夹从 node_modules 复制到 dist 文件夹。
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new CopyWebpackPlugin([
{ from: './node_modules/@fortawesome/fontawesome-free/webfonts', to: './webfonts'}
])
]
}
最后,在你的 main.scss 文件中,告诉 fontawesome webfonts 文件夹被复制到哪里,然后从 node_modules 导入你想要的 SCSS 文件强>。
$fa-font-path: "/webfonts"; // destination folder in dist
//Adapt the path to be relative to your main.scss file
@import "../node_modules/@fortawesome/fontawesome-free/scss/fontawesome";
//Include at least one of the below, depending on what icons you want.
//Adapt the path to be relative to your 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 you also want to use `fa v4` like: `fa fa-address-book-o`
并将以下 font-family 应用到 html 文档中要使用 fontawesome 图标的所需区域。
示例:
body {
font-family: 'Font Awesome 5 Free'; // if you use fa v5 (regular/solid)
// font-family: 'Font Awesome 5 Brands'; // if you use fa v5 (brands)
}