Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"start": "firebase emulators:exec --project=rxfire-525a3 \"vitest --ui\"",
"build": "tsc && vite build && npm run build:types-cjs && npm run docs",
"build:types-cjs": "node -e \"require('fs').copyFileSync('dist/index.d.ts','dist/index.d.cts')\"",
"build:types-cjs": "node scripts/emit-cjs-types.mjs",
"test": "firebase emulators:exec --project=rxfire-525a3 \"vitest\"",
"test:firestore": "firebase emulators:exec --only firestore --ui --project=rxfire-525a3 \"vitest firestore\"",
"test:database": "firebase emulators:exec --only database --project=rxfire-525a3 \"vitest database\"",
Expand Down
20 changes: 20 additions & 0 deletions scripts/emit-cjs-types.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Writes a CommonJS declaration file (`.d.cts`) next to every `dist/*.d.ts`, for the `require` export condition.
// A copy of `index.d.ts` is not enough: its relative imports resolve to `.d.ts` files, which TypeScript reads
// as ESM in this `"type": "module"` package, so CommonJS consumers get TS1479 on each import (#850).
// Rewriting `./x.js` to `./x.cjs` points each import at the matching `.d.cts` instead.
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

const dir = 'dist';
const relativeSpecifier = /(\bfrom\s*|\bimport\s*\(\s*)(['"])(\.{1,2}\/[^'"]*)\2/g;

for (const file of readdirSync(dir).filter((f) => f.endsWith('.d.ts'))) {
const source = readFileSync(join(dir, file), 'utf8');
const cjs = source.replace(relativeSpecifier, (_, keyword, quote, specifier) => {
if (!specifier.endsWith('.js')) {
throw new Error(`${file}: relative import without a .js extension: ${specifier}`);
}
return `${keyword}${quote}${specifier.slice(0, -'.js'.length)}.cjs${quote}`;
});
writeFileSync(join(dir, file.replace(/\.d\.ts$/, '.d.cts')), cjs);
}
Loading