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

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',
     hashFunction: 'sha256',
  },
  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)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'fonts',
            publicPath: '/static/fonts/',
          },
        },
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/main.css',
      allChunks: true,
    }),
    new CleanWebpackPlugin({
      cleanOnceBeforeBuildPatterns: ['js', 'css', 'fonts'],
      cleanAfterEveryBuildPatterns: ['!fonts/**'],
    }),
  ],
};
