import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'; import inject from '@rollup/plugin-inject'; import { extname, relative, resolve } from 'path'; import { fileURLToPath } from 'url'; import { glob } from 'glob'; import dts from 'vite-plugin-dts'; export default defineConfig({ plugins: [ react(), dts({ insertTypesEntry: true, }) ], optimizeDeps: { esbuildOptions: { define: { global: 'globalThis', }, plugins: [ // Enable esbuild polyfill plugins NodeGlobalsPolyfillPlugin({ process: false, buffer: true }), ], }, }, build: { rollupOptions: { plugins: [inject({ Buffer: ['buffer', 'Buffer'] })], external: ['react', 'react/jsx-runtime'], input: Object.fromEntries( // https://rollupjs.org/configuration-options/#input glob.sync('src/**/*.{ts,tsx}', { ignore: ["src/**/*.d.ts"], }).map(file => [ // 1. The name of the entry point // lib/nested/foo.js becomes nested/foo relative( 'src', file.slice(0, file.length - extname(file).length) ), // 2. The absolute path to the entry file // lib/nested/foo.ts becomes /project/lib/nested/foo.ts fileURLToPath(new URL(file, import.meta.url)) ]) ), jsx: 'react', output: { } }, lib: { entry: resolve(__dirname, 'src/index.ts'), formats: ['es', 'cjs'], }, outDir: resolve(__dirname, 'dist'), }, });