简体   繁体   English

在Webpack中加载CSS

[英]loading CSS in Webpack

I am trying to include the bootstrap css file in my app using Webpack (v4.5) using the instructions found on the offical website ( https://getbootstrap.com/docs/4.0/getting-started/webpack/ ) but am getting the following error when running webpack. 我正在尝试使用官方网站( https://getbootstrap.com/docs/4.0/getting-started/webpack/ )上的说明,使用Webpack(v4.5)在我的应用程序中包含bootstrap css文件,但是运行webpack时出现以下错误。

ERROR in ./node_modules/bootstrap/dist/css/bootstrap.min.css Module parse failed: Unexpected token (6:3) You may need an appropriate loader to handle this file type.

I have tried all I can think of to get this working but am afraid I am probably missing something really obvious. 我已尽我所能使此工作奏效,但恐怕我可能遗漏了一些明显的东西。

I import the css into my code using: import 'bootstrap/dist/css/bootstrap.min.css'; 我使用以下命令import 'bootstrap/dist/css/bootstrap.min.css'; css导入我的代码中: import 'bootstrap/dist/css/bootstrap.min.css';

And my webpack config looks like: 我的webpack配置看起来像:

  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /js$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader',
        query: { presets: ['react-app'] },
      },

    ],
  }

I have both css-loader and style-loader installed and am using the same webpack config that bootstrap displays in their docs. 我同时安装了css-loaderstyle-loader并且正在使用引导程序在其文档中显示的同一webpack配置。

Can anyone see what I am doing wrong? 谁能看到我在做什么错?

Do this first 先做这个

npm install --save-dev css-loader

and then this 然后这个

npm install --save-dev style-loader

After installing change your configuration like this 安装后,像这样更改您的配置

{
    test: /\.css$/,
    exclude: /node_modules/,
    use: ExtractTextPlugin.extract({
       fallback: "style-loader",
       use: [
           {
              loader: 'css-loader'
           },
           {
              loader: 'style-loader'
           }
        ]
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM