Skip to content

fix(webpack,rspack): avoid normalizing virtual module ids in load hook - #645

Open
ychampion wants to merge 1 commit into
unjs:mainfrom
ychampion:fix/windows-virtual-module-slash-prefix
Open

ychampion wants to merge 1 commit into
unjs:mainfrom
ychampion:fix/windows-virtual-module-slash-prefix

Conversation

@ychampion

@ychampion ychampion commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #421

Summary

On Windows, when resolveId returns a virtual module ID starting with / (e.g. /__virtual/1, as used by unplugin-vue-router), webpack and rspack load loaders decoded the virtual module prefix and then passed id through normalizeAbsolutePath(id). Because path.win32.isAbsolute('/__virtual/1') is true, path.win32.normalize rewrote / to \ (\__virtual\1), while loadInclude and filter received /__virtual/1.

Changes

  • src/webpack/loaders/load.ts & src/rspack/loaders/load.ts: apply normalizeAbsolutePath only to non-virtual resource paths alongside unescapeResourcePath, keeping decoded virtual module IDs untouched.
  • test/unit-tests/webpack/loaders/load.test.ts, test/unit-tests/rspack/loaders/load.test.ts, & test/unit-tests/virtual-id/virtual-id.test.ts: add unit and integration tests verifying / prefixed virtual module IDs across webpack, rspack, vite, rollup, esbuild, and bun.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed handling of virtual module IDs that begin with a slash, so they are passed to plugin load handlers in the expected form.
    • Improved consistency when loading these modules across Vite, Rollup, Webpack, Rspack, esbuild, and Bun.
    • Added coverage to verify virtual modules load correctly and, where applicable, appear in the resulting bundle.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Webpack and Rspack now normalize non-virtual resource IDs before calling the load hook. Tests verify that slash-prefixed virtual IDs retain their leading slash across six bundlers.

Changes: Virtual ID path normalization

Layer / File(s) Summary
Normalize non-virtual resource IDs
src/rspack/loaders/load.ts, src/webpack/loaders/load.ts
Both loaders normalize non-virtual IDs before calling the load hook. They pass the resulting ID directly to the hook, preserving decoded virtual IDs.
Test slash-prefixed virtual IDs
test/unit-tests/rspack/loaders/load.test.ts, test/unit-tests/webpack/loaders/load.test.ts, test/unit-tests/virtual-id/virtual-id.test.ts
Loader tests check that /__virtual/1 reaches the load hook with its leading slash. Cross-bundler tests cover Vite, Rollup, Webpack, Rspack, esbuild, and Bun. Webpack and Rspack tests also check generated bundle contents.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: sxzz, antfu, antfubot

Merge Risk: Minimal · up to 785cc

This fixes slash-prefixed virtual module IDs being rewritten to backslashes on Windows in webpack and rspack. The change is small and localized, with added tests, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two packages affected (webpack and rspack) and the core change: the load hook no longer normalizes virtual module IDs. The raw summary and objectives confirm that normalization now…
Linked Issues check Passed Issue #421 requires that /-prefixed virtual module IDs reach the load hook unchanged on Windows. At the reviewed head, src/webpack/loaders/load.ts decodes IDs that start with `plugin.__virtualMo…
Out of Scope Changes check Passed All reported changes relate to #421. The source changes in the webpack and rspack load loaders apply the fix only to virtual module ID handling. The added unit tests cover the webpack and rspack loa…
Full details: Title check

Explanation

The title names the two packages affected (webpack and rspack) and the core change: the load hook no longer normalizes virtual module IDs. The raw summary and objectives confirm that normalization now applies only to non-virtual resource paths, which fixes the Windows backslash issue in issue #421. The title is specific and matches the main change. It uses the conventional commit format and is 70 characters long.

Full details: Linked Issues check

Explanation

Issue #421 requires that /-prefixed virtual module IDs reach the load hook unchanged on Windows. At the reviewed head, src/webpack/loaders/load.ts decodes IDs that start with plugin.__virtualModulePrefix and does not pass them through normalizeAbsolutePath. Only non-virtual resources run normalizeAbsolutePath(unescapeResourcePath(id, 'webpack')). This removes the / to \ rewrite that caused \__virtual\1 in the issue. The PR summary and raw summary report the same change in src/rspack/loaders/load.ts (+2/-2). I could not read the rspack diff or file, so the rspack result relies on the reported summary. The PR also adds tests in test/unit-tests/webpack/loaders/load.test.ts, test/unit-tests/rspack/loaders/load.test.ts, and test/unit-tests/virtual-id/virtual-id.test.ts for /__virtual/1. I did not execute these tests. No non-coding requirements exist in #421.

Full details: Out of Scope Changes check

Explanation

All reported changes relate to #421. The source changes in the webpack and rspack load loaders apply the fix only to virtual module ID handling. The added unit tests cover the webpack and rspack loaders. The integration tests in test/unit-tests/virtual-id/virtual-id.test.ts cover /-prefixed virtual IDs across bundlers, which supports the same objective. The summary reports no changes to exported declarations. I found no unrelated changes in the evidence reviewed. Verification of the rspack diff and of the Windows behavior was not possible, so this result relies on the reported summaries.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

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/rspack/loaders/load.ts

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

src/webpack/loaders/load.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

test/unit-tests/rspack/loaders/load.test.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

  • 2 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

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: 5b725553-df6f-42b8-aa1e-cfc0bb39adde
Commits

Reviewing files that changed from the base of the PR and between 5daf6e6 and 785cc7e.

Files selected for processing: 5

  • src/rspack/loaders/load.ts
  • src/webpack/loaders/load.ts
  • test/unit-tests/rspack/loaders/load.test.ts
  • test/unit-tests/virtual-id/virtual-id.test.ts
  • test/unit-tests/webpack/loaders/load.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.

This branch has not been deployed

No deployments
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.

webpack / rspack load virtual module id with / prefix on windows caused error

1 participant