const { defineConfig } = require("@vue/cli-service");
const dayjs = require("dayjs");
const { name, version, chineseName } = require("./package.json");

const buildTag = `${dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss")}`;
const injectScript = `<script>const aVersionInfo = { name: '${name}', chineseName: '${chineseName}', version: '${version}', buildDate:'${buildTag}' }; if (!window.versionInfo) {window.versionInfo = {};} window.versionInfo['${name}'] = aVersionInfo;</script>`;
const rootId = name + "__id";

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: "./",
  devServer: {
    host: "0.0.0.0",
    https: {
      key: "./cert/localhost+1-key.pem",
      cert: "./cert/localhost+1.pem",
    },
    hot: true,
    port: 8332,
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
  },
  configureWebpack: {
    output: {
      library: name,
      libraryTarget: "umd", // 把微应用打包成 umd 库格式
      chunkLoadingGlobal: `webpackJsonp_${name}`, // webpack4 => jsonpFunction
    },
    plugins: [],
  },
  chainWebpack: (config) => {
    config.plugin("html").tap((args) => {
      if (args && args.length > 0) {
        args[0].rootId = rootId;
        args[0].customInjectScript = injectScript;
      }

      return args;
    });
  },
});
