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

module.exports = ({ config }) => {
  config.resolve.modules =  ['node_modules'];
  config.module.rules.push({
    test: /\.stories\.tsx?$/,
    loaders: [require.resolve('@storybook/source-loader')],
    enforce: 'pre',
  });
  config.module.rules.push({
    test: /\.less$/,
    include: /node_modules|antd\.less/,
    use: [
      "style-loader",
      "css-loader",
      {
        loader: "less-loader",
        options: {
          javascriptEnabled: true
        }
      }
    ]
  });
  config.module.rules.push({
    test: /\.less$/,
    use: [
    'style-loader', 
    {
      loader: 'css-loader',// translates CSS into CommonJS
      options: {
        modules: {
          localIdentName: '[name]__[local]___[hash:base64:5]',
        },
      }
    },
    'postcss-loader',
    {
      loader: 'less-loader',
      options: {
        javascriptEnabled: true,
      },
    }],
    exclude: /node_modules|antd\.less/,
  });
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    use: [
      {
        loader: require.resolve('babel-loader'),
        options: {
          presets: [['react-app', { flow: false, typescript: true }]]
        }
      },
      // Optional
      {
        loader: require.resolve("react-docgen-typescript-loader")
      },
    ]
  });
  config.resolve.extensions.push(".ts", ".tsx");
  config.resolve.alias = {
    ...config.resolve.alias,
    '@': `${path.resolve(fs.realpathSync(process.cwd()), './src')}`,
    '@components': `${path.resolve(fs.realpathSync(process.cwd()), './src')}/components`,
  }
  config.output.library = "[name]";
  return config;
};
