From 2350aa140eb692610cc8ba1788ac0300188a5144 Mon Sep 17 00:00:00 2001 From: Pawel Zelawski Date: Wed, 9 Apr 2025 20:33:24 +0200 Subject: build: Configure Vite for library build and finalize package fields - Add `vite-plugin-dts` dev dependency. - Create `vite.config.ts` specifically for library build mode: - Configure ESM and UMD outputs (`dist/digiid-ts.es.js`, `dist/digiid-ts.umd.js`). - Set up `vite-plugin-dts` for generating `dist/index.d.ts`. - Externalize Node.js built-ins and 'digibyte-message' dependency. - Update `package.json`: - Change `scripts.build` to `vite build`. - Point `main`, `module`, and `types` fields to the correct files in `dist/`. - Run and verify the build process successfully generates the expected distribution files. --- vite.config.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 vite.config.ts (limited to 'vite.config.ts') diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..a69cc50 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,37 @@ +import { defineConfig } from 'vite'; +import dts from 'vite-plugin-dts'; // Plugin to generate consolidated .d.ts file +import { resolve } from 'path'; + +// 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 + }), + ], + 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', 'module', 'digibyte-message'], // Externalize Node built-ins and the core dependency + output: { + // Provide global variables to use in the UMD build + // for externalized deps + globals: { + crypto: 'crypto', // Map 'crypto' import to global 'crypto' (Node) + module: 'module', // Map 'module' import to global 'module' (Node) + 'digibyte-message': 'DigibyteMessage' // Example global name if needed, might not be necessary for UMD if only used internally + }, + }, + }, + sourcemap: true, // Generate source maps for debugging + emptyOutDir: true, // Clean the dist directory before building + }, +}); \ No newline at end of file -- cgit v1.2.3