2
votes

I use webpack with bootstrap, but can't get bootstrap fonts in my application.

Path /Users/and/devel/webpack_bootstrap/ is root folder of project.

I get errors:

jquery.js:9077 GET file:///Users/and/devel/webpack_bootstrap/448c34a56d699c29117adc64c43affeb.woff2 net::ERR_FILE_NOT_FOUND
GET file:///Users/and/devel/webpack_bootstrap/fa2772327f55d8198301fdb8bcfc8158.woff net::ERR_FILE_NOT_FOUND
GET file:///Users/and/devel/webpack_bootstrap/e18bbf611f2a2e43afc071aa2f4e1512.ttf net::ERR_FILE_NOT_FOUND

Files

448c34a56d699c29117adc64c43affeb.woff2, fa2772327f55d8198301fdb8bcfc8158.woff, e18bbf611f2a2e43afc071aa2f4e1512.ttf

are generated into /Users/and/devel/webpack_bootstrap/dist folder after compilation

I tried bootstrap-webpack, but still get the same errors.

package.json

{
  "name": "Webpack_bootstrap",
  "version": "1.0.0",
  "description": "webpack bootstrap test",
  "main": "index.js",
  "scripts": {
    "start": "npm run serve | npm run dev",
    "serve": "./node_modules/.bin/http-server -p 8080",
    "dev": "webpack-dev-server -d --progress --colors --port 8090"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "bootstrap": "^3.3.6",
    "bootstrap-select": "^1.9.3",
    "bootstrap-table": "^1.9.1",
    "bootstrap-webpack": "0.0.5",
    "bower-webpack-plugin": "^0.1.9",
    "css-loader": "^0.23.0",
    "eonasdan-bootstrap-datetimepicker": "^4.15.35",
    "events": "^1.1.0",
    "exports-loader": "^0.6.2",
    "extract-text-webpack-plugin": "^0.9.1",
    "file-loader": "^0.8.5",
    "http-server": "^0.8.5",
    "jquery": "^2.1.4",
    "jquery-resizable-columns": "^0.2.3",
    "jquery-slimscroll": "^1.3.6",
    "jquery-ui": "^1.10.5",
    "less": "^2.5.3",
    "less-loader": "^2.2.2",
    "lodash": "^3.10.1",
    "moment": "^2.10.6",
    "node-sass": "^3.4.2",
    "object-assign": "^4.0.1",
    "path": "^0.12.7",
    "sass-loader": "^3.1.2",
    "select2": "^4.0.1",
    "select2-bootstrap-css": "^1.4.6",
    "style-loader": "^0.13.0",
    "svg-sprite-loader": "0.0.15",
    "typeahead.js": "^0.11.1",
    "url-loader": "^0.5.7",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  }
}

webpack.config.js

var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var BowerWebpackPlugin = require("bower-webpack-plugin");
var path = require('path');

module.exports = {
    cache: true,
    entry: {
      index: './src/script/index.js'
    },
    output: {
        path: path.join(__dirname, './dist'),
        filename: '[name].js',
        sourceMapFilename: "[file].map"
    },
    debug: true,
    devtool: 'source-map',
    module: {
      loaders: [
      // **IMPORTANT** This is needed so that each bootstrap js file required by
      // bootstrap-webpack has access to the jQuery object
      { test: /bootstrap\/js\//, loader: 'imports?jQuery=jquery' },

      // Needed for the css-loader when [bootstrap-webpack](https://github.com/bline/bootstrap-webpack)
      // loads bootstrap's css.
      {
        test: /\.scss$/,
        loaders: [ 'style', 'css', 'sass' ]
      },
      {
        test: /\.less$/,
        loaders: [ 'style', 'css', 'less' ]
      },
      {
        test: /\.css$/,
        loaders: [ 'style', 'css']
      },
      {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'}
    ]
    },
    plugins: [
        new BowerWebpackPlugin(),
        new CommonsChunkPlugin('index', 'index.js', Infinity)
    ],
    resolve: {
        extensions: ['', '.js', '.jsx']
    }
};

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Won</title>
</head>
<body>

    <script type="text/javascript" src="./dist/index.js"></script>

    <div id="content">

      <!-- Button trigger modal -->
      <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        Launch demo modal
      </button>

      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
              <button type="button" class="btn btn-default btn-lg">
                <span class="glyphicon glyphicon-star" aria-hidden="true"></span> Star
              </button>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>

</body>
</html>

src -> script -> index.js

var bootstrap = require('bootstrap-webpack');

Edit: I use this webpack config and everything works.

2
start at github.com/bline/bootstrap-webpack-example, build it back out to what you have? - Mike 'Pomax' Kamermans
When I build this example project, dist folder is empty - Matt
I was able to get the bootstrap-webpack-example working with a couple of config fixes. I have submitted the pull request and you can try cloning and building my fork for now at github.com/fagiani/bootstrap-webpack-example - fagiani
@Matt are you using the gulp build command? what errors do you get? To make sure all dependencies are there I guess you need to use npm install --save-dev after you have gulp installed which you can see details here: github.com/gulpjs/gulp/blob/master/docs/getting-started.md - fagiani
@Matt I'd guess it is set in the karma.conf.js which I believe calls the webpack.config.js. Hope it helps! - fagiani

2 Answers

1
votes

To get the bootstrap-webpack-example working it was required to make a couple of config fixes. You can try cloning and building this github fork for now at https://github.com/fagiani/bootstrap-webpack-example

Make sure you use the gulp build command. To install all dependencies you need to use npm install --save-dev after you have gulp installed which you can see details here: github.com/gulpjs/gulp/blob/master/docs/getting-started.md

In the above project, the entry is set in the karma.conf.js file which calls the webpack.config.js.

1
votes

You must have this structure in your code:

  • index.html
    • dist/
      • index.js
      • fonts....

You are loading the index.js from index.html, but webpack does not know where the public path are, and he use the default root path. Change the respective output to something like this:

output: {
    path: path.join(__dirname, './dist'),
    filename: '[name].js',
    sourceMapFilename: "[file].map",
    publicPath: '/dist'
},