Skip to content

fix CommonJS require broken by import.meta.url in CJS output (#157)#166

Merged
9cb14c1ec0 merged 1 commit intomasterfrom
commonjs
Mar 6, 2026
Merged

fix CommonJS require broken by import.meta.url in CJS output (#157)#166
9cb14c1ec0 merged 1 commit intomasterfrom
commonjs

Conversation

@9cb14c1ec0
Copy link
Copy Markdown
Collaborator

@9cb14c1ec0 9cb14c1ec0 commented Feb 23, 2026

  • Wrap import.meta.url in eval() so it's hidden from the CJS parser, preventing SyntaxError when loading dist/cjs files via require()
  • Add dist/cjs/package.json {"type":"commonjs"} and dist/esm/package.json {"type":"module"} to explicitly declare module types regardless of parent package.json settings
  • Convert scripts/create-wrappers.js from ESM to CJS syntax

Closes #157

Fixes #157 .

Changes proposed in this pull request

To test (it takes a while): npm install github:<username>/venom#<branch>

Summary by CodeRabbit

  • Chores
    • Restructured module distribution with separate CommonJS and ES Module directories for enhanced compatibility.
    • Improved cross-platform path handling to support dual-build environments across different JavaScript runtimes.

- Wrap import.meta.url in eval() so it's hidden from the CJS parser,
  preventing SyntaxError when loading dist/cjs files via require()
- Add dist/cjs/package.json {"type":"commonjs"} and
  dist/esm/package.json {"type":"module"} to explicitly declare module
  types regardless of parent package.json settings
- Convert scripts/create-wrappers.js from ESM to CJS syntax

Closes #157

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 23, 2026

📝 Walkthrough

Walkthrough

This PR implements dual-build support (CommonJS and ESM) for the venom-bot package to address cross-environment compatibility. Changes include updating the wrapper creation script to generate separate cjs and esm output directories with package.json module-type markers, and modifying __dirname resolution in source files to safely access import.meta.url via eval() for CJS parser compatibility.

Changes

Cohort / File(s) Summary
Build Configuration & Wrapper Generation
scripts/create-wrappers.js
Migrates from ES module to CommonJS module loading. Adds recursive creation of cjs and esm subdirectories with package.json marker files (type: "commonjs" and type: "module") to enforce correct Node module resolution during dual-build output.
Cross-Platform Dirname Resolution
src/api/whatsapp.ts, src/utils/qr-generator.ts
Updates __dirname fallback logic to use eval-wrapped import.meta.url (eval('import.meta.url')) instead of direct access, preventing SyntaxError in CJS parser while maintaining ES module functionality. Retains simplified path imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Two paths converge in the forest of code,
CJS and ESM walk the same road,
With eval() as guide and markers so bright,
The dual-build bundle shines with pure light! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly summarizes the main change: wrapping import.meta.url in eval() to fix CommonJS require compatibility in CJS output, directly addressing issue #157.
Linked Issues check ✅ Passed All code changes directly address the requirements from issue #157: wrapping import.meta.url in eval() to prevent SyntaxError in CJS, adding package.json markers (type: commonjs/module), and converting scripts to CommonJS syntax.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing CommonJS/ESM compatibility as specified in issue #157; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch commonjs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/api/whatsapp.ts`:
- Around line 11-21: Suppress the Biome linter for the intentional eval in
getDirname by adding a Biome ignore comment immediately above the eval usage;
specifically, place a biome noGlobalEval suppression (e.g., /* biome-ignore
noGlobalEval */) right before the line that calls eval('import.meta.url') inside
getDirname so the fallback using fileURLToPath(import.meta.url) is allowed
without CI lint failure.

In `@src/utils/qr-generator.ts`:
- Around line 9-16: The intentional use of eval in getDirname triggers Biome's
no-eval rule; add an inline Biome ignore comment immediately before the eval
expression in src/utils/qr-generator.ts (inside the getDirname function) to
silence the linter for this intentional, constant-string usage — e.g., annotate
the eval('import.meta.url') call with the appropriate Biome ignore comment so
linting in CI won't fail while leaving the logic in getDirname unchanged.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 71de406 and d4f1c26.

⛔ Files ignored due to path filters (10)
  • dist/cjs/api/whatsapp.js is excluded by !**/dist/**
  • dist/cjs/api/whatsapp.js.map is excluded by !**/dist/**, !**/*.map
  • dist/cjs/package.json is excluded by !**/dist/**
  • dist/cjs/utils/qr-generator.js is excluded by !**/dist/**
  • dist/cjs/utils/qr-generator.js.map is excluded by !**/dist/**, !**/*.map
  • dist/esm/api/whatsapp.js is excluded by !**/dist/**
  • dist/esm/api/whatsapp.js.map is excluded by !**/dist/**, !**/*.map
  • dist/esm/package.json is excluded by !**/dist/**
  • dist/esm/utils/qr-generator.js is excluded by !**/dist/**
  • dist/esm/utils/qr-generator.js.map is excluded by !**/dist/**, !**/*.map
📒 Files selected for processing (3)
  • scripts/create-wrappers.js
  • src/api/whatsapp.ts
  • src/utils/qr-generator.ts

Comment on lines +11 to 21
import { dirname } from 'path';

// Cross-platform __dirname
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
try {
if (typeof __dirname !== 'undefined') {
return __dirname;
} catch (e) {
// @ts-ignore
return dirname(fileURLToPath(import.meta.url));
}
// @ts-ignore
return dirname(fileURLToPath(eval('import.meta.url')));
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Suppress the Biome noGlobalEval error for this fallback.

This eval is intentional and constant-string, but Biome treats it as an error. Add an ignore to avoid CI lint failures.

✅ Suggested fix
   // `@ts-ignore`
+  // biome-ignore lint/security/noGlobalEval -- needed to hide import.meta from CJS parser
   return dirname(fileURLToPath(eval('import.meta.url')));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { dirname } from 'path';
// Cross-platform __dirname
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
try {
if (typeof __dirname !== 'undefined') {
return __dirname;
} catch (e) {
// @ts-ignore
return dirname(fileURLToPath(import.meta.url));
}
// @ts-ignore
return dirname(fileURLToPath(eval('import.meta.url')));
};
import { dirname } from 'path';
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
if (typeof __dirname !== 'undefined') {
return __dirname;
}
// `@ts-ignore`
// biome-ignore lint/security/noGlobalEval -- needed to hide import.meta from CJS parser
return dirname(fileURLToPath(eval('import.meta.url')));
};
🧰 Tools
🪛 Biome (2.4.4)

[error] 20-20: eval() exposes to security risks and performance issues.

(lint/security/noGlobalEval)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/api/whatsapp.ts` around lines 11 - 21, Suppress the Biome linter for the
intentional eval in getDirname by adding a Biome ignore comment immediately
above the eval usage; specifically, place a biome noGlobalEval suppression
(e.g., /* biome-ignore noGlobalEval */) right before the line that calls
eval('import.meta.url') inside getDirname so the fallback using
fileURLToPath(import.meta.url) is allowed without CI lint failure.

Comment on lines +9 to +16
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
if (typeof __dirname !== 'undefined') {
return __dirname;
}
// @ts-ignore
return dirname(fileURLToPath(import.meta.url));
return dirname(fileURLToPath(eval('import.meta.url')));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add a Biome ignore for the intentional eval.

Biome flags eval as an error; if lint runs in CI, this will fail. Since the use is intentional and constant-string, add an inline ignore for the rule.

✅ Suggested fix
   // `@ts-ignore`
+  // biome-ignore lint/security/noGlobalEval -- needed to hide import.meta from CJS parser
   return dirname(fileURLToPath(eval('import.meta.url')));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
if (typeof __dirname !== 'undefined') {
return __dirname;
}
// @ts-ignore
return dirname(fileURLToPath(import.meta.url));
return dirname(fileURLToPath(eval('import.meta.url')));
// Cross-platform __dirname for CJS/ESM dual builds
// eval hides import.meta from the CJS parser to avoid SyntaxError
const getDirname = () => {
if (typeof __dirname !== 'undefined') {
return __dirname;
}
// `@ts-ignore`
// biome-ignore lint/security/noGlobalEval -- needed to hide import.meta from CJS parser
return dirname(fileURLToPath(eval('import.meta.url')));
🧰 Tools
🪛 Biome (2.4.4)

[error] 16-16: eval() exposes to security risks and performance issues.

(lint/security/noGlobalEval)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/utils/qr-generator.ts` around lines 9 - 16, The intentional use of eval
in getDirname triggers Biome's no-eval rule; add an inline Biome ignore comment
immediately before the eval expression in src/utils/qr-generator.ts (inside the
getDirname function) to silence the linter for this intentional, constant-string
usage — e.g., annotate the eval('import.meta.url') call with the appropriate
Biome ignore comment so linting in CI won't fail while leaving the logic in
getDirname unchanged.

@9cb14c1ec0 9cb14c1ec0 merged commit 23aa708 into master Mar 6, 2026
3 checks passed
@9cb14c1ec0 9cb14c1ec0 deleted the commonjs branch March 6, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot run CommonJS script since v6.5.0 - v.6.9.0

1 participant