Skip to content

fix(bun): respect loader returned from load hook + feat(bun): support plugin.bun.loader for per-plugin loader resolution#601

Open
81reap wants to merge 4 commits into
unjs:mainfrom
81reap:feat/bun-loader-override
Open

fix(bun): respect loader returned from load hook + feat(bun): support plugin.bun.loader for per-plugin loader resolution#601
81reap wants to merge 4 commits into
unjs:mainfrom
81reap:feat/bun-loader-override

Conversation

@81reap
Copy link
Copy Markdown
Contributor

@81reap 81reap commented May 22, 2026

I am volunteering and working on getting a bun plugin for TanStack/router and found this bug.

Summary by CodeRabbit

  • New Features

    • Load hooks can now return an explicit loader override; plugin configuration may also supply a loader value or function to determine a module's loader.
  • Tests

    • Added unit tests covering loader selection and precedence for virtual modules, ensuring hook-provided loaders take priority over inferred or configured loaders.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 67384b07-48e8-4273-a963-feab001f55ce

📥 Commits

Reviewing files that changed from the base of the PR and between 50d8d7c and 7668984.

📒 Files selected for processing (4)
  • src/bun/index.ts
  • src/bun/utils.ts
  • src/types.ts
  • test/unit-tests/bun/nested.test.ts

📝 Walkthrough

Walkthrough

Adds LoadResult and unwrapLoader; Bun adapter records load-hook-provided loader and active plugin, then selects loader with precedence: load-hook override → plugin.bun.loader (possibly invoked) → original loader → guessLoader(id). Tests cover virtual-module loader resolution and precedence.

Changes

Bun Loader Override Support

Layer / File(s) Summary
LoadResult type contract and hook signature
src/types.ts
Bun Loader imported as BunLoader; new LoadResult extends the transform result object with optional loader?: BunLoader; HookFnMap.load now returns Thenable<LoadResult>.
unwrapLoader helper
src/bun/utils.ts
Exported unwrapLoader(loader, code, id) that invokes a functional Bun loader with (code, id) or returns the loader value unchanged.
Bun adapter loader extraction and selection
src/bun/index.ts
processLoadTransform records activePlugin and loaderOverride from load hook results, types handler returns as LoadResult, and selects final loader using precedence: loaderOverrideunwrapLoader(plugin.bun.loader, code, id) → original loader arg → guessLoader(id).
Test coverage for loader precedence
test/unit-tests/bun/nested.test.ts
Adds tests for load-hook-provided loader, static plugin.bun.loader, functional plugin.bun.loader invocation, and precedence when both hook and plugin loader exist.

Sequence Diagram

sequenceDiagram
  participant Plugin as Plugin.load hook
  participant Adapter as Bun adapter (processLoadTransform)
  participant Unwrap as unwrapLoader
  Plugin->>Adapter: return { code, loader? } | or string code
  Adapter->>Adapter: record loaderOverride, activePlugin
  Adapter->>Unwrap: call unwrapLoader(plugin.bun.loader, code, id)
  Unwrap-->>Adapter: resolved plugin loader
  Adapter-->>Caller: return contents and chosen loader (loaderOverride || pluginLoader || originalLoader || guessLoader)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • sxzz

Poem

🐰 I nibble code with careful hops,
LoadResult gives loader props,
Adapter listens, notes the cue,
unwraps plugin loaders too,
Hooks decide which loader pops!

🚥 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 title accurately describes the two main changes: fixing loader respect from load hooks and adding plugin.bun.loader support for per-plugin loader resolution.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/bun/index.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

src/bun/utils.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

src/types.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

  • 1 others

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.

@81reap 81reap changed the title fix(bun) :: overrider loader fix(bun) :: override loader May 22, 2026
@sxzz sxzz linked an issue May 22, 2026 that may be closed by this pull request
Use the loader returned by object load results before falling back to Bun's provided loader or guessed loader.
@81reap 81reap force-pushed the feat/bun-loader-override branch from d6d0846 to cc573f1 Compare May 22, 2026 14:47
@81reap 81reap changed the title fix(bun) :: override loader fix(bun): respect loader returned from load hook May 22, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 22, 2026

Open in StackBlitz

npm i https://pkg.pr.new/unplugin@601

commit: 7668984

@81reap
Copy link
Copy Markdown
Contributor Author

81reap commented May 22, 2026

@sxzz I've updated the commit message and fixed the merge conflict, lmk if there's any more feedback :)

81reap added 2 commits May 22, 2026 23:28
Add `loader` to the `bun` plugin option block, accepting either a `Loader` literal or `(code, id) => Loader` function, mirroring the existing `plugin.esbuild.loader` API.
@81reap 81reap changed the title fix(bun): respect loader returned from load hook fix(bun): respect loader returned from load hook + feat(bun): support plugin.bun.loader for per-plugin loader resolution May 23, 2026
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.

Bun adapter: load hook cannot override the inferred Loader

1 participant