// craco.config.js
const TerserPlugin = require("terser-webpack-plugin");
const CracoLessPlugin = require("craco-less");
const { convertLegacyToken } = require("@ant-design/compatible/lib");
const { theme } = require("antd/lib");

const { defaultAlgorithm, defaultSeed } = theme;

const mapToken = defaultAlgorithm(defaultSeed);
const v4Token = convertLegacyToken(mapToken);

// TODO, At the moment we are using less in the Dashboard. Once we remove this we can remove the less processor entirely.

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: { ...v4Token },
            javascriptEnabled: true
          }
        }
      }
    }
  ],
  webpack: {
    configure: (webpackConfig) => {
      return {
        ...webpackConfig,
        // Required for Dev Server
        devServer: {
          ...webpackConfig.devServer,
          allowedHosts: "all"
        },
        optimization: {
          ...webpackConfig.optimization,
          // Workaround for CircleCI bug caused by the number of CPUs shown
          // https://github.com/facebook/create-react-app/issues/8320
          minimizer: webpackConfig.optimization.minimizer.map((item) => {
            if (item instanceof TerserPlugin) {
              item.options.parallel = 2;
            }

            return item;
          })
        }
      };
    }
  },
  devtool: "source-map"
};
