Skip to content

fix(webpack): surface resolveId errors and emit Uint8Array assets byte-exactly - #632

Merged
antfu merged 4 commits into
unjs:mainfrom
antfubot:fix/webpack-resolve-errors-and-emit-bytes
Oct 27, 2026
Merged

antfu merged 4 commits into
unjs:mainfrom
antfubot:fix/webpack-resolve-errors-and-emit-bytes

Conversation

@antfubot

@antfubot antfubot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements plans/003-webpack-resolveid-errors-and-emitfile-bytes.md: two small, independent webpack adapter bugs.

  • A throwing resolveId now fails the build instead of hanging it. The resolver tap is registered with tapAsync but was given an async function with no try/catch. Tapable ignores the returned promise, so when a hook threw, the callback was never called: a one-shot build hung until the test timeout, and in watch mode it surfaced as an unhandled rejection. rspack uses tapPromise and already propagated the error; fix(webpack): forward async load hook errors to the loader callback #610 fixed the same gap in the webpack load loader but not in the resolver. The body after the two early-return guards is now wrapped in try/catch, and errors are normalised to Error the same way the loaders do.
  • emitFile with a Uint8Array emits exactly those bytes. getSource did Buffer.from(fileSource.buffer), which wraps the entire backing ArrayBuffer and ignores byteOffset/byteLength. Node pools small Buffers, so this.emitFile({ source: Buffer.from('tiny') }) emitted the whole 8 KiB pool. Now Buffer.from(fileSource), matching src/rspack/context.ts.

Tests

  • resolve-id.test.ts: webpack and rspack builds reject with the hook's message when resolveId throws. On main, the webpack case times out (10 s).
  • webpack/context.test.ts: emitting a pooled 4-byte Buffer produces a 4-byte asset. On main, the emitted RawSource.buffer() is the pool.

pnpm test → 215 passed; lint/typecheck green. The diff in src/webpack/index.ts is one indentation level; git diff -w shows the 8 real lines.


Created with the help of an agent.

Summary by CodeRabbit

  • Bug Fixes
    • Webpack and Rspack builds now report errors thrown during module resolution, including thrown values that are not already errors.
    • Emitted assets created from buffers now preserve the buffer’s visible bytes without including unrelated data from its shared backing storage.
    • Existing module resolution behavior, including skipping virtual-prefixed modules, remains unchanged.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d87c71ed-6834-43b5-9db8-1ea92fb18b1e
Commits

Reviewing files that changed from the base of the PR and between ec6b74e and 36b4b01.

Files selected for processing: 5
  • plans/README.md
  • src/webpack/context.ts
  • src/webpack/index.ts
  • test/unit-tests/resolve-id/resolve-id.test.ts
  • test/unit-tests/webpack/context.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

The webpack adapter now preserves the bytes in a Uint8Array when creating a source. It also sends errors from resolveId processing to the resolver callback. Tests cover both changes, and the execution-order table marks Plan 003 as done.

Changes: Webpack adapter behavior

Layer / File(s) Summary
Preserve emitted asset bytes
src/webpack/context.ts, test/unit-tests/webpack/context.test.ts
getSource passes the Uint8Array itself to Buffer.from. A test checks that emitted asset bytes match the original Buffer view.
Propagate resolveId errors
src/webpack/index.ts, test/unit-tests/resolve-id/resolve-id.test.ts, plans/README.md
resolveId processing now catches failures and passes an Error to the callback, converting non-Error values. Webpack and Rspack tests check that builds reject with the expected error. The execution-order table marks Plan 003 as DONE.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: antfu

Merge Risk: Minimal · previous review 36b4b

The reported asset-byte and resolver-error fixes have no identified merge-blocking issue; merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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 clearly and concisely summarizes both primary changes: forwarding webpack resolveId errors and preserving the exact byte range of Uint8Array assets.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 4 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@pkg-pr-new

pkg-pr-new Bot commented Oct 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/unjs/unplugin@632

commit: 36b4b01

@antfu
antfu merged commit 962ede3 into unjs:main Oct 27, 2026
11 checks passed
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.

2 participants