Skip to content

Commit f1dffbd

Browse files
committed
feat: add post-build script to resolve relative paths in generated declaration files
1 parent 8f40e4b commit f1dffbd

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"scripts": {
3333
"dev": "bun --watch run src/index.ts",
34-
"build": "tsc -p tsconfig.build.json && node esbuild.config.js",
34+
"build": "tsc -p tsconfig.build.json && node esbuild.config.js && node scripts/fix-dts-paths.js",
3535
"lint": "eslint .",
3636
"format": "prettier --write .",
3737
"release": "bun run build && bunx bumpp --push"

scripts/fix-dts-paths.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { readdirSync, readFileSync, writeFileSync, existsSync } from 'fs';
2+
import { resolve, dirname, relative } from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = dirname(__filename);
7+
const rootDir = resolve(__dirname, '..');
8+
9+
function walkDir(dir, callback) {
10+
readdirSync(dir, { withFileTypes: true }).forEach((entry) => {
11+
const path = resolve(dir, entry.name);
12+
if (entry.isDirectory()) {
13+
walkDir(path, callback);
14+
} else if (entry.name.endsWith('.d.ts')) {
15+
callback(path);
16+
}
17+
});
18+
}
19+
20+
function fixPaths() {
21+
const distDir = resolve(rootDir, 'dist');
22+
23+
walkDir(distDir, (filePath) => {
24+
let content = readFileSync(filePath, 'utf-8');
25+
let modified = content.replace(/from '@\/([^']+)'/g, (match, path) => {
26+
const cleanPath = path.replace(/^@/, '');
27+
let targetPath = resolve(distDir, cleanPath + '.d.ts');
28+
if (!existsSync(targetPath)) {
29+
targetPath = resolve(distDir, cleanPath);
30+
}
31+
const relPath = './' + relative(dirname(filePath), targetPath).replace('.d.ts', '');
32+
return `from '${relPath}'`;
33+
});
34+
if (modified !== content) {
35+
writeFileSync(filePath, modified);
36+
console.log('Fixed:', filePath);
37+
}
38+
});
39+
}
40+
41+
fixPaths();

0 commit comments

Comments
 (0)