Skip to content

fix(bundle): don't use createRequire when targeting browser in Deno.bundle - #31534

Merged
nathanwhit merged 3 commits into
denoland:mainfrom
nathanwhit:bundle-api-browser-require
Dec 9, 2025
Merged

nathanwhit merged 3 commits into
denoland:mainfrom
nathanwhit:bundle-api-browser-require

Conversation

@nathanwhit

@nathanwhit nathanwhit commented Dec 8, 2025 •

Copy link
Copy Markdown
Member

Fixes #31524

Missed a code path in the original fix

@nathanwhit nathanwhit changed the title fix(bundle): don't use createRequire when targeting platform in Deno.bundle fix(bundle): don't use createRequire when targeting browser in Deno.bundle Dec 8, 2025
@coderabbitai

coderabbitai Bot commented Dec 8, 2025 •

Copy link
Copy Markdown

Walkthrough

This pull request modifies the bundle provider to conditionally apply Shim replacement based on platform and introduces a new test suite for browser platform bundling. The provider logic now computes whether Shim replacement should occur using platform-derived logic instead of a hardcoded boolean. A new test specification includes both API and CLI test cases that bundle a CommonJS module targeting the browser platform and verify that the createRequire function is not included in the output.

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: preventing createRequire from being used when the bundle targets the browser platform.
Description check ✅ Passed The description references issue #31524 and mentions fixing a missed code path, which directly relates to the changeset addressing the Deno.bundle() API platform option.
Linked Issues check ✅ Passed The changes directly address issue #31524 by modifying the bundle provider logic to respect the platform option in the API, matching CLI behavior and preventing createRequire inclusion for browser builds.
Out of Scope Changes check ✅ Passed All changes are scoped to the bundle feature: provider logic fix and test cases specifically validating browser platform behavior; no unrelated modifications present.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 17cdcc2 and b05e3ec.

📒 Files selected for processing (5)
  • cli/tools/bundle/provider.rs (1 hunks)
  • tests/specs/bundle/require_browser/__test__.jsonc (1 hunks)
  • tests/specs/bundle/require_browser/bundle.ts (1 hunks)
  • tests/specs/bundle/require_browser/foo.cjs (1 hunks)
  • tests/specs/bundle/require_browser/has-createrequire.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx,js}: For JavaScript runtime debugging, enable V8 inspector with --inspect-brk flag and connect Chrome DevTools to chrome://inspect
Use console.log() for debug prints in JavaScript runtime code

Files:

  • tests/specs/bundle/require_browser/bundle.ts
  • tests/specs/bundle/require_browser/has-createrequire.ts
tests/specs/**/__test__.jsonc

📄 CodeRabbit inference engine (CLAUDE.md)

Spec tests should be created in tests/specs/ with a __test__.jsonc file describing test steps and input files

Files:

  • tests/specs/bundle/require_browser/__test__.jsonc
tests/specs/**/{__test__.jsonc,*.out}

📄 CodeRabbit inference engine (CLAUDE.md)

Output assertions in spec tests should use __test__.jsonc inline fields or .out files with special matching syntax: [WILDCARD], [WILDLINE], [WILDCHAR], [WILDCHARS(n)], [UNORDERED_START]/[UNORDERED_END], and [# comment]

Files:

  • tests/specs/bundle/require_browser/__test__.jsonc
cli/tools/**/*.rs

📄 CodeRabbit inference engine (CLAUDE.md)

CLI tools should be implemented in cli/tools/<tool> or cli/tools/<tool>/mod.rs

Files:

  • cli/tools/bundle/provider.rs
**/*.rs

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.rs: For debugging Rust code, set breakpoints in IDE debuggers (VS Code with rust-analyzer, IntelliJ IDEA) or use lldb directly
Use eprintln!() or dbg!() macros for debug prints in Rust code

Files:

  • cli/tools/bundle/provider.rs

⚙️ CodeRabbit configuration file

Don't worry about coverage of Rust docstrings. Don't be nitpicky about it. Leave it to the author's judgement if such a documentation is necessary.

Files:

  • cli/tools/bundle/provider.rs
🧠 Learnings (3)
📚 Learning: 2025-11-24T16:19:37.808Z
Learnt from: CR
Repo: denoland/deno PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-24T16:19:37.808Z
Learning: Applies to tests/specs/**/__test__.jsonc : Spec tests should be created in `tests/specs/` with a `__test__.jsonc` file describing test steps and input files

Applied to files:

  • tests/specs/bundle/require_browser/__test__.jsonc
📚 Learning: 2025-11-24T16:19:37.808Z
Learnt from: CR
Repo: denoland/deno PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-24T16:19:37.808Z
Learning: Applies to tests/specs/**/{__test__.jsonc,*.out} : Output assertions in spec tests should use `__test__.jsonc` inline fields or `.out` files with special matching syntax: `[WILDCARD]`, `[WILDLINE]`, `[WILDCHAR]`, `[WILDCHARS(n)]`, `[UNORDERED_START]`/`[UNORDERED_END]`, and `[# comment]`

Applied to files:

  • tests/specs/bundle/require_browser/__test__.jsonc
📚 Learning: 2025-11-24T16:19:37.808Z
Learnt from: CR
Repo: denoland/deno PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-24T16:19:37.808Z
Learning: Applies to tests/specs/**/*.out : Use `[UNORDERED_START]` and `[UNORDERED_END]` in spec test output files to match lines in any order for non-deterministic output

Applied to files:

  • tests/specs/bundle/require_browser/__test__.jsonc
🧬 Code graph analysis (1)
cli/tools/bundle/provider.rs (1)
cli/tools/bundle/mod.rs (2)
  • bundle (290-353)
  • should_replace_require_shim (463-465)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: test release linux-x86_64
  • GitHub Check: test debug linux-x86_64
  • GitHub Check: test debug windows-x86_64
  • GitHub Check: test debug linux-aarch64
  • GitHub Check: test debug macos-aarch64
  • GitHub Check: build libs
  • GitHub Check: lint debug linux-x86_64
  • GitHub Check: lint debug windows-x86_64
🔇 Additional comments (5)
cli/tools/bundle/provider.rs (1)

140-183: Require‑shim decision now correctly derives from platform

Using crate::tools::bundle::should_replace_require_shim(bundle_flags.platform) here brings the provider’s process_result call in line with the CLI path in cli/tools/bundle/mod.rs, so browser bundles from Deno.bundle no longer get the Deno/Node createRequire shim injected. This is the right fix and keeps both branches (write_output vs. in‑memory) consistent.

tests/specs/bundle/require_browser/bundle.ts (1)

1-5: Runtime bundle invocation matches intended browser scenario

The Deno.bundle call correctly mirrors the CLI case (entrypoint ./foo.cjs, platform: "browser", outputPath: "bundle.js"), which is exactly what’s needed to validate parity between API and CLI behavior.

tests/specs/bundle/require_browser/has-createrequire.ts (1)

1-9: Helper script is simple and sufficient for the spec

Reading the target file from Deno.args[0] and checking contents.includes("createRequire") is a direct way to assert that the shim symbol isn’t present in the generated bundle; using run -A in the spec covers the required permissions.

tests/specs/bundle/require_browser/__test__.jsonc (1)

1-29: Spec test cleanly exercises both API and CLI bundle paths

The JSONC spec is well-structured: it uses tempDir, drives both --unstable-bundle bundle.ts and bundle --platform browser ..., and then asserts via has-createrequire.ts that the resulting bundle.js does not contain createRequire. [WILDCARD] usage for the non-deterministic bundle output is appropriate.

tests/specs/bundle/require_browser/foo.cjs (1)

1-11: CJS fixture is adequate for stressing require + node:os in bundles

This entrypoint gives the bundler a CommonJS require("node:os") to deal with and exports a simple object, which is exactly what the surrounding specs need. Since the tests only inspect the generated bundle and never execute it, the slightly unusual globalThis.window check doesn’t pose a problem here.


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.

@nathanwhit
nathanwhit enabled auto-merge (squash) December 8, 2025 21:50
@nathanwhit
nathanwhit merged commit 1317a36 into denoland:main Dec 9, 2025
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.

Deno.bundle does not match CLI behavior for the platform option

2 participants