'use strict';
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require('terser-webpack-plugin');

function _path(p) {
  return path.join(__dirname, p);
}

module.exports = {
  mode: 'production',
  entry: {
    'polyfills': './build/scripts/polyfills.js',
    'vendor': './build/scripts/vendor.js',
    'main': './build/scripts/main.aot.js'
  },

  context: process.cwd(),

  output: {
    path: path.join(process.cwd(), './dist'),
    filename: '[name].[chunkhash].bundle.js',
    chunkFilename: '[id].[chunkhash].chunk.js'
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [path.resolve(__dirname, "node_modules")],
        options: {
          presets: ['@babel/preset-env']
        }
      },
      {
        test: /\.js$/,
        loader: 'angular-router-loader?aot=true'
      },
      {
        test: /\.html$/,
        loader: 'raw-loader'
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              // you can specify a publicPath here
              // by default it use publicPath in webpackOptions.output
              // publicPath: '../'
            }
          },
          "css-loader"
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico|otf)$/,
        loader: 'file-loader?name=src/resources/[name].[hash].[ext]'
      }
    ]
  },

  optimization: {
    minimizer: [
      new TerserPlugin({
        cache: true,
        parallel: true,
        terserOptions: {
          // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
        }
      })
    ],
    splitChunks: {
      // include all types of chunks
      chunks: 'all'
    }
  },

  plugins: [
    // new BundleAnalyzerPlugin(),
    new HtmlWebpackPlugin({
      template: 'index.webpack.html',
      filename: 'index.html',
      chunksSortMode : 'none'
    }),
    new CopyWebpackPlugin([
      {from: 'index.webpack.html', to: 'index.html'},
      {from: 'src/resources/img/progress.gif', to: 'src/resources/img/progress.gif'},
      {from: 'src/resources/img/logo.png', to: 'src/resources/img/logo.png'},
      {from: 'src/resources/app-config.json', to: 'src/resources/app-config.json'},
      {from: 'src/resources/app.version', to: 'src/resources/app.version'}
    ]),
    new MiniCssExtractPlugin({
      filename: '[name].[hash].css',
      chunkFilename: '[id].[hash].css'
    }),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery",
      Popper: ['popper.js', 'default']
    })
  ],

  resolve: {
    alias: {
      'jquery': _path('node_modules/jquery/dist/jquery.min'),
      'inputmask': _path('node_modules/inputmask/dist/inputmask'),
      'downloadjs': _path('node_modules/downloadjs/download.min.js'),
      'esmarttokenjs': _path('node_modules/esmarttokenjs/esmarttoken.js'),
      'cadesplugin_api': _path('node_modules/cadesplugin_api/index.js'),
      'eonasdan-bootstrap-datetimepicker': _path('node_modules/eonasdan-bootstrap-datetimepicker/src/js/bootstrap-datetimepicker'),
      'autonumeric': _path('node_modules/autonumeric/dist/autoNumeric.js'),
      'jsgantt-improved': _path('node_modules/jsgantt-improved/dist/jsgantt.js'),
      'js-year-calendar': _path('node_modules/js-year-calendar/dist/js-year-calendar.js'),
      'chart.js': _path('node_modules/chart.js/dist/chart.js')
    },
    modules: [
      'node_modules',
      path.resolve(process.cwd(), './build'),
      path.resolve(process.cwd(), './build/scripts'),
    ],
    extensions: ['.js']
  },

  stats: {
    children: false
  },
  devtool: false
};
