尝试使用webpack加载font-awesome会给我这个错误:
ERROR in ./~/font-awesome/fonts/fontawesome-webfont.eot?v=4.6.3
Module parse failed: ...\node_modules\font-awesome\fonts\fontawesome-webfont.eot?v=4.6.3 Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
我正在尝试在这样的React组件中导入font-awesome:
import "font-awesome/scss/font-awesome.scss";
这是我的webpack.config.js的样子。我想将font-awesome和相关资源与其他所有内容分开加载,即因为我已经加载了带有babel的SVG,但是不想用font-awesome来做这件事。
{
test: /\.scss/,
loader: ExtractPlugin.extract("style", "css!sass")
},
{
test: /\.svg$/,
loader: "babel!svg-react",
exclude: /node_modules/
},
{
test: /\.ttf$/,
loader: "file-loader?mimetype=application/octet-stream&name=[name].[ext]",
exclude: /node_modules/
},
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff&name=[name].[ext]",
exclude: /node_modules/
},
{
test: /\.(otf|eot|png|svg|ttf|woff|woff2)(\?[\s\S]+)?$/,
loader: "url?limit=10000",
include: "node_modules/font-awesome/fonts"
}
我也尝试过这样的正则表达式而且它不起作用:
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url?limit=10000" }
答案 0 :(得分:13)
要正确加载font-awesome字体,您需要在webpack.config.js中配置加载器以使用url-loader和file-loader。例如:
module.exports = {
module: {
loaders: [
// the url-loader uses DataUrls.
// the file-loader emits files.
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url-loader?limit=10000&mimetype=application/font-woff"
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader"
},
]
}
};
为此,您需要npm install font-awesome-sass-loader npm i font-awesome-sass-loader
您还需要url-loader npm i url-loader
和文件加载器npm i file-loader
答案 1 :(得分:9)
在尝试了许多例子后,这对我有用:
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" },
{ test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" },
并且肯定需要在主文件中导入fontawsome
import 'font-awesome/css/font-awesome.css';
也不要忘记安装加载器
npm install url-loader --save-dev
答案 2 :(得分:2)
尝试使用.eot
代替file-loader
加载url-loader
个文件。
示例:
module: {
loaders: [
{
test: /\.(eot|ttf|woff2?|otf|svg|png|jpg)$/,
loaders: ['file']
}
]
}