const path = require('path');
const { DefinePlugin } = require('webpack');

const babelConfig = require('../babel.config');

const noTranspileLibsRegex = new RegExp('node_modules/(?!(@wealthsimple))');

module.exports = ({ config }) => {
  config.plugins.push(new DefinePlugin({ PROJECT_ROOT: `'${process.cwd()}'` }));
  config.resolve.alias = {
    ...config.resolve.alias,
    'styled-components': path.resolve(
      './',
      'node_modules',
      'styled-components',
    ),
  };

  config.module.rules.push(
    {
      test: /\.mkd$/,
      use: 'raw-loader',
    },
    // Transpile ES6+ to ES5.
    {
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          ...babelConfig,
          plugins: [...babelConfig.plugins, 'dynamic-import-webpack'],
        },
      },
      exclude: noTranspileLibsRegex,
    },
  );

  return config;
};
