import path from 'path'
import { uniuseAutoImports } from '@uni-helper/uni-use'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default async () => {
  const uni = (await import('@dcloudio/vite-plugin-uni')).default.default
  const UnoCSS = (await import('unocss/vite')).default
  const AutoImport = (await import('unplugin-auto-import/vite')).default
  const srcDir = path.resolve(__dirname, 'src')

  return defineConfig({
    build: {
      target: 'es6',
      cssTarget: 'chrome61'
    },
    optimizeDeps: {
      exclude: ['vue-demi']
    },
    plugins: [
      uni(),
      UnoCSS(),
      AutoImport({
        imports: ['vue', 'pinia', 'uni-app', uniuseAutoImports()],
        dts: 'src/auto-import.d.ts',
        eslintrc: {
          enabled: true
        }
      })
    ],
    resolve: {
      alias: {
        '@': srcDir,
        api: `${srcDir}/api`,
        pages: `${srcDir}/pages`,
        components: `${srcDir}/pages/components`,
        utils: `${srcDir}/utils`,
        helper: `${srcDir}/helper`,
        store: `${srcDir}/store`,
        constants: `${srcDir}/constants`,
        hooks: `${srcDir}/composables`
      }
    }
  })
}
