const path = require('path');
const { DefinePlugin } = require('webpack');
const { version } = require('../package.json');
const babelConfig = require('../babel.config');
const noTranspileLibsRegex = new RegExp('node_modules/(?!(@wealthsimple))');

module.exports = ({ config }) => {
  config.plugins.push(
    new DefinePlugin({
      DEFAULT_THEME_NAME: `'${
        process.env.DEFAULT_THEME_NAME || 'wealthsimple'
      }'`,
      STORYBOOK_TITLE: `'Patchwork ${version}'`,
    }),
  );
  config.resolve.alias = {
    ...config.resolve.alias,
    'styled-components': path.resolve(
      './',
      'node_modules',
      'styled-components',
    ),
  };

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

  return config;
};
