Skip to content

Instantly share code, notes, and snippets.

@Kequc
Created October 4, 2025 21:50
Show Gist options
  • Select an option

  • Save Kequc/5fb1ed3fc7b2e4e9b2862eb4f60a4eda to your computer and use it in GitHub Desktop.

Select an option

Save Kequc/5fb1ed3fc7b2e4e9b2862eb4f60a4eda to your computer and use it in GitHub Desktop.
Fix build output of tsc .d.ts files when written with valid ecmascript.
import { readdir, readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
async function walk(dir: string) {
for (const e of await readdir(dir, { withFileTypes: true })) {
const p = join(dir, e.name);
if (e.isDirectory()) {
await walk(p);
} else if (e.isFile() && p.endsWith('.d.ts')) {
const src = await readFile(p, 'utf8');
// Replace only *relative* .ts specifiers with .js in import/export/dynamic-import
const out = src
.replace(/(from\s+['"])(\.{1,2}\/[^'"]+)\.ts(['"])/g, '$1$2.js$3')
.replace(/(import\(\s*['"])(\.{1,2}\/[^'"]+)\.ts(['"]\s*\))/g, '$1$2.js$3')
.replace(/(export\s+\*\s+from\s+['"])(\.{1,2}\/[^'"]+)\.ts(['"])/g, '$1$2.js$3');
if (out !== src) await writeFile(p, out, 'utf8');
}
}
}
await walk('dist');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment