【问题标题】:Bootstrap not applying CSS using WebpackBootstrap 不使用 Webpack 应用 CSS
【发布时间】:2020-03-22 10:41:48
【问题描述】:

我正在使用带有 webpack 的 Bootstrap,但是当我运行我的应用程序时,没有应用 Bootstrap 也没有应用 CSS。

这是我的 webpack.config.js :

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    // mode: 'production',
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'build'),
        publicPath: '',
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: path.join(__dirname, './build'),
        compress: true,
        port: 3000
    },
    performance: {
        maxEntrypointSize: 512000,
        maxAssetSize: 512000
    },
    module: {
        rules: [
            {
              test: /\.s?css$/,
              use: [
                {
                  loader: "file-loader",
                  options: {
                    name: "[name].css"
                  }
                },
                {
                  loader: "extract-loader"
                },
                {
                  loader: "css-loader", 
                },
                {
                  loader: "sass-loader"
                }
              ]
            },
            {
              test: /\.(js|jsx)$/,
              exclude: /node_modules/,
              use: {
                loader: "babel-loader"
              }
            },
            {
              test: /\.(png|svg|jpg|gif)$/,
              use: ["file-loader"]
            },
            {
                test: /\.(woff(2)?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                use: [
                  {
                    loader: 'file-loader',
                    options: {
                      name: '[name].[ext]',
                      outputPath: 'fonts/'
                    }
                  }
                ]
            }
        ]
    },
     //remove comments from JS files
    optimization: {
        minimizer: [
        new UglifyJsPlugin({
            uglifyOptions: {
            output: {
                comments: false,
            },
            },
        }),
        new OptimizeCSSAssetsPlugin({
            cssProcessorPluginOptions: {
            preset: ['default', { discardComments: { removeAll: true } }],
            }
        })
        ],
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "[name].css"
        }),
        new ManifestPlugin(),
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            template: path.resolve('./public/index.html'),
        }),
    ]
};

我在浏览器控制台中遇到了这种错误:

Uncaught Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js):
CssSyntaxError

(192:1) Unknown word

我不知道这个错误是从哪里来的。

【问题讨论】:

  • 请不要复制+粘贴闲聊材料到问题中以破坏内容长度过滤器。如果编辑说您需要编辑问题,请有意义地这样做。

标签: css node.js webpack bootstrap-4 loader


【解决方案1】:

以下两个版本的 dev/prod 以及您可以在此处看到的所有代码 webpack-boilerplate

开发人员:

{
  test: /\.(css|sass|scss)$/,
  use: [
    'style-loader',
    {
      loader: 'css-loader',
      options: {
        importLoaders: 2,
        sourceMap: true
      },
    },
    {
      loader: 'sass-loader',
      options: {
        sourceMap: true,
      },
    }
  ],
},

产品:

{
  test: /\.(css|sass|scss)$/,
  use: [
    MiniCssExtractPlugin.loader,
    {
      loader: 'css-loader',
      options: {
        sourceMap: true,
        importLoaders: 2
      },
    },
    {
      loader: 'sass-loader',
      options: {
        sourceMap: true,
      },
    },
  ],
},

【讨论】:

  • 嘿,谢谢。请问我什么时候可以使用这两个代码?
  • 第一个是开发版,第二个是生产版。您在documentation 中有详细描述
猜你喜欢
  • 2017-03-20
  • 2017-07-10
  • 2020-12-27
  • 2015-11-10
  • 2020-06-28
  • 2017-05-27
  • 2019-09-21
  • 1970-01-01
  • 2021-04-30
相关资源
最近更新 更多