【问题标题】:Webpack css-loader not handling the css although the configuration appears to be alrightWebpack css-loader 不处理 css,尽管配置似乎没问题
【发布时间】:2021-07-02 11:02:11
【问题描述】:

我目前正在学习 webpack,但我无法弄清楚这一点。我真的很感激一些帮助。 我收到此错误:

ERROR in ./src/style.css 1:5
Module parse failed: Unexpected token (1:5)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> body {
|     background-color: red;
|     color: white;
 @ ./src/index.js 1:0-20

我已经导入了 css-loader 和 style-loader:npm i -D css-loader style-loader。 package.json 文件如下所示:

  {
  "name": "webpack-styles",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^5.2.6",
    "style-loader": "^3.0.0",
    "webpack": "^5.42.0",
    "webpack-cli": "^4.7.2"
  }
}

webpack.config.js 看起来像这样:

module.exports = {
    module: {
      rules: [
        {
          test: /\.css$/i,
          use: [ "css-loader", "style-loader"],
        },
      ],
    },
  };

我已将 css 文件导入 index.js,如下所示:

import "./style.css"

谁能看到问题可能是什么? 谢谢!

【问题讨论】:

    标签: webpack css-loader


    【解决方案1】:

    尝试将您的 webpack.config.js 替换为:

    module.exports = {
      module: {
        rules: [
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
        ],
      },
    };
    

    注意顺序:

    1. style-loader第一
    2. css-loader

    配置是顺序敏感的,和 webpack 文档中的完全一样: https://webpack.js.org/loaders/style-loader/#root

    【讨论】:

    • 哦,我明白了。所以它的行为就像一个链式函数?我不认为订单很重要。谢谢马辛!
    猜你喜欢
    • 2018-04-04
    • 2018-08-06
    • 2019-08-24
    • 2019-06-26
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多