Skip to content

Commit 34829a0

Browse files
committed
fix(esm): replace custom fix-esm.js with tsc-alias fileExtensionReplacer
Switches from the bespoke post-processor script to tsc-alias, which handles the full range of ESM path-rewriting edge cases (barrel re-exports, conditional imports, declaration files, dynamic imports). - Replace `node ../../scripts/fix-esm.js dist/esm` with `tsc-alias -p tsconfig.esm.json` in both build:esm scripts - Add tsc-alias ^1.8.10 to devDependencies in both modules - Configure tsc-alias fileExtensionReplacer in both package.json files (rewrites relative imports in compiled output to add .js extension) - Retire scripts/fix-esm.js (still present but no longer invoked)"
1 parent f851e7f commit 34829a0

3 files changed

Lines changed: 26 additions & 56 deletions

File tree

modules/abstract-utxo/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"scripts": {
2626
"build": "npm run build:cjs && npm run build:esm",
2727
"build:cjs": "yarn tsc --build --incremental --verbose .",
28-
"build:esm": "yarn tsc --project tsconfig.esm.json && node ../../scripts/fix-esm.js dist/esm",
28+
"build:esm": "yarn tsc --project tsconfig.esm.json && yarn tsc-alias -p tsconfig.esm.json",
2929
"fmt": "prettier --write '{src,test}/**/*.{ts,js,json}'",
3030
"check-fmt": "prettier --check '{src,test}/**/*.{ts,js,json}'",
3131
"clean": "rm -rf ./dist",
@@ -59,6 +59,14 @@
5959
".ts"
6060
]
6161
},
62+
"tsc-alias": {
63+
"replacers": [
64+
{
65+
"enabled": true,
66+
"file": "tsc-alias/dist/replacers/fileExtensionReplacer.js"
67+
}
68+
]
69+
},
6270
"dependencies": {
6371
"@bitgo/blockapis": "^1.13.4",
6472
"@bitgo/sdk-api": "^1.77.0",
@@ -77,7 +85,8 @@
7785
},
7886
"devDependencies": {
7987
"@bitgo/sdk-test": "^9.1.38",
80-
"mocha": "^10.2.0"
88+
"mocha": "^10.2.0",
89+
"tsc-alias": "^1.8.10"
8190
},
8291
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c"
8392
}

modules/utxo-core/package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"scripts": {
4848
"build": "npm run build:cjs && npm run build:esm",
4949
"build:cjs": "yarn tsc --build --incremental --verbose .",
50-
"build:esm": "yarn tsc --project tsconfig.esm.json && node ../../scripts/fix-esm.js dist/esm",
50+
"build:esm": "yarn tsc --project tsconfig.esm.json && yarn tsc-alias -p tsconfig.esm.json",
5151
"fmt": "prettier --write .",
5252
"check-fmt": "prettier --check '**/*.{ts,js,json}'",
5353
"clean": "rm -r ./dist",
@@ -77,6 +77,14 @@
7777
".ts"
7878
]
7979
},
80+
"tsc-alias": {
81+
"replacers": [
82+
{
83+
"enabled": true,
84+
"file": "tsc-alias/dist/replacers/fileExtensionReplacer.js"
85+
}
86+
]
87+
},
8088
"dependencies": {
8189
"@bitgo/secp256k1": "^1.11.0",
8290
"@bitgo/unspents": "^0.51.3",
@@ -85,5 +93,8 @@
8593
"bip174": "npm:@bitgo-forks/bip174@3.1.0-master.4",
8694
"fast-sha256": "^1.3.0"
8795
},
96+
"devDependencies": {
97+
"tsc-alias": "^1.8.10"
98+
},
8899
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c"
89100
}

scripts/fix-esm.js

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,3 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
4-
function processDir(dir) {
5-
if (!fs.existsSync(dir)) return;
6-
const files = fs.readdirSync(dir);
7-
for (const file of files) {
8-
const fullPath = path.join(dir, file);
9-
if (fs.statSync(fullPath).isDirectory()) {
10-
processDir(fullPath);
11-
} else if (fullPath.endsWith('.js') || fullPath.endsWith('.d.ts')) {
12-
let content = fs.readFileSync(fullPath, 'utf8');
13-
// Regex to match import/export statements (including dynamic imports)
14-
const regex = /(from\s+['"]|import\s*\(?['"]|import\s+['"])([^'"]+)(['"]\)?)/g;
15-
16-
let modifiedContent = content.replace(regex, (match, p1, p2, p3) => {
17-
// Skip if already has an extension or is a known standard module/bare module that doesn't need it
18-
if (p2.endsWith('.js') || p2.endsWith('.json') || p2.endsWith('.cjs') || p2.endsWith('.mjs')) {
19-
return match;
20-
}
21-
22-
// Handle relative paths
23-
if (p2.startsWith('.')) {
24-
// If the path corresponds to a directory, it needs /index.js
25-
const targetPath = path.join(path.dirname(fullPath), p2);
26-
if (fs.existsSync(targetPath) && fs.statSync(targetPath).isDirectory()) {
27-
return `${p1}${p2}/index.js${p3}`;
28-
} else {
29-
return `${p1}${p2}.js${p3}`;
30-
}
31-
}
32-
33-
// Handle specific subpath third-party imports mentioned in the bug report
34-
if (p2.startsWith('bip174/src/')) {
35-
return `${p1}${p2}.js${p3}`;
36-
}
37-
38-
return match;
39-
});
40-
41-
if (modifiedContent !== content) {
42-
fs.writeFileSync(fullPath, modifiedContent, 'utf8');
43-
}
44-
}
45-
}
46-
}
47-
48-
const targetDir = process.argv[2];
49-
if (!targetDir) {
50-
console.error("Usage: node fix-esm.js <directory>");
51-
process.exit(1);
52-
}
53-
processDir(targetDir);
1+
// Retired: ESM import path rewriting is now handled by tsc-alias.
2+
// See modules/*/package.json build:esm scripts and the tsc-alias
3+
// configuration (fileExtensionReplacer) in each module's package.json.

0 commit comments

Comments
 (0)