Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

reactjs - How to configure webpack to load glyphicons/font-awesome icons in React

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)

My Webpack config

module: {
  loaders: [
    {
      test: /.(ttf|eot|svg|woff(2)?)(?[a-z0-9=&.]+)?$/,
      loader: 'file-loader',
      include: paths
    }
  ]
}

Bootstrap version:

"bootstrap": "3.3.6",
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...