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

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

export default defineConfig(() => ({
	plugins: [
		tsconfigPaths({ root: indexDir, }),
		ReactivityTransform(),
		vue(),
		dts({
			entryRoot: indexDir,
		}),
	],
	resolve: {
		alias: {
			'@bagelink/vue': indexDir,
		},
	},
	build: {
		minify: false,
		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
	},
}))
