一起使用 Webpack 和 Bootstrap 时出错

Errors using Webpack and Bootstrap together

几天来我一直在为这个问题苦思冥想,如果这是一个简单的修复,我深表歉意。我是 Webpack 的新手。

我希望将 Bootstrap 导入到我的项目中,而不是使用 CDN 或任何其他方法。但是我遇到了两个问题:

  1. Bootstrap 未在页面上呈现
  2. 我在尝试加载 .woff 和 .woff2 字形时一直遇到问题

我将在下面提供我的代码,它将向您展示我所在的位置。提前感谢您的帮助!

文件树(为清楚起见,将文件留在外面):

-node_modules
-src
--index.html
--app
---vendor.jsx
--build
---app.js
---vendor.js
-webpack.config.js

index.html

<body>
    <!-- Insert other Body code here -->

    <!-- Webpack Bundle -->
    <script src="build/vendor.js"></script>
    <script src="build/app.js"></script>
</body>

vendor.jsx

// JS dependencies
const $ = require('jquery');
const bootstrap = require('bootstrap');
import React from 'react';
import ReactDOM from 'react-dom';

// CSS files
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';

webpack.config.js

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, './src/build');
var APP_DIR = path.resolve(__dirname, './src/app');

var config = {
    entry: {
        app: APP_DIR + '/app.jsx',
        vendor: APP_DIR + '/vendor.jsx'
    },
    output: {
        path: BUILD_DIR,
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                include: APP_DIR,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.png$/,
                use: 'url-loader?limit=100000'
            },
            {
                test: /\.jpg$/,
                use: 'file-loader'
            },
            {
                test: /\.(woff|woff2) (\?v=\d+\.\d+\.\d+)?$/,
                use: 'url-loader?limit=10000&mimetype=application/font-woff'
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                use: 'url-loader?limit=10000&mimetype=application/octet-stream'
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, 
                use: 'file-loader'
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, 
                use: 'url-loader?limit=10000&mimetype=image/svg+xml'
            }
        ]
    }
};

module.exports = config;

编辑:

@InfiniteStack 要求提供错误日志,因此包含在下面:

Chrome调试错误:

Uncaught Error: Module parse failed: C:\Users\{user}\Code\ref-data-mapper\node_modules\bootstrap\dist\fonts\glyphicons-halflings-regular.woff2 Unexpected character '

Webpack 错误:

ERROR in ./~/bootstrap/dist/fonts/glyphicons-halflings-regular.woff Module parse failed: C:\Users\{user}\Code\ref-data-mapper\node_modules\bootstrap\dist\fonts\glyphicons-halflings-regula r.woff Unexpected character ' ' (1:4) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file) @ ./~/css-loader!./~/bootstrap/dist/css/bootstrap.css 6:4707-4760 @ ./~/bootstrap/dist/css/bootstrap.css @ ./src/app/vendor.jsx

ERROR in ./~/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2 Module parse failed: C:\Users\{user}\Code\ref-data-mapper\node_modules\bootstrap\dist\fonts\glyphicons-halflings-regula r.woff2 Unexpected character ' ' (1:4) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file) @ ./~/css-loader!./~/bootstrap/dist/css/bootstrap.css 6:4622-4676 @ ./~/bootstrap/dist/css/bootstrap.css @ ./src/app/vendor.jsx

感谢@InfiniteStack 和 webpack/webpack 的 Gitter 工作人员,我现在有了完整的答案。

在 webpack.config.js 中,对 url-loader 的引用 just url-loader?limit100000 在进一步修改之前是必要的。

同样,jQuery 需要在靠近底部的 webpack 中定义为插件。需要在 webpack.config.js 中进行所有更改。我完成的文件如下:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, './src/build');
var APP_DIR = path.resolve(__dirname, './src/app');

var config = {
    entry: {
        app: APP_DIR + '/app.jsx',
        vendor: APP_DIR + '/vendor.jsx'
    },
    output: {
        path: BUILD_DIR,
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                include: APP_DIR,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.(png|woff|woff2|eot|ttf|svg)$/,
                use: 'url-loader?limit=100000'
            },
            {
                test: /\.png$/,
                use: 'url-loader?limit=100000'
            },
            {
                test: /\.jpg$/,
                use: 'file-loader'
            },
            {
                test: /\.(woff|woff2) (\?v=\d+\.\d+\.\d+)?$/,
                use: 'url-loader?limit=10000&mimetype=application/font-woff'
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                use: 'url-loader?limit=10000&mimetype=application/octet-stream'
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, 
                use: 'file-loader'
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, 
                use: 'url-loader?limit=10000&mimetype=image/svg+xml'
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            jQuery: 'jquery',
            $: 'jquery',
            jquery: 'jquery'
        })
    ]
};

module.exports = config;

所以长话短说;博士: 将 jQuery 定义为插件 在添加 extras

之前使用 url-loader 定义所有非 js 资产

如果其他人有更多关于为什么这有效的答案,如果你想 PM/message 我愿意接受。

再次感谢!