首页 > 解决方案 > 使用 Bootstrap 3 配置 webpack 4

问题描述

我只想用 Webpack 4 配置 Bootstrap。我MiniCssExtractPlugin用来加载 css 和 sass。这是我的Webpack 配置

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');


module.exports = {
    entry: { main: './src/index.js' },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].[chunkhash].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.scss$/,
                use:  [  'style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            },
            {
                test: /\.(png|woff|woff2|eot|ttf|svg)$/,
                loader: "url?limit=5000"
            }


        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.[contenthash].css',
        }),
        new HtmlWebpackPlugin({
            inject: false,
            hash: true,
            template: './src/index.html',
            filename: 'index.html'
        })

    ]
};

我只想添加引导程序并使用它。这是我的index.js

import 'bootstrap/dist/css/bootstrap.css'
import "./assets/main.scss"

console.log("Hello")

当我想运行它时,我得到了错误:

ERROR in ./node_modules/bootstrap/dist/css/bootstrap.css 7:5
Module parse failed: Unexpected token (7:5)
You may need an appropriate loader to handle this file type.
|  */
| /*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */
> html {
|   font-family: sans-serif;
|   -webkit-text-size-adjust: 100%;
 @ ./src/index.js 3:0-43

有什么帮助吗?欣赏它

标签: javascripttwitter-bootstrapwebpack

解决方案


将此添加到您的配置中。

module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: "babel-loader"
            }
        },
        {
            test: /\.scss$/,
            use:  [  MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
        },

        {
            test: /\.css$/,
            use:  [  MiniCssExtractPlugin.loader, 'css-loader']
        },
        {
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            loader: "url?limit=5000"
        }


    ]
},

推荐阅读