import path from 'node:path';
import { fileURLToPath } from 'node:url';

import vue from '@vitejs/plugin-vue';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig({
  build: {
    cssCodeSplit: true,
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      fileName: (format) => {
        switch (format) {
          case 'cjs': {
            return 'index.cjs';
          }
          case 'es': {
            return 'index.mjs';
          }
          case 'umd': {
            return 'index.js';
          }
          default: {
            return `index.${format}.js`;
          }
        }
      },
      formats: ['es', 'cjs', 'umd'],
      name: 'RendererAdapter',
    },
    outDir: 'dist',
    rollupOptions: {
      external: ['vue'],
      output: {
        assetFileNames: (assetInfo) => {
          if (assetInfo.name === 'style.css') return 'assets/[name][extname]';
          return 'assets/[name]-[hash][extname]';
        },
        globals: {
          vue: 'Vue',
        },
      },
    },
    sourcemap: true,
  },
  css: {
    extract: false,
    modules: {
      localsConvention: 'camelCaseOnly',
      scopeBehaviour: 'local',
    },
  },
  plugins: [
    vue({
      css: {
        injectCss: true,
      },
    }),
    dts({
      insertTypesEntry: true,
      outDir: 'dist/types',
      staticImport: true,
    }),
  ],
});
