import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { fileURLToPath, URL } from 'node:url'
import { viteStaticCopy } from 'vite-plugin-static-copy'

export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    viteStaticCopy({
      targets: [
        {
          src: 'src/assets/images',
          dest: 'assets'
        }
      ]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  build: {
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          if (assetInfo && assetInfo.name) {
            const info = assetInfo.name.split('.')
            const extType = info[info.length - 1]

            if (/\.(png|jpe?g|gif|svg|webp|avif|ico)$/.test(assetInfo.name)) {
              return `assets/images/[name].[ext]`
            }
            if (/\.(woff|woff2|ttf|eot)$/.test(assetInfo.name)) {
              return `assets/fonts/[name].[ext]`
            }
            if (/\.css$/.test(assetInfo.name)) {
              return `assets/css/[name].[ext]`
            }
          }
          return `assets/[name].[ext]`
        },
        chunkFileNames: 'assets/js/[name].js',
        entryFileNames: 'assets/js/[name].js',
      }
    }
  }
})
