Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

reactjs - Error: No module factory available for dependency type: CssDependency on build a nextjs app - mini-css-extract-plugin

I ran npm run build in my nextjs project and i saw this error

Error: No module factory available for dependency type: CssDependency

Build error occurred Error: > Build failed because of webpack errors at build (D:projectsfrontend_vaghtebazi ode_modules extdistuildindex.js:15:918) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:93:5)

my next.config.js file:

const withLess = require('@zeit/next-less');
const lessToJS = require('less-vars-to-js');
const withPlugins = require('next-compose-plugins');
const withOptimizedImages = require('next-optimized-images');

const fs = require('fs');
const path = require('path');

const dotenv = require('dotenv');

dotenv.config();

// Where your antd-custom.less file lives
const themeVariables = lessToJS(fs.readFileSync(path.resolve(__dirname, './static/styles/antd.less'), 'utf8'));

const plugins = [
  withOptimizedImages,
  [
    // withOptimizedImages,
    withLess({
      lessLoaderOptions: {
        javascriptEnabled: true,
        modifyVars: themeVariables, // make your antd custom effective
      },
      webpack: (config, { isServer }) => {
        if (isServer) {
          const antStyles = /antd/.*?/style.*?/;
          const origExternals = [...config.externals];
          config.externals = [
            (context, request, callback) => {
              if (request.match(antStyles)) return callback();
              if (typeof origExternals[0] === 'function') {
                origExternals[0](context, request, callback);
              } else {
                callback();
              }
            },
            ...(typeof origExternals[0] === 'function' ? [] : origExternals),
          ];

          config.module.rules.unshift({
            test: antStyles,
            use: 'null-loader',
          });
        }

        const builtInLoader = config.module.rules.find((rule) => {
          if (rule.oneOf) {
            return (
              rule.oneOf.find((deepRule) => {
                return deepRule.test && deepRule.test.toString().includes('/a^/');
              }) !== undefined
            );
          }
          return false;
        });

        if (typeof builtInLoader !== 'undefined') {
          config.module.rules.push({
            oneOf: [
              ...builtInLoader.oneOf.filter((rule) => {
                return (rule.test && rule.test.toString().includes('/a^/')) !== true;
              }),
            ],
          });
        }

        config.resolve.alias['@'] = path.resolve(__dirname);
        return config;
      },
    }),
  ],
];

const nextConfig = {
  env: {},
};

module.exports = withPlugins(plugins, nextConfig);
question from:https://stackoverflow.com/questions/65844893/error-no-module-factory-available-for-dependency-type-cssdependency-on-build-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had the same issue.

Solutions

When using next-compose-plugins you need to export all plugins alongside their config inside the withPlugin() functions as follow:

exports.module = withPlugin([[withOptimizedImages, { ... }], [withLess, {
      lessLoaderOptions: {
        javascriptEnabled: true,
        modifyVars: themeVariables, // make your antd custom effective
      },
      webpack: (config, { isServer }) => {
        if (isServer) {
          const antStyles = /antd/.*?/style.*?/;
          const origExternals = [...config.externals];
          config.externals = [
            (context, request, callback) => {
              if (request.match(antStyles)) return callback();
              if (typeof origExternals[0] === 'function') {
                origExternals[0](context, request, callback);
              } else {
                callback();
              }
            },
            ...(typeof origExternals[0] === 'function' ? [] : origExternals),
          ];

          config.module.rules.unshift({
            test: antStyles,
            use: 'null-loader',
          });
        }

        const builtInLoader = config.module.rules.find((rule) => {
          if (rule.oneOf) {
            return (
              rule.oneOf.find((deepRule) => {
                return deepRule.test && deepRule.test.toString().includes('/a^/');
              }) !== undefined
            );
          }
          return false;
        });

        if (typeof builtInLoader !== 'undefined') {
          config.module.rules.push({
            oneOf: [
              ...builtInLoader.oneOf.filter((rule) => {
                return (rule.test && rule.test.toString().includes('/a^/')) !== true;
              }),
            ],
          });
        }

        config.resolve.alias['@'] = path.resolve(__dirname);
        return config;
      },
    }]], nextConfig);

If you still face this issue or another, you can still use the next-plugin-antd-less package, and with next-compose-plugins, It seems this package has a workaround for this mini-css-extract-plugin issue. You need to follow the same structure as the @zeit/next-less:

module.exports = withPlugins([
  [withOptimizedImages],
  [
    withAntdLess,
    {
      lessVarsFilePath: "./static/styles/antd.less",
    },
  ],
]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...