Skip to content

Commit 8345303

Browse files
committed
Fix relative import paths in compiled output
Adjust CommonJS require paths from ../ to ./ in dist files. After compilation, both main files and subdirectories are in dist/, so cross-directory references need to use ./ instead of ../.
1 parent f53d496 commit 8345303

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

scripts/fix-commonjs-exports.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async function processDirectory(dir) {
3333
fixedCount += await processDirectory(fullPath)
3434
} else if (entry.isFile() && entry.name.endsWith('.js')) {
3535
let content = await fs.readFile(fullPath, 'utf8')
36+
let modified = false
3637

3738
// Check if this is a single default export.
3839
if (content.includes('exports.default =')) {
@@ -44,7 +45,22 @@ async function processDirectory(dir) {
4445
/Object\.defineProperty\(exports, "__esModule", \{ value: true \}\);\n?/g,
4546
'',
4647
)
48+
modified = true
49+
}
50+
51+
// Fix relative paths from ../ to ./ in dist files.
52+
// After compilation, both main files and subdirectories are in dist/,
53+
// so references should use ./ instead of ../.
54+
if (content.includes('require("../')) {
55+
content = content.replace(/require\("\.\.\//g, 'require("./')
56+
modified = true
57+
}
58+
if (content.includes("require('../")) {
59+
content = content.replace(/require\('\.\.\//g, "require('./")
60+
modified = true
61+
}
4762

63+
if (modified) {
4864
await fs.writeFile(fullPath, content)
4965
const relativePath = path.relative(distDir, fullPath)
5066
console.log(` Fixed ${relativePath}`)

0 commit comments

Comments
 (0)