本文介绍了如何配置 webpack 以在 React 中加载 glyphicons/font-awesome 图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在反应项目中使用引导程序时,运行开发环境工作正常.但是当我为包项目运行 npm build 时.显示引导字体错误.
When use bootstrap in react project, run dev environment work fine. but when I run npm build for package project. show bootstrap font error.
ERROR in ./~/bootstrap/dist/fonts/glyphicons-halflings-regular.eot
Module parse failed: /Users/xx/WorkDir/react-leap/node_modules/bootstrap/dist/fonts/glyphicons-halflings-regular.eot Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '�' (1:0)
at Parser.pp.raise (/Users/xx/WorkDir/react-leap/node_modules/webpack/node_modules/acorn/dist/acorn.js:923:13)
at Parser.pp.getTokenFromCode (/Users/xx/WorkDir/react-leap/node_modules/webpack/node_modules/acorn/dist/acorn.js:2831:8)
我的 Webpack 配置
module: {
loaders: [
{
test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: 'file-loader',
include: paths
}
]
}
引导程序版本:
"bootstrap": "3.3.6",
推荐答案
我遇到了一个解决方案:
I have come across a solutions to this:
webpack.config.js
module: {
preLoaders: [
{test: /\.js$/, exclude: /node_modules/, loader: 'jshint-loader'}
],
loaders: [
{test: /bootstrap\/js\//, loader: 'imports?jQuery=jquery' },
{test: /\.less$/, loader: 'style-loader!css-loader!less-loader'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{test: /\.woff(2)?(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" }
]
}
确保安装所有这些加载程序
Make sure to install all these loaders
npm i jshint-loader style-loader css-loader less-loader url file --save-dev
在您的文件中导入引导程序后
After this import bootstrap in you file as
require('bootstrap/less/bootstrap.less');
这篇关于如何配置 webpack 以在 React 中加载 glyphicons/font-awesome 图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!