const WebpackRemoveEmptyScripts = require("webpack-remove-empty-scripts");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ConfigBuilderPlugin = require("./webpack/config-builder-plugin");
const path = require("path");
const entry = require("./webpack.entry");
const { BuildHelper } = require("./webpack/webpack-helpers");
const isDevel =
  process.argv[process.argv.indexOf("--mode") + 1] === "development";

const config = {
  entry: new BuildHelper(entry).build(),
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "[name].js",
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.(scss|sass)$/,
        use: [
          MiniCssExtractPlugin.loader,
          { loader: "css-loader" },
          {
            loader: "postcss-loader",
            options: {
              plugins: function () {
                return [require("precss"), require("autoprefixer")];
              },
            },
          },
          { loader: "sass-loader" },
        ],
      },
      { test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/, loader: "url-loader" },
      {
        test: /\.(json|jsonc)$/,
        loader: path.resolve(__dirname, "webpack/jsonc-loader.js"),
      },
    ],
  },
  plugins: [
    new WebpackRemoveEmptyScripts(),
    new MiniCssExtractPlugin({
      filename: "[name].css",
    }),
    new ConfigBuilderPlugin({
      chunkName: "config",
    }),
  ],
  devtool: isDevel ? "inline-source-map" : undefined,
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    alias: {
      "@assets": path.resolve(__dirname, "assets"),
      "@lib": path.resolve(__dirname, "assets/js/lib"),
      "@extensions": path.resolve(__dirname, "assets/js/lib/extensions"),
      "@prototypes": path.resolve(__dirname, "assets/css/prototypes"),
      "@molecules": path.resolve(__dirname, "assets/css/molecules"),
    },
  },
  externals: {
    jquery: "jQuery",
    react: "React",
    "react-dom": "ReactDOM",
  },
};

module.exports = config;
