const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");

module.exports = function (_env, argv) {
  const isProduction = _env.mode === "production";
  const isDevelopment = !isProduction;

  console.log(isProduction);

  var config = {
    mode: isDevelopment ? "development" : "production",
    watch: isDevelopment && true,
    devtool: isDevelopment && "cheap-module-source-map",
    module: {
      rules: [
        {
          test: /\.(?:js|mjs|cjs)$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
              presets: [["@babel/preset-env", { targets: "defaults" }]],
            },
          },
        },
      ],
    },
    optimization: {
      minimize: isProduction,
      minimizer: [new TerserPlugin()],
    },
  };

  var epakaAdminConfig = Object.assign({}, config, {
    entry: {
      "epaka-admin": path.resolve(
        process.cwd(),
        "admin",
        "js",
        "src",
        "epaka-admin.js",
      ),
    },
    output: {
      path: path.resolve(__dirname, "admin", "js", "build"),
      filename: "[name].js",
      publicPath: "/",
    },
  });

  var epakaAssetsConfig = Object.assign({}, config, {
    entry: {
      "epaka-form": path.resolve(
        process.cwd(),
        "assets",
        "js",
        "src",
        "epaka-form.js",
      ),
      "epaka-alerts": path.resolve(
        process.cwd(),
        "assets",
        "js",
        "src",
        "epaka-alerts.js",
      ),
    },
    output: {
      path: path.resolve(__dirname, "assets", "js", "build"),
      filename: "[name].js",
      publicPath: "/",
    },
  });

  return [epakaAssetsConfig, epakaAdminConfig];
};
