Skip to content

Commit

Permalink
fix(vite.config.ts): don't output cjs-specific .d.ts file ext to fix …
Browse files Browse the repository at this point in the history
…npm types errors
  • Loading branch information
waldronmatt committed May 31, 2024
1 parent 4f54821 commit e15c4a9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand Down
25 changes: 5 additions & 20 deletions packages/ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down

0 comments on commit e15c4a9

Please sign in to comment.