const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');

const isProduction = process.env.NODE_ENV === 'production';

const config = {
  entry: './assets/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    clean: true,
    filename: '[name].[contenthash].js'
  },
  devServer: {
    open: true,
    host: 'localhost',
  },
  plugins: [
      new WebpackManifestPlugin({ publicPath: "" }),
      new MiniCssExtractPlugin({
      filename:'[name].[contenthash].css'
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'assets/'),
    },
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/i,
        loader: 'babel-loader',
      },
      {
        test: /\.s[ac]ss$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', {
          loader: 'sass-loader',
          options: {
            api: 'modern-compiler',
            sassOptions: {
              silenceDeprecations: ['mixed-decls', 'color-functions', 'global-builtin', 'import'],
            }
          }
        }],
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
        type: 'asset',
      },


      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ],
  },
};

module.exports = () => {
  if (isProduction) {
    config.mode = 'production';
  } else {
    config.mode = 'development';
  }
  return config;
};
