[英]How to import font-awesome scss correctly
I am trying to use an icon from font-awesome with webpack 4 via scss.我正在尝试通过 scss 在 webpack 4 中使用 font-awesome 中的图标。
The webconfig file looks as following: webconfig文件如下所示:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const autoprefixer = require("autoprefixer");
module.exports = {
entry: ["./src/index.js"],
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.[hash].js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {
minimize: true
}
}
]
},
{
test: /\.(sass|scss)$/,
use: [
{
loader: "file-loader",
options: {
name: "style.css"
}
},
{ loader: "extract-loader" },
{
loader: "css-loader"
},
{
loader: "postcss-loader",
options: {
plugins: () => [autoprefixer({ grid: false })]
}
},
{
loader: "sass-loader",
options: {
includePaths: ["./node_modules"]
}
}
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "file-loader",
options: {
name: "[name][hash].[ext]",
outputPath: "fonts/"
}
}
]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./public/index.html",
filename: "./index.html"
}),
new CopyWebpackPlugin([
{
from: "public"
}
])
]
};
I imported the scss as following:我按如下方式导入了 scss:
@import "@fortawesome/fontawesome-free/scss/fontawesome.scss";
and use:并使用:
<a class="mdc-list-item mdc-list-item--selected demo-drawer-list-item" href="#">
<i class="fas fa-inbox mdc-list-item__graphic"></i>Inbox
</a>
It shows:表明:
a rectangle instead an icon.一个矩形而不是一个图标。 What am I doing wrong?
我究竟做错了什么? The fully example is on github .
完整的示例在github 上。
我遇到了同样的问题,将 font-awesome scss 导入到我的项目中,这对我有用。
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
For anyone else running into an issue with the fonts not loading (eg showing the empty square glyph) despite Webpack (4) compiling and file-loader copying the fonts properly, I found an option that makes everything load as expected.尽管 Webpack (4) 编译和文件加载器正确复制字体,但对于遇到字体未加载问题(例如显示空方形字形)的任何其他人,我找到了一个选项,可以按预期加载所有内容。
First the recommended SCSS imports with ~
node_modules path as seen in other posts:首先推荐的 SCSS 导入带有
~
node_modules 路径,如其他帖子所示:
$fa-font-path: '~@fortawesome/fontawesome-pro/webfonts';
@import '~@fortawesome/fontawesome-pro/scss/fontawesome';
@import '~@fortawesome/fontawesome-pro/scss/light';
Then you'll have the file-loader config targeting font files (above).然后,您将拥有针对字体文件的文件加载器配置(上图)。 To the file-loader options you want to add the
esModule: false
option .要添加
esModule: false
option的文件加载器选项。 Note that this could mess with other fonts you may be importing outside of SCSS.请注意,这可能会干扰您可能在 SCSS 之外导入的其他字体。
Basically what I discovered is the fonts would get copied, but the font file urls in the compiled CSS would show as src: url([object Module]);
基本上我发现的是字体会被复制,但是编译后的 CSS 中的字体文件 url 将显示为
src: url([object Module]);
. . By default file-loader turns the imported file into a module for convenience, but of course we don't need that for fonts here, so adding the option above fixes that.
默认情况下,为了方便起见,file-loader 将导入的文件转换为模块,但当然我们这里的字体不需要它,所以添加上面的选项可以解决这个问题。
to get font-awesome for Laravel project the next worked for me:为了让 Laravel 项目的字体很棒,下一个对我有用:
npm install --save @fortawesome/fontawesome-free
- that downloaded and created files to node_modules. npm install --save @fortawesome/fontawesome-free
- 将文件下载并创建到 node_modules。resources/sass/app.scss
resources/sass/app.scss
@import '~@fortawesome/fontawesome-free/scss/fontawesome';
@import '~@fortawesome/fontawesome-free/scss/regular';
@import '~@fortawesome/fontawesome-free/scss/brands';
@import '~@fortawesome/fontawesome-free/scss/solid';
resources/sass/_variables.scss
(not sure for this)resources/sass/_variables.scss
(不确定)$fa-font-path: "../webfonts";
npm run dev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.