/** @format */

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { resolve } from 'path';
import dayjs from 'dayjs';
import pkg from './package.json';
import vueJsx from '@vitejs/plugin-vue-jsx';
import path from 'path';
import legacy from '@vitejs/plugin-legacy';
const { dependencies, devDependencies, name, version } = pkg;
const __APP_INFO__ = {
  platform: process.env.PLATFORM,
  pkg: { dependencies, devDependencies, name, version },
  lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  serviceMode: '',
};
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir) + '/';
}
const serviceMode = 'paas';
// @ts-ignore
export default defineConfig(({ mode, command }) => {
  const root = process.cwd();
  const env = loadEnv(mode, root);
  const { VITE_PORT, VITE_PROXY = '{}', VITE_LEGACY, VITE_PUBLIC_PATH } = env;
  const port = Number(VITE_PORT);
  const proxy = JSON.parse(VITE_PROXY);
  const isBuild = command === 'build';
  __APP_INFO__.serviceMode = serviceMode;
  const server = {
    host: true,
    port,
    proxy,
    open: false,
  };
  return {
    base: VITE_PUBLIC_PATH,
    build: {
      lib: {
        entry: path.resolve(__dirname, './lib/paas/index.ts'),
        name: 'nerv-lib',
        fileName: (format) => `nerv-lib.${format}.js`,
        formats: ['es'],
      },
      rollupOptions: {
        external: ['vue', 'vue-router', /^ant-design-vue(\/.+|$)/, 'vue-demi', 'pinia', 'dayjs'],
        output: {
          globals: {
            vue: 'Vue',
            'vue-router': 'vueRouter',
            'vue-demi': 'VueDemi',
            pinia: 'Pinia',
            dayjs: 'dayjs',
            'ant-design-vue': 'antd',
          },
          // inlineDynamicImports: true, //使用了按需加载
        },
      },
    },
    resolve: {
      alias: [
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        {
          find: /\/@\//,
          replacement: pathResolve('src'),
        },
        {
          find: /\/nerv-lib\//,
          replacement: pathResolve('lib'),
        },
        {
          find: /\/nerv-base\//,
          replacement: pathResolve(`lib/${serviceMode}`),
        },
      ],
    },
    server,
    define: {
      __APP_INFO__: JSON.stringify(__APP_INFO__),
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import "/lib/${serviceMode}/theme/variable.less";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    plugins: [
      // commonjs(),
      // nodePolyfills(),
      legacy({
        targets: ['defaults', 'not IE 11'],
      }),
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [pathResolve('src/icon')],
        svgoOptions: isBuild,
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
  };
});
