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");
// const HtmlWebpackPlugin = require("html-webpack-plugin");
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")],
        },
      },
      sass: {
        additionalData: `
          @import "@/styles/global.scss";
        `,
      },
    },
  },

  devServer: {
    // contentBase: path.join(__dirname, "public"),
    proxy: {
      "/api": {
        // target: "http://192.168.5.4:16200", // 石月
        target: "http://192.168.50.235:16200", //内网 开发环境
        // target: "http://192.168.5.15:16200",//陈宏琰
        // target: "http://192.168.50.235:16300", //内网 生产环境
        // target: "http://106.120.102.238:16600", //外网 生产环境
        //  target: "http://10.96.137.5",//大～^唐
        changeOrigin: true,
        pathRewrite: {
          "^/api": "", // 需要regit write重写的,
        },
      },
      "/transDataWeb": {
        target: "http://192.168.50.241:9000/trans_data_web",
        changeOrigin: true,
        pathRewrite: {
          "^/transDataWeb": "", // 需要regit write重写的,
        },
      },
      // "/downLoadApi": {
      //   target: "http://192.168.50.241:8080", //http://192.168.50.241:8080/
      //   changeOrigin: true,
      //   pathRewrite: {
      //     "^/downLoadApi": "", // 需要rewrite重写的,z
      //   },
      // },
    },
  },

  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
    optimization: {
      usedExports: true,
      splitChunks: {
        chunks: "all",
      },
      minimize: true,
      minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
    },
    // plugins: [
    //   new HtmlWebpackPlugin({
    //     filename: "custom-filename.html", // 自定义输出的 HTML 文件名
    //     title: "风机运行管理系统", // 设置页面的标题
    //     // 其他配置...
    //   }),
    //   // 其他插件...
    // ],
  },

  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "sass",
      patterns: [],
    },
  },
  configureWebpack: {
    devtool: "source-map",
  },
};
