【问题标题】:mini-css-extract plugin with postcss throws this.getOptions is not a function带有 postcss 的 mini-css-extract 插件抛出 this.getOptions is not a function
【发布时间】:2021-05-17 02:08:22
【问题描述】:

我正在尝试为我的个人项目设置一个顺风 css。这是一个反应 SSR 应用程序。我在 webpack 配置下的 postcss 设置有问题。它会在每个 *.css 文件(即使是空文件)上引发相同的错误。

似乎无法解析配置文件或默认选项?尝试了不同的配置,但没有效果。最初,我认为这可能与我的 css 文件有关,但如果我删除 postcss 插件,它们都有效并编译

webpack 配置

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');

const paths = require('./paths');

module.exports = {
  entry: {
    index: path.resolve(paths.projectSrc, 'index.js'),
  },
  resolve: {
    alias: {
      '@src': paths.projectSrc,
    },
  },
module: {
  rules: [
  {
    test: /.js$/,
    exclude: /node_modules/,
    use: {
      loader: 'babel-loader',
    },
  },
  {
    test: /\.html$/,
    use: [
      {
        loader: 'html-loader',
        options: { minimize: true },
      },
    ],
    exclude: /node_modules/,
  },
  {
    exclude: /node_modules/,
    test: /\.css$/,
    use: [
      {
        loader: MiniCssExtractPlugin.loader,
        options: {
          publicPath: path.resolve(__dirname, './client-build/css/'),
        },
      },
      {
        loader: 'css-loader',
        options: { importLoaders: 1 },
      },
      {
        loader: 'postcss-loader',
        options: {
          postcssOptions: {
            config: path.resolve(__dirname, 'postcss.config.js'),
          },
        },
      },
    ],
  },
  {
    test: /\.(woff2?|ttf|otf|eot|png|jpg|svg|gif)$/,
    exclude: /node_modules/,
    loader: 'file-loader',
    options: {
      name: './assets/[name].[ext]',
    },
  },
],
},
  plugins: [
    new ESLintPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(paths.public, 'index.html'),
      filename: 'index.html',
    }),
    new MiniCssExtractPlugin({
      filename: '[name].bundle.css',
      chunkFilename: '[id].css',
    }),
    new CopyWebpackPlugin({
      patterns: [{ from: path.resolve(paths.public, 'assets'), to: 'assets' }],
    }),
  ],
  devtool: 'inline-source-map',
};

postcss.config.js

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

控制台输出

【问题讨论】:

    标签: reactjs webpack tailwind-css postcss mini-css-extract-plugin


    【解决方案1】:

    这是由 postcss-loaderv5.0.0 中的重大更改引起的,其中支持版本 4 of webpack 已删除。


    README 声明:

    更新日志

    此项目的所有显着更改都将记录在此文件中。有关提交指南,请参阅 standard-version

    5.0.0 (2021-02-02)

    ⚠ 重大变化

    • 支持的最低webpack 版本是5

    如何解决

    您需要将postcss-loader 降级到v4.2

    Angular 项目的附注

    如果这对其他读者有所帮助:您可能不仅仅在 React 项目中看到这一点。我在将 Angular 8 项目升级到 Angular 11 后发现了这个问题。

    虽然我无法为 React 项目提供帮助,但这个 Angular 提示无论如何也可能有用。 如果您在一个 Angular 项目中,并且您已经尝试通过升级到 webpackv5 来解决此问题,然后使用 v4 of webpack - 您需要按照这些步骤操作。

    1. 如上所述,将postcss-loader 降级为 v4.2
    2. package.json 中删除webpack v5
    3. 删除package.lock.json
    4. 删除node_modules目录。
    5. 运行npm install
    6. 运行ng serveng build
    7. 告诉老板你修好了。

    在此之后,您应该可以使用 tailwindcss 和 Angular。

    【讨论】:

    • 我在版本 8 中使用 less-loader,我的解决方案类似:我必须降级到版本 7 才能与 Webpack 4 一起使用。
    猜你喜欢
    • 2021-11-10
    • 2021-05-30
    • 2015-11-06
    • 2021-06-08
    • 2014-01-31
    • 2021-01-07
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多