const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

module.exports = env => {
  let config = {};

  config.entry = [
    './src/login-form/auth-login-form.scss',
    './src/admins/list/auth-admins-list.scss',
    './src/admins/form/auth-admins-form.scss',
  ];

  config.module = {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.(scss)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '../../../../../public/admin-css/[name].css'
            }
          },
          {
            loader: 'extract-loader'
          },
          {
            loader: 'css-loader',
            options: {
              url: false
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: { // ✅ REQUIRED in v7+
                plugins: [      // Move `plugins` inside `postcssOptions`
                  require('autoprefixer'),
                  require('cssnano') // (optional)
                ]
              }
            }
          }
          , {
            loader: 'sass-loader', options: {
              sourceMap: (env === undefined || !env.production)
            }
          }]
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader'
      }
    ]
  };
  config.resolve = {
    alias: {
      'common': path.resolve(__dirname, '../../../Common/assets'),
    },
  };
  config.output = {
    filename: '[name].js',
    path: path.resolve(__dirname, './garbitch')
  }

  if (process.env.NODE_ENV === 'production') {
    config.optimization = {
      minimize: true,
      minimizer: [
        new CssMinimizerPlugin(),
      ],
    }
  }

  return config;
}
