summaryrefslogtreecommitdiff
path: root/vite.config.ts
blob: 2fd4f508ef1b83eebbaa9d5e163e6d298b9f67a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import path, { resolve } from 'path'; // Import path module
import { fileURLToPath } from 'url'; // Import for ESM __dirname alternative
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts'; // Plugin to generate consolidated .d.ts file

// Get current directory path in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// https://vitejs.dev/guide/build.html#library-mode
export default defineConfig({
  plugins: [
    dts({ // Generate declaration files
      insertTypesEntry: true, // Create a single entry point for types
      exclude: ['src/__tests__/**'], // Keep test declarations out of the published package
    }),
  ],
  build: {
    lib: {
      // Could also be a dictionary or array of multiple entry points
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'DigiIDTs', // Global variable name in UMD build
      formats: ['es', 'umd'], // Output formats (ES Module, Universal Module Definition)
      fileName: (format) => `digiid-ts.${format}.js`, // Output file names
    },
    rollupOptions: {
      // Make sure to externalize deps that shouldn't be bundled
      // into your library (e.g., peer dependencies)
      external: ['crypto'], // Externalize Node built-ins
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          crypto: 'crypto', // Map 'crypto' import to global 'crypto' (Node)
        },
      },
    },
    sourcemap: true, // Generate source maps for debugging
    emptyOutDir: true, // Clean the dist directory before building
  },
});