var path = require("path");
const { defineConfig } = require("@vue/cli-service");
const webpack = require("webpack");
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const IS_PROD = ["production", "prod"].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir); // 路径
module.exports = defineConfig({
  transpileDependencies: true,
  pages: {
    index: {
      // page 的入口
      entry: "examples/main.ts",
      // 模板来源
      template: "public/index.html",
      // 输出文件名
      filename: "index.html",
    },
  },
  publicPath: "./",
  chainWebpack: (config) => {
    config.plugin("polyfills").use(NodePolyfillPlugin);
    config.resolve.alias.set("vue-i18n", "vue-i18n/dist/vue-i18n.cjs.js");
    config.resolve.alias.set("@", resolve("packages"));
    // 使用 common 包以便打补丁将内部 i18n 挂到 globalThis，与 mergeMessagesIntoLtABaseUi 配合
    config.plugin('define').tap((definitions) => {
      Object.assign(definitions[0], {
        __VUE_OPTIONS_API__: 'true',
        __VUE_PROD_DEVTOOLS__: 'false',
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false'
      })
      return definitions
    })
    config.module
      //增加ICO验证规则，查看实际输出规则指令 vue inspect > output.js
      .rule("image")
      .test(/\.(ico)(\?.*)?$/)
      .use("url-loader")
      .loader("url-loader")
      .tap((options) => {
        // 修改它的选项...
        return options;
      });
    if (IS_PROD) {
      config.module
        .rule("font")
        .test(/\.(woff2?|eot|ttf|otf|svg)(\?.*)?$/)
        .exclude.add(/node_modules/) //
        .end()
        .type("asset/resource")
        .use("file-loader")
        .loader("file-loader")
        .options({
          name: "fonts/[name].[ext]",
        })
        .tap((options) => {
          // 修改它的选项...
          return options;
        });
    }
  },
  configureWebpack: (config) => {
    config.plugins.push(
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "windows.jQuery": "jquery",
      })
    );
    config.plugins.push(
      new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 1,
        // minChunkSize: 1000,
      })
    );
    config.plugins.push(
      new webpack.optimize.MinChunkSizePlugin({
        minChunkSize: 10000, // Minimum number of characters
      })
    );
    if (process.env.NODE_ENV === "production") {
      config.mode = "production";
      // 打包文件大小配置
      config.performance = {
        maxEntrypointSize: 10000000,
        maxAssetSize: 30000000,
      };
    }
  },
  devServer: {
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
  },
});
