javascript - 将 Bootstrap 与 Webpack 结合使用

标签 javascript twitter-bootstrap webpack

我有一个使用 Webpack 的应用程序。我是 Webpack 的新手,正在尝试学习如何有效地使用它。具体来说,我正在尝试将 Bootstrap with Font Awesome 导入到我的项目中。我找到了这个 SO post但是,我仍然无法使用 Bootstrap。我不确定它是否已过时,或者我是否误解了某些内容。

我尝试通过 url-loader 加载 Bootstrap 和 Font Awesome。我引用了以下 URL:

https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css
https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css

我还尝试通过 NPM 加载 Bootstrap 和 Font Awesome,然后在我的入口文件中引用它,如下所示:

require('bootstrap');
require('font-awesome');

这似乎应该是常用模板的一部分。但是,我没有找到。如何在 Webpack 中使用 Bootstrap 和 Font Awesome?

但是,我也对这种方法感到失望。

最佳答案

我在 GitHub 上创建了一个简单示例.使用了 Webpack 2 和 Bootstrap 3。

安装依赖npm install jquery bootstrap

index.js

require('bootstrap');
require('bootstrap/dist/css/bootstrap.css');
require('font-awesome/css/font-awesome.css'); //Optional. The question author uses this package.

网页包

const webpack = require('webpack');
const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
    entry: path.resolve(__dirname, "index.js"),
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: "bundle.js"
    },
    module: {
        rules: [    
            {
                test: /\.woff2?$|\.ttf$|\.eot$|\.svg$/,
                use: [{
                    loader: "file-loader"
                }]
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader'
                })
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('bundle.styles.css'),
        new webpack.ProvidePlugin({
            // inject ES5 modules as global vars
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        })
    ]
};

index.html

<link rel="stylesheet" type="text/css" href="/dist/bundle.styles.css">
<script type="text/javascript" src="/dist/bundle.js"></script>

如果不想手动插入 bundle.styles.cssbundle.js,可以使用 HtmlWebpackPlugin

关于javascript - 将 Bootstrap 与 Webpack 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44925969/

相关文章:

javascript - Chrome 中 chrome.app.isInstalled 始终返回 false

javascript - 我如何编写一个函数,如果它的参数是一个 Node 集合,则对 javascript 中的所有 Node 元素执行?

jquery - Bootstrap 3 折叠菜单在移动屏幕上不起作用

css - Bootstrap 4 垂直对齐文本不会在卡片上居中

javascript - Webpack HMR 不适用于 Node-Webkit/NW.js

javascript - thymeleaf 如何将 id 从下拉列表传递到另一个 html

Javascript:使用元组作为字典键

javascript - 根据下拉菜单中选定的值启用/禁用表单中的元素的最佳方法?

javascript - 为什么使用这个resolve.alias配置时Webpack会抛出错误?

unit-testing - 如何使用 Jasmine 和 Webpack 运行 Typescript(不是 AngularJs)单元测试