text: const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  const HtmlWebpackPlugin = require('html-webpack-plugin');
  const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  const paths = require('../utils/paths');
  const path = require('path');
  const fs = require('fs')
  const htmlPlugins = generateHtmlPlugins(`${paths.SRC_DIR}/html/views`);
  
  module.exports = env => ({
    devtool: 'source-map',
    output: {
      filename: '[name].[contenthash].js',
    },
    optimization: {
      moduleIds: 'hashed',
      runtimeChunk: 'single',
      splitChunks: {
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name: 'vendors',
            chunks: 'all',
          },
        },
      },
    },
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
        },
      ],
    },
    plugins: [
      ...htmlPlugins,
      new MiniCssExtractPlugin({
        filename: '[name].[contenthash].css',
        chunkFilename: '[name].[id].[contenthash].css',
      }),
      new OptimizeCssAssetsPlugin({}),
    ],
  });
  
  function generateHtmlPlugins(templateDir, isProduction) {
    const templateFiles = fs.readdirSync(path.resolve(__dirname, templateDir));
  
    return templateFiles.map(item => {
      const parts = item.split('.');
      const name = parts[0];
      const extension = parts[1];
      console.log(`${templateDir}/${name}.${extension}`);
  
      const minify = {
        collapseWhitespace: true,
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        useShortDoctype: true,
      }
      const htmlWebpackPluginSettings = {
        filename: `${name}.html`,
        template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`),
        // inject: false,
      }
  
      if(isProduction) htmlWebpackPluginSettings.minify = minify
  
      return new HtmlWebpackPlugin(htmlWebpackPluginSettings)
    })
  }
  
  