css - Bootstrap 不使用 Webpack 应用 CSS

标签 css node.js webpack bootstrap-4 loader

我将 Bootstrap 与 webpack 结合使用,但是当我运行我的应用时,没有应用任何 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

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

最佳答案

以下两个版本的 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,
      },
    },
  ],
},

关于css - Bootstrap 不使用 Webpack 应用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59060647/

相关文章:

php - 使用 php 从源代码中提取 css 类和 ID

node.js - 你如何在 tensorflow.js 中设置 Adam 优化器学习率?

python - Python-Shell/Node JS输出为空

node.js - 使用命令行参数运行 webpack

javascript - html模板中的Webpack静态文件引用

css - 主 div 不包含其他 div

css - 使用 react-native-web FlatList 的自定义 CSS 滚动条

javascript - Webpack Angular 2 应用类型错误 : Cannot read property 'getOptional' of undefined

php - 我可以将 Javascript 条件的结果导入 PHP 变量吗?

javascript - 使用nodejs将javascript变量通过管道传递到shell命令中