字体很棒,没有在Bootstrap中加载图标

时间:2015-10-24 06:35:39

标签: javascript twitter-bootstrap-3 font-awesome webpack webpack-style-loader

我正在尝试使用 Webpack Bootstrap 字体非常棒进行简单的要求。我设法加载了Bootstrap CSS但Font-awesome无法正常工作。

这就是我得到的结果:

enter image description here

我的代码如下。我在这里做错了什么?

资源:Github code可在此处获取。

的package.json

{
  "name": "",
  "version": "0.0.1",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack -w"
  },
  "license": "MIT",
  "devDependencies": {
    "css-loader": "^0.21.0",
    "expose-loader": "^0.7.0",
    "file-loader": "^0.8.4",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.6",
    "webpack": "^1.12.2"
  },
  "dependencies": {
    "admin-lte": "^2.3.2",
    "bootstrap": "^3.3.5",
    "bootstrap-webpack": "0.0.5",
    "exports-loader": "^0.6.2",
    "extract-text-webpack-plugin": "^0.8.2",
    "font-awesome": "^4.4.0",
    "font-awesome-webpack": "0.0.4",
    "imports-loader": "^0.6.5",
    "jquery": "^2.1.4",
    "less": "^2.5.3",
    "less-loader": "^2.2.1"
  }
}

入口点: index.js

var $ = require('jquery');

require('expose?$!expose?jQuery!jquery');

require("bootstrap-webpack");
require('./node_modules/font-awesome/css/font-awesome.css');

require('./resources/css/style.css');


$('h1').html('<i class="fa fa-bars"></i> this is bootstrap');
console.log('hi there');

Webpack配置文件 webpack.config.js

module.exports = {

    entry: './index.js',
    output: {
        path: 'public/assets/',
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    module: {
        loaders: [

            {test: /\.css$/, loader: "style!css!" },
            { test: /\.less$/, loader: "style!css!less!" },

            //bootstrap
            {test: /\.(woff|woff2)(\?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'},

            //font-awesome
            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }



        ]
    },

} 

2 个答案:

答案 0 :(得分:7)

检查您的F12调试控制台,并查看有关给定文件错误的Web浏览器详细信息。

您的网络服务器没有使用正确的mime类型提供.woff和.woff2文件,因此从浏览器 - 客户端角度来看它们根本没有加载。

您需要修改Web服务器端以添加对.woff文件的支持,例如在您应该具有的Web.config中的IIS Express上:

  <system.webServer>
    <staticContent>
      <mimeMap fileExtension="woff" mimeType="application/font-woff" />
      <mimeMap fileExtension="woff2" mimeType="application/font-woff" />
    </staticContent>
  </system.webServer>

并且在webpack.config.js中你应该有像

这样的模块加载器
    {
        test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
        loader: 'url-loader'
    }

答案 1 :(得分:2)

我知道这是一个1岁的问题,但万一有人最终会从谷歌出来:

css-loader generating wrong file paths存在一个已知问题。这不会在生产构建中发生,但在开发模式下渲染方块时仍然很烦人。

在修复此问题之前,您可以禁用css-loader源映射以查看在开发中正确呈现的字体。在webpack配置中找到写css?sourceMap的位置并将其更改为css