const {
  override,
  addLessLoader,
  disableEsLint,
  addBundleVisualizer,
  addWebpackAlias,
  fixBabelImports,
  addWebpackPlugin
} = require("customize-cra")


const path = require('path');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')

let instance = new HardSourceWebpackPlugin({
  // Either an absolute path or relative to webpack's options.context.
  cacheDirectory: 'node_modules/.cache/hard-source/[confighash]',
  // Either a string of object hash function given a webpack config.
  configHash: function (webpackConfig) {
    // node-object-hash on npm can be used to build this.
    return require('node-object-hash')({ sort: false }).hash(webpackConfig);
  },
  // Either false, a string, an object, or a project hashing function.
  environmentHash: {
    root: process.cwd(),
    directories: [],
    files: ['package-lock.json', 'yarn.lock'],
  },// How to launch the extra processes. Default:
  fork: (fork, compiler, webpackBin) => fork(
    webpackBin(),
    ['--config', __filename], {
    silent: true,
  }
  ),
  // Number of workers to spawn. Default:
  numWorkers: () => require('os').cpus().length,
  // Number of modules built before launching parallel building. Default:
  minModules: 10,
  // An object.
  info: {
    // 'none' or 'test'.
    mode: 'none',
    // 'debug', 'log', 'info', 'warn', or 'error'.
    level: 'debug',
  },
  // Clean up large, old caches automatically.
  cachePrune: {
    // Caches younger than `maxAge` are not considered for deletion. They must
    // be at least this (default: 2 days) old in milliseconds.
    maxAge: 2 * 24 * 60 * 60 * 1000,
    // All caches together must be larger than `sizeThreshold` before any
    // caches will be deleted. Together they must be at least this
    // (default: 50 MB) big in bytes.
    sizeThreshold: 50 * 1024 * 1024
  },
})

module.exports = override(
  disableEsLint(),
  addBundleVisualizer(),
  addWebpackAlias({
    "educoder": path.resolve(__dirname, 'src/common/educoder.js')
  }),
  addLessLoader({
    javascriptEnabled: true
  }),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true
  }),
  addWebpackPlugin(new MonacoWebpackPlugin({})),
  addWebpackPlugin(instance),
  (config) => {
    config.resolve.plugins = config.resolve.plugins.filter(plugin => !(plugin instanceof ModuleScopePlugin));
    if (process.env.NODE_ENV !== "development") {
      config.output.publicPath = `/react/build/`;
    }
    return config
  }
);