const path = require('path');

module.exports = {
  webpack: {
    alias: {
      '@': path.resolve(__dirname, 'src/')
    },
    configure: (webpackConfig) => {
      // Optimize for production builds
      if (process.env.NODE_ENV === 'production') {
        // Reduce memory usage during build
        webpackConfig.optimization = {
          ...webpackConfig.optimization,
          splitChunks: {
            chunks: 'all',
            cacheGroups: {
              defaultVendors: {
                test: /[\\/]node_modules[\\/]/,
                priority: -10,
                reuseExistingChunk: true,
              },
              default: {
                minChunks: 2,
                priority: -20,
                reuseExistingChunk: true,
              },
            },
          },
          // Use single runtime chunk
          runtimeChunk: 'single',
        };

        // Disable source maps if env variable is set
        if (process.env.GENERATE_SOURCEMAP === 'false') {
          webpackConfig.devtool = false;
        }
      }

      return webpackConfig;
    },
  }
};
