为什么 webpack 无法导入 PrimeReact CSS 个文件?

Why webpack cant import PrimeReact CSS Files?

我想在我的应用程序中使用 PrimeReact DataTable。当我导入 css 个文件时:

import "primereact/resources/themes/nova-light/theme.css";
import "primereact/resources/primereact.min.css";
import "primeicons/primeicons.css";

它抛出这个错误:

ERROR in ./node_modules/primereact/resources/themes/nova-light/theme.css 1:0
Module parse failed: Unexpected character '@' (1:0)
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
> @charset "UTF-8";
| /* open-sans-300 - latin */
| @font-face {
 @ ./src/pages/_app.js 20:0-58
 @ ./src/index.js

ERROR in ./node_modules/primeicons/primeicons.css 1:0
Module parse failed: Unexpected character '@' (1:0)
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
> @font-face {
|     font-family: 'PrimeIcons';
|     font-display: auto;
 @ ./src/pages/_app.js 22:0-35
 @ ./src/index.js

ERROR in ./node_modules/primereact/resources/primereact.min.css 1:0
Module parse failed: Unexpected token (1:0)
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

我把webpack.config.js调整成这样:

var path = require("path");

module.exports = {
  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        exclude: [
          path.resolve("node_modules/primereact/resources/primereact.css"),
          path.resolve(
            "node_modules/primereact/resources/themes/nova-light/theme.css"
          )
        ],
        use: [
          // Creates `style` nodes from JS strings
          "style-loader",
          // Translates CSS into CommonJS
          "css-loader",
          // Compiles Sass to CSS
          "sass-loader"
        ]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },
  externals: {
    Config: JSON.stringify(
      process.env.NODE_ENV === "production"
        ? require("./config.prod.json")
        : require("./config.dev.json")
    )
  }
};

类似问题的其他答案似乎已经过时,因为这样做会引发其他错误或相同的错误。

也许 post 可以回答您的问题。复制整个答案太多了,我相信如果您能够访问此页面,您将能够访问该答案。

Minimum Reproducible Answer

您需要安装es2015预设:

npm install babel-preset-es2015
and then configure babel-loader:

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    exclude: /node_modules/,
    query: {
        presets: ['es2015']
    }
}

这个 post 有解决方案: https://github.com/webpack-contrib/sass-loader/issues/344

我缺少这些装载机:

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