From e15c4a9627bed71a190037726fc0eb7f7253f83a Mon Sep 17 00:00:00 2001 From: waldronmatt Date: Fri, 31 May 2024 15:17:06 -0400 Subject: [PATCH] fix(vite.config.ts): don't output cjs-specific .d.ts file ext to fix npm types errors --- packages/ui/README.md | 4 ++-- packages/ui/package.json | 2 +- packages/ui/vite.config.ts | 25 +++++-------------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/packages/ui/README.md b/packages/ui/README.md index d1a997fd..6ae71928 100644 --- a/packages/ui/README.md +++ b/packages/ui/README.md @@ -11,8 +11,8 @@ This includes many modern best practices for publishing and consuming in monorep - Outputs: - `esm` and `cjs` source files - source maps for JavaScript files (`.js.map`) - - `esm` and `cjs` declaration files (`.d.ts` and `.d.cts`) - - `esm` and `cjs` source maps for declaration files (`.d.ts.map` and `.c.ts.map`) + - separate `esm` and `cjs` declaration files (`.d.ts`) + - separate `esm` and `cjs` source maps for declaration files (`.d.ts.map`) - compiled CSS modules (consuming app is not required to support css modules) - Subpath exports for explicit path referencing. Will auto map to the right module system - Live monorepo hmr supported: diff --git a/packages/ui/package.json b/packages/ui/package.json index c847e951..9322a6e1 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -17,7 +17,7 @@ "default": "./dist/esm/index.js" }, "require": { - "types": "./dist/types/cjs/index.d.cts", + "types": "./dist/types/cjs/index.d.ts", "default": "./dist/cjs/index.cjs" } }, diff --git a/packages/ui/vite.config.ts b/packages/ui/vite.config.ts index 6609b85f..ebf332f2 100644 --- a/packages/ui/vite.config.ts +++ b/packages/ui/vite.config.ts @@ -1,5 +1,4 @@ import { defineConfig } from 'vite'; -import fs from 'fs-extra'; import react from '@vitejs/plugin-react-swc'; import dts from 'vite-plugin-dts'; import { libInjectCss } from 'vite-plugin-lib-inject-css'; @@ -24,26 +23,12 @@ export default defineConfig({ // outDir: ['dist/types'], // }), dts({ - // create two type folders, one for esm and cjs + // Since TypeScript 5.0, it has emphasized that type files (*.d.ts) are also affected by its ESM and CJS context. + // This means that you can't share a single type file for both ESM and CJS exports of your library. + // You need to have two type files when dual-publishing your library. + // see https://www.typescriptlang.org/docs/handbook/modules/reference.html#node16-nodenext and + // https://publint.dev/rules#export_types_invalid_format outDir: ['dist/types/esm', 'dist/types/cjs'], - // modify type files after they have been written - afterBuild: async () => { - // Fetch all .d.ts files recursively from the dist/types/cjs directory - const files = glob.sync('dist/types/cjs/**/*.d.{ts,ts.map}', { nodir: true }); - // Since TypeScript 5.0, it has emphasized that type files (*.d.ts) are also affected by its ESM and CJS context. - // This means that you can't share a single type file for both ESM and CJS exports of your library. - // You need to have two type files when dual-publishing your library. - // see https://www.typescriptlang.org/docs/handbook/modules/reference.html#node16-nodenext and - // https://publint.dev/rules#export_types_invalid_format - await Promise.all( - files.map(async (file) => { - // Generate the new files with the new .c.ts/.c.ts.map naming - const newFilePath = file.replace(/\.d\.ts(\.map)?$/, '.d.cts$1'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access - await fs.move(file, newFilePath, { overwrite: true }); - }), - ); - }, }), // generates a separate CSS file for each chunk and includes an import statement // at the beginning of each chunk's output file