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

const isDevelopmentMode = process.env.dev === "development";

module.exports = {
  mode: isDevelopmentMode ? "development" : "production",
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.(jsx|tsx)$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          cacheDirectory: true,
          presets: ["@babel/preset-env", "@babel/preset-typescript"],
          plugins: [
            "transform-class-properties",
            "@babel/plugin-syntax-jsx",
            "@babel/plugin-transform-react-jsx",
            "@babel/plugin-transform-runtime",
          ],
        },
      },
      {
        test: /\.(js|ts)$/,
        loader: "babel-loader",
        exclude: /node_modules|libs/,
        options: {
          cacheDirectory: true,
          presets: ["@babel/preset-env", "@babel/preset-typescript"],
          plugins: [
            "transform-class-properties",
            "@babel/plugin-transform-runtime",
          ],
        },
      },
      {
        test: /\.less$/i,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "less-loader",
        ],
      },
      {
        test: /\.css$/i,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader"
        ],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "sass-loader",
        ],
      },
      {
        test: /\.txt$/,
        use: [
          {
            loader: "raw-loader",
          },
        ],
      },
      {
        test: /\.svg$/,
        loader: "svg-inline-loader",
      },
      {
        test: /\.(ttf|eot|woff|woff2)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "fonts/[name].[ext]",
            },
          },
        ],
      },
      {
        test: /\.tpl$/,
        use: [
          {
            loader: "raw-loader",
          },
        ],
      },
      {
        test: /\.(png|jpg)$/,
        loader: "url-loader",
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.jsx', '.js', "json"],
    alias: {
      "@": __dirname
    }
  },
  output: {
    filename: 'suanpan-sdk.dashboard.js',
    path: path.resolve(__dirname, 'dist'),
  },
  optimization: {
    minimizer: [
      `...`,
      new CssMinimizerPlugin(),
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/dashboard/template/dashboard.html')
    }),
    new MiniCssExtractPlugin({ filename: "suanpan-sdk.dashboard.css" }),
    new webpack.ProvidePlugin({
      "prop-types": "prop-types",
      "React": "react",
      "window.React": "react",
      $: "jquery"
    }),
    new webpack.DefinePlugin({
      VERSION_PLACEHOLDER: "1.0",
      DEBUG: isDevelopmentMode
    })
  ],
  devtool: "source-map",
  devServer: {
    client: {
      overlay: {
        warnings: false,
        errors: true
      }
    },
    static: {
      directory: path.join(__dirname, '')
    },
    compress: true,
    port: 3000
  }
};