import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import copyPlugin from 'rollup-plugin-copy';
import path from 'path';
import autoprefixer from 'autoprefixer';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');

  return {
    base: './',
    build: {
      sourcemap: true,
      emptyOutDir: false,
      rollupOptions: {
        plugins: [
          copyPlugin({
            targets: [{ src: 'src/assets/**/*', dest: 'dist/assets' }],
          }),
        ],
      },
      outDir: 'dist',
      assetsDir: 'assets',
    },
    plugins: [vue(), AutoImport({}), Components({})],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@assets': path.resolve(__dirname, './src/assets'),
        '@style': path.resolve(__dirname, './src/styles'),
        '@stores': path.resolve(__dirname, './src/stores'),
        '@components': path.resolve(__dirname, './src/components'),
        '@utils': path.resolve(__dirname, './src/utils'),
        '@api': path.resolve(__dirname, './src/api'),
        '@types': path.resolve(__dirname, './types'),
        '@views': path.resolve(__dirname, './src/views'),
        '@composables': path.resolve(__dirname, './src/utils/composables'),
        '@router': path.resolve(__dirname, './src/router'),
        '@directive': path.resolve(__dirname, './src/directive'),
        '@layout': path.resolve(__dirname, './src/layout'),
        '@interface': path.resolve(__dirname, './interface'),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
      postcss: {
        plugins: [autoprefixer()],
      },
    },
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      proxy: {
        '/demo': {
          target: env.VITE_DEMO_PROXY_TARGET,
          changeOrigin: true,
        },
      },
    },
  };
});
