|
1 | 1 | #!/usr/bin/env node |
2 | 2 | /** |
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 |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | const fs = require('fs'); |
8 | 9 | const path = require('path'); |
9 | 10 |
|
10 | 11 | const distFile = path.join(__dirname, '..', 'dist', 'index.cjs'); |
11 | 12 |
|
12 | | -console.log('Patching dist/index.cjs to remove import.meta.resolve()...'); |
| 13 | +console.log('Patching dist/index.cjs for CommonJS compatibility...'); |
13 | 14 |
|
14 | 15 | let content = fs.readFileSync(distFile, 'utf8'); |
| 16 | +const originalLength = content.length; |
| 17 | +let patchCount = 0; |
15 | 18 |
|
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 |
17 | 20 | // This is safe because the code has a try-catch around it |
18 | | -const before = content.length; |
| 21 | +const patch1Before = content.length; |
19 | 22 | 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 | +} |
21 | 56 |
|
22 | | -if (before !== after) { |
| 57 | +if (content.length !== originalLength) { |
23 | 58 | 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)`); |
25 | 60 | } else { |
26 | | - console.log('✓ No import.meta.resolve() found to patch'); |
| 61 | + console.log('✓ No patches needed'); |
27 | 62 | } |
0 commit comments