Skip to content

Commit de941fe

Browse files
Copilotfelickz
andcommitted
fix: patch circular require references in bundled code
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent ca74379 commit de941fe

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

dist/index.cjs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/patch-dist.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
11
#!/usr/bin/env node
22
/**
3-
* Patch the bundled dist/index.cjs to remove ES module syntax that causes issues in CommonJS
4-
* Specifically, replace import.meta.resolve() with a fallback that works in CommonJS
3+
* Patch the bundled dist/index.cjs to fix CommonJS compatibility issues
4+
* 1. Remove import.meta.resolve() ES module syntax
5+
* 2. Fix circular *_require reference patterns created by ncc bundler
56
*/
67

78
const fs = require('fs');
89
const path = require('path');
910

1011
const distFile = path.join(__dirname, '..', 'dist', 'index.cjs');
1112

12-
console.log('Patching dist/index.cjs to remove import.meta.resolve()...');
13+
console.log('Patching dist/index.cjs for CommonJS compatibility...');
1314

1415
let content = fs.readFileSync(distFile, 'utf8');
16+
const originalLength = content.length;
17+
let patchCount = 0;
1518

16-
// Replace import.meta.resolve() with null (which will be caught by the try-catch)
19+
// Patch 1: Replace import.meta.resolve() with error-throwing function
1720
// This is safe because the code has a try-catch around it
18-
const before = content.length;
21+
const patch1Before = content.length;
1922
content = content.replace(/import\.meta\.resolve\(/g, '(function(){throw new Error("import.meta not available in CommonJS")})(');
20-
const after = content.length;
23+
if (content.length !== patch1Before) {
24+
patchCount++;
25+
console.log(` ✓ Patched import.meta.resolve() (${(patch1Before - content.length) / -1} chars added)`);
26+
}
27+
28+
// Patch 2: Fix circular *_require reference patterns
29+
// The ncc bundler sometimes creates patterns like: const esm_require = createRequire(esm_require("url")...)
30+
// We need to replace the inner self-reference with plain require
31+
const circularPatterns = [
32+
/lib_require\("url"\)/g,
33+
/esm_require\("url"\)/g,
34+
/lib_require\("path"\)/g,
35+
/esm_require\("path"\)/g,
36+
/lib_require\("fs"\)/g,
37+
/esm_require\("fs"\)/g,
38+
];
39+
40+
let circularPatchCount = 0;
41+
circularPatterns.forEach(pattern => {
42+
const beforePatch = content;
43+
content = content.replace(pattern, (match) => {
44+
const moduleName = match.match(/"([^"]+)"/)[1];
45+
return `require("${moduleName}")`;
46+
});
47+
if (content !== beforePatch) {
48+
circularPatchCount++;
49+
}
50+
});
51+
52+
if (circularPatchCount > 0) {
53+
patchCount++;
54+
console.log(` ✓ Fixed ${circularPatchCount} circular *_require reference(s)`);
55+
}
2156

22-
if (before !== after) {
57+
if (content.length !== originalLength) {
2358
fs.writeFileSync(distFile, content, 'utf8');
24-
console.log(`✓ Patched dist/index.cjs (replaced ${(before - after) / -1} characters)`);
59+
console.log(`✓ Successfully patched dist/index.cjs (${patchCount} patch types applied, ${originalLength - content.length} bytes changed)`);
2560
} else {
26-
console.log('✓ No import.meta.resolve() found to patch');
61+
console.log('✓ No patches needed');
2762
}

0 commit comments

Comments
 (0)