Skip to content

Commit ca74379

Browse files
Copilotfelickz
andcommitted
fix: patch import.meta.resolve in bundled code for CommonJS compatibility
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 34ecb2f commit ca74379

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

dist/index.cjs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format": "prettier --write '**/*.ts'",
1010
"format-check": "prettier --check '**/*.ts'",
1111
"lint": "eslint src/**/*.ts",
12-
"package": "ncc build --source-map --license licenses.txt --target es2015 -o dist && mv dist/index.js dist/index.cjs && mv dist/index.js.map dist/index.cjs.map",
12+
"package": "ncc build --source-map --license licenses.txt --target es2015 -o dist && mv dist/index.js dist/index.cjs && mv dist/index.js.map dist/index.cjs.map && node scripts/patch-dist.js",
1313
"test": "jest",
1414
"release": "release-it",
1515
"all": "npm run build && npm run format && npm run lint && npm run package"

scripts/patch-dist.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
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
5+
*/
6+
7+
const fs = require('fs');
8+
const path = require('path');
9+
10+
const distFile = path.join(__dirname, '..', 'dist', 'index.cjs');
11+
12+
console.log('Patching dist/index.cjs to remove import.meta.resolve()...');
13+
14+
let content = fs.readFileSync(distFile, 'utf8');
15+
16+
// Replace import.meta.resolve() with null (which will be caught by the try-catch)
17+
// This is safe because the code has a try-catch around it
18+
const before = content.length;
19+
content = content.replace(/import\.meta\.resolve\(/g, '(function(){throw new Error("import.meta not available in CommonJS")})(');
20+
const after = content.length;
21+
22+
if (before !== after) {
23+
fs.writeFileSync(distFile, content, 'utf8');
24+
console.log(`✓ Patched dist/index.cjs (replaced ${(before - after) / -1} characters)`);
25+
} else {
26+
console.log('✓ No import.meta.resolve() found to patch');
27+
}

0 commit comments

Comments
 (0)