diff --git a/modules/abstract-utxo/package.json b/modules/abstract-utxo/package.json index 503ba1e9be..69d7322ea0 100644 --- a/modules/abstract-utxo/package.json +++ b/modules/abstract-utxo/package.json @@ -25,7 +25,7 @@ "scripts": { "build": "npm run build:cjs && npm run build:esm", "build:cjs": "yarn tsc --build --incremental --verbose .", - "build:esm": "yarn tsc --project tsconfig.esm.json", + "build:esm": "yarn tsc --project tsconfig.esm.json && node ../../scripts/fix-esm.js dist/esm", "fmt": "prettier --write '{src,test}/**/*.{ts,js,json}'", "check-fmt": "prettier --check '{src,test}/**/*.{ts,js,json}'", "clean": "rm -rf ./dist", diff --git a/modules/utxo-core/package.json b/modules/utxo-core/package.json index c766d91698..18229cb083 100644 --- a/modules/utxo-core/package.json +++ b/modules/utxo-core/package.json @@ -47,7 +47,7 @@ "scripts": { "build": "npm run build:cjs && npm run build:esm", "build:cjs": "yarn tsc --build --incremental --verbose .", - "build:esm": "yarn tsc --project tsconfig.esm.json", + "build:esm": "yarn tsc --project tsconfig.esm.json && node ../../scripts/fix-esm.js dist/esm", "fmt": "prettier --write .", "check-fmt": "prettier --check '**/*.{ts,js,json}'", "clean": "rm -r ./dist", diff --git a/scripts/fix-esm.js b/scripts/fix-esm.js new file mode 100644 index 0000000000..f638262110 --- /dev/null +++ b/scripts/fix-esm.js @@ -0,0 +1,53 @@ +const fs = require('fs'); +const path = require('path'); + +function processDir(dir) { + if (!fs.existsSync(dir)) return; + const files = fs.readdirSync(dir); + for (const file of files) { + const fullPath = path.join(dir, file); + if (fs.statSync(fullPath).isDirectory()) { + processDir(fullPath); + } else if (fullPath.endsWith('.js') || fullPath.endsWith('.d.ts')) { + let content = fs.readFileSync(fullPath, 'utf8'); + // Regex to match import/export statements (including dynamic imports) + const regex = /(from\s+['"]|import\s*\(?['"]|import\s+['"])([^'"]+)(['"]\)?)/g; + + let modifiedContent = content.replace(regex, (match, p1, p2, p3) => { + // Skip if already has an extension or is a known standard module/bare module that doesn't need it + if (p2.endsWith('.js') || p2.endsWith('.json') || p2.endsWith('.cjs') || p2.endsWith('.mjs')) { + return match; + } + + // Handle relative paths + if (p2.startsWith('.')) { + // If the path corresponds to a directory, it needs /index.js + const targetPath = path.join(path.dirname(fullPath), p2); + if (fs.existsSync(targetPath) && fs.statSync(targetPath).isDirectory()) { + return `${p1}${p2}/index.js${p3}`; + } else { + return `${p1}${p2}.js${p3}`; + } + } + + // Handle specific subpath third-party imports mentioned in the bug report + if (p2.startsWith('bip174/src/')) { + return `${p1}${p2}.js${p3}`; + } + + return match; + }); + + if (modifiedContent !== content) { + fs.writeFileSync(fullPath, modifiedContent, 'utf8'); + } + } + } +} + +const targetDir = process.argv[2]; +if (!targetDir) { + console.error("Usage: node fix-esm.js "); + process.exit(1); +} +processDir(targetDir);