const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const WorkboxPlugin = require("workbox-webpack-plugin");
const HappyPack = require("happypack");
const webpack = require("webpack");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  chainWebpack(config) {
    config.module.rule("svg").exclude.add(resolve("src/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();

    config.plugin("happypack").use(HappyPack, [
      {
        loaders: ["babel-loader"],
      },
    ]);

    // config.plugin('dll').use(webpack.DllPlugin, [
    //   {
    //     name: '[name]_library',
    //     path: path.join(__dirname, 'dll', '[name]-manifest.json'),
    //   },
    // ]);

    config.plugin("workbox").use(WorkboxPlugin.GenerateSW, [
      {
        clientsClaim: true,
        skipWaiting: true,
      },
    ]);
  },
  css: {
    loaderOptions: {
      postcss: {
        postcssOptions: {
          plugins: [require("tailwindcss"), require("autoprefixer")],
        },
      },
    },
  },
  devServer: {
    proxy: {
      "/api": {
        // target: "http://192.168.5.4:16200", // 石月
        target: "http://192.168.50.235:16200",//内网
        // target: "http://106.120.102.238:16600",//外网
        changeOrigin: true,
        pathRewrite: {
          "^/api": "", // 需要rewrite重写的,
        },
      },
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
    optimization: {
      usedExports: true,
      splitChunks: {
        chunks: "all",
      },
      minimize: true,
      minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
    },
  },
};
