0

我目前正在使用带有自定义 cra 的 react-app-rewired。目前,npm start可以工作,但npm run build会出现以下错误。

Error: You forgot to add 'mini-css-extract-plugin' plugin

不完全确定发生了什么,这是我的 config-overrides.js 文件。我尝试在此处添加插件,但由于某种原因无法正常工作,并且我继续收到此错误。

// Overriding CreateReactApp settings, ref: https://github.com/arackaf/customize-cra
const {
  override,
  fixBabelImports,
  addLessLoader,
  useEslintRc,
  addDecoratorsLegacy,
  useBabelRc,
  addWebpackPlugin
} = require('customize-cra')
// eslint config
const eslintConfig = require('./.eslintrc.js');

const useEslintConfig = configRules => config => {
  config.module.rules = config.module.rules.map(
    rule => {
      // Only target rules that have defined a `useEslintrc` parameter in their options
      if (rule.use && rule.use.some(use => use.options && use.options.useEslintrc !== void 0)) {
        const ruleUse = rule.use[0]
        const baseOptions = ruleUse.options
        const baseConfig = baseOptions.baseConfig || {}
        ruleUse.options = {
          useEslintrc: false,
          ignore: true,
          baseConfig: { ...baseConfig, ...configRules },
        }
        return rule

        // Rule not using eslint. Do not modify.
      } else {
        return rule
      }
    }
  );
  return config;
}

module.exports = override(
  addDecoratorsLegacy(),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: 'css', // dont use true
  }),
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useEslintRc(),
  addLessLoader({
    lessOptions: {
      javascriptEnabled: true,
    },
  }),
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useEslintConfig(eslintConfig),
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useBabelRc(),
);

知道为什么吗?

4

1 回答 1

0

我的解决方案是在config-overrides.js

const {
  override,
  ...
  addWebpackPlugin, // <- added this line here
} = require('customize-cra')

const MiniCssExtractPlugin = require('mini-css-extract-plugin') // <- added this line here

module.exports = override(
  ...
  addWebpackPlugin(new MiniCssExtractPlugin()), // <- added this line here
)

之后,构建过程成功,没有任何问题。

于 2022-02-15T07:09:37.717 回答