我尝试在webpack 4中使用font-awesome 5.1.0。webpack配置文件的内容:
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", "./src/index.scss", "react-hot-loader/patch"],
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: "bundle.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"
}
])
]
};
字符串
在index.scss文件中:
@import "@fortawesome/fontawesome-free/css/fontawesome.css";
@import "@material/button/mdc-button.scss";
@import "@material/drawer/mdc-drawer.scss";
@import "@material/top-app-bar/mdc-top-app-bar.scss";
@import "@material/list/mdc-list.scss";
@import "./App/scss/app.scss";
* {
padding: 0;
margin: 0;
}
html, body {
height: 100%;
width: 100%;
}
#root {
display: flex;
flex-direction: row;
box-sizing: border-box;
height: 100%;
width: 100%;
}
型
正如你可以在图片上:
x1c 0d1x的数据
Font-awesome是从正确的地方导入的,但我遇到了编译器错误:
ERROR in ./src/index.scss
Module not found: Error: Can't resolve './@fortawesome/fontawesome-free/css/fontawesome.css' in '/home/developer/Desktop/reasonreact/cockpit/src'
@ ./src/index.scss
@ multi (webpack)-dev-server/client?http://localhost:9000 webpack/hot/dev-server ./src/index.js ./src/index.scss react-hot-loader/patch
型
我做错了什么?
更新
我把webconfig改成:
{
test: /\.(sass|scss)$/,
use: [
{
loader: "file-loader",
options: {
name: "bundle.css"
}
},
{ loader: "extract-loader" },
{
loader: "css-loader"
},
{
loader: "postcss-loader",
options: {
plugins: () => [autoprefixer({ grid: false })]
}
},
{
loader: "sass-loader",
options: {
includePaths: ["./node_modules", "src", "."]
}
}
]
},
型
我得到了以下错误:
ERROR in ./src/index.scss
Module build failed (from ./node_modules/extract-loader/lib/extractLoader.js):
NonErrorEmittedError: (Emitted value instead of an instance of Error) ReferenceError: require is not defined
at runLoaders (/home/developer/Desktop/reason-react/cockpit/node_modules/webpack/lib/NormalModule.js:300:13)
at /home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:364:11
at /home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:230:18
at context.callback (/home/developer/Desktop/reason-react/cockpit/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
at process._tickCallback (internal/process/next_tick.js:68:7)
@ multi (webpack)-dev-server/client?http://localhost:9000 webpack/hot/dev-server ./src/index.js ./src/index.scss react-hot-loader/patch
型
6条答案
按热度按时间ego6inou1#
有点晚了,但也许能帮上忙:
我觉得
字符串
应当
型
htrmnn0y2#
安装它:
字符串
7gs2gvoe3#
在终端中运行“npm audit fix”修复了我的问题
1tuwyuhd4#
这条导入路径看起来很混乱。
字符串
它不会在node_modules文件夹中查找fortawesome。
你能显式地尝试从node_modules导入它吗?
例如...
型
另外,我认为它没有在node_modules中查找的原因是因为webpack需要有一个SASS加载器配置项,你有一个,但我很好奇你的路径中的点符号是否会以某种方式抛出webpack。你能试试下面吗?
型
5m1hhzi45#
您可能只是忘记在loader:
test: /\.(sass|scss|css)$/,
中包含css
文件(因为您尝试导入fontawesome.css,而不是scss)kpbwa7wx6#
我也有同样的问题
webpack css-loader不支持(scss和.css捆绑)你需要sass来做
只需安装sass-loader和node-sass(阅读说明)
字符串
webpack
型
注意事项:人们在安装
npm install -d sass-loader
时会犯错误,但是sass-loader带来了最新版本的sass,这意味着你得到了sass-loader:11.0.0或更高版本,所以webpack 4不支持最新的sass-loader解决方案
你的webpack版本是4,所以你只需要低于sass loader版本10
型
我也附上我的webpack给你(供参考)我用的是vue框架
型