import { resolve } from 'node:path'
// import { fileURLToPath, URL } from 'node:url'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import tsconfigPaths from 'vite-tsconfig-paths'

const indexDir = resolve(import.meta.dirname, 'src')

// @ts-ignore - rolldown-vite in root workspace causes plugin type mismatch; build is unaffected
export default defineConfig(() => ({
	plugins: [
		tsconfigPaths({ root: indexDir, }),
		vue(),
		dts({
			entryRoot: indexDir,
			copyDtsFiles: true,
		}),
	],
	resolve: {
		alias: {
			'@bagelink/vue': indexDir,
		},
	},
	// experimental: { enableNativePlugin: true },
	build: {
		minify: 'esbuild', // Arrow functions should preserve scope even with minification
		lib: {
			entry: resolve(indexDir, 'index.ts'),
			formats: ['es', 'cjs'],
			fileName: (module, entry) => `${entry}.${module === 'es' ? 'mjs' : module}`,
			cssFileName: 'style',
		},
		rollupOptions: {
			// external modules won't be bundled into your library
			external: ['vue', 'vue-router', 'fsevents'], // not every external has a global
			output: {
				// disable warning on src/index.ts using both default and named export
				exports: 'named',
				// Provide global variables to use in the UMD build
				// for externalized deps (not useful if 'umd' is not in lib.formats)
				globals: {
					vue: 'Vue',
				},
			},
		},
		emptyOutDir: false, // to retain the types folder generated by tsc
	},
}))
