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

const webpack = require("webpack");
let config = {
  entry:{
    main: [
      "./src/scripts/vendors/jquery-ui.min.js", "./src/scripts/vendors/json2.js", "./src/scripts/util/bindingHandlers.js", "./src/scripts/util/validationRules.js",
      "./src/scripts/vendors/knockout.sortable.js", "./src/scripts/vendors/knockout.contextmenu.js",
      "./src/scripts/vendors/jquery.bootstrap-touchspin.min.js", "./src/scripts/vendors/jquery.maskedinput.min.js",
      "./src/scripts/vendors/big.js", "./src/scripts/vendors/nprogress.js", "./src/scripts/vendors/toastr.js",  "./src/scripts/vendors/xTimeAgo.js",  
      "./src/scripts/util/helper.js", "./src/scripts/util/htmlHelper.js", "./src/scripts/util/keyboardHandler.js",
       "./src/scripts/util/toast.js", "./src/scripts/util/pasteDataFormatter.js",
       "./src/scripts/util/comparator.js", "./src/scripts/util/commonResources.js",
      "./src/scripts/pages/master/apiMessages.js", "./src/scripts/pages/master/cmdMessages.js", 
      "./src/scripts/pages/master/phraseTranslator.js", "./src/scripts/appConfig.js"
    ],
    login: ["./src/scripts/vendors/json2.js", "./src/scripts/vendors/bootstrap.js", "./src/scripts/vendors/toastr.js",
      "./src/scripts/pages/account/viewModel.js"
    ]
  }, 
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, "dist"),
    clean: true,
  },
  'resolve': {
    'alias': {
      'jquery-ui/sortable': 'jquery-ui/ui/widgets/sortable',
      'jquery-ui/draggable': 'jquery-ui/ui/widgets/draggable',
      'jquery-ui/droppable': 'jquery-ui/ui/widgets/droppable',
    },
  },
  performance: {
    hints: false,
    maxEntrypointSize: 512000,
    maxAssetSize: 512000
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      
    ],
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        styles: {
          name: 'main',
          test: /\.css$/,
          chunks: 'all',
          enforce: true,
        },
      },
    },
  },
  plugins: [
    new HtmlWebpackPlugin({ template: "./index.html" }),
    new MiniCssExtractPlugin({
      filename: `[name].css`
    }),
    new CssMinimizerPlugin({
      minify: CssMinimizerPlugin.cleanCssMinify,
      minimizerOptions: {
        preset: [
          "default",
          {
            discardComments: { removeAll: true },
          },
        ],
      }
    }),
    new webpack.ProvidePlugin({
      $: 'jquery' ,
      jQuery: 'jquery',
      ko: 'knockout',
      knockout: 'knockout',
      'jquery-ui': 'jquery-ui/ui',
      "knockout.validation": "knockout.validation",
      '_h': path.resolve(path.join(__dirname, './src/scripts/util/helper.js')),
      '_autoMapper': path.resolve(path.join(__dirname, './src/scripts/util/autoMapper.js')),
      'dateHelper': path.resolve(path.join(__dirname, './src/scripts/util/dateHelper.js')),
      '_hh': path.resolve(path.join(__dirname, './src/scripts/util/htmlHelper.js')),
      'pasteDataFormatter': path.resolve(path.join(__dirname, './src/scripts/util/pasteDataFormatter.js')),
      'comparator': path.resolve(path.join(__dirname, './src/scripts/util/comparator.js')),
      'toast': path.resolve(path.join(__dirname, './src/scripts/util/toast.js')),
      'confirmBox': path.resolve(path.join(__dirname, './src/scripts/util/confirm.js')),
    })
  ],
    mode: "development",//"development" "production"
  devServer: {
    watchFiles: ["src/**/*", "index.html"],
    static: "./dist",
  },
};

module.exports = config;
