import { resolve } from 'path';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';

const exlcudeCleanFiles = ['favicon.png'];

export default {
  resolve: { extensions: ['.js', '.scss'] },
  entry: {
    main: ['./src/newsreader/js/index.js', './src/newsreader/scss/index.scss'],
  },
  output: {
    path: resolve(__dirname, 'src', 'newsreader', 'static'),
    filename: 'js/[name].bundle.js',
    clean: {
      keep: filename => {
        return exlcudeCleanFiles.find(excludedFile => filename.endsWith(excludedFile));
      },
    },
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: { loader: 'babel-loader' },
      },
      {
        test: /\.(sass|scss)$/,
        use: [{ loader: MiniCssExtractPlugin.loader }, 'css-loader', 'sass-loader'],
      },
      {
        test: /\.(ttf|woff|woff2|eot)$/,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext]',
          outputPath: 'fonts',
          publicPath: '/fonts/',
        },
      },
      {
        test: /\.(svg)$/,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext]',
          outputPath: 'images',
          publicPath: '/fonts/',
        },
      },
    ],
  },
  plugins: [new MiniCssExtractPlugin({ filename: 'css/main.css' })],
};
