Skip to content

fix(unplugin): support definePage with webpack and rspack - #2829

Merged
posva merged 1 commit into
vuejs:mainfrom
Nsttt:fix/define-page-webpack-rspack
Oct 8, 2026
Merged

posva merged 1 commit into
vuejs:mainfrom
Nsttt:fix/define-page-webpack-rspack

Conversation

@Nsttt

@Nsttt Nsttt commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes definePage() with webpack and rspack/rsbuild. Originally reported in posva/unplugin-vue-router#531; it still happens with vue-router@5.4.0.

Problem

For .vue pages, the generated routes import the route data as page.vue?definePage&vue&lang.tsx. webpack and rspack match rules against the file path, so this module still goes through the .vue rule, and the vue query also triggers vue-loader's pitcher. The plugin's pre transform has already reduced the module to export default { ... }, so vue-loader fails:

File: ./src/pages/about.vue.js?definePage&vue&lang.tsx:1:1
  × Module Error (from rspack-vue-loader):
  │ At least one <template> or <script> is required in a single file component.

Fix

With webpack and rspack, the import uses an inline match resource, the same mechanism vue-loader uses for SFC blocks:

page.vue.definePage.tsx?definePage!=!page.vue?definePage&vue&lang.tsx

Rules now match page.vue.definePage.tsx, so the .vue rule and vue-loader's pitcher skip it and the tsx rules strip any TS. The loaders still read the real page.vue?definePage&vue&lang.tsx, so definePageTransform is unchanged. Vite and the other bundlers keep the current id.

Other changes:

  • The plugin passes meta.framework through a new internal _framework option.
  • Both codegen sites (generateRouteRecords and generateRouteResolver) now share a single getDefinePageImportId() helper.
  • DEFINE_PAGE_QUERY_RE no longer requires &vue, so the transform rule also matches the match resource id. That id can't contain a vue param, or vue-loader's pitcher would pick it up again.

Testing

  • New unit test for the webpack and rspack import ids. vitest run src/unplugin, test:types and lint pass.
  • rsbuild 2.2.12 + @rsbuild/plugin-vue 2.0.1: the build fails on 5.4.0 and passes with this branch. meta from definePage() ends up in the route records, including with TS syntax (as const) in a lang="ts" page.
  • webpack 5.111 + vue-loader 17: same failure on 5.4.0, same result with this branch.
  • The Vite file-based playground still builds.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed route page metadata imports for Vue files when using webpack or rspack, improving compatibility with these build tools.
    • Preserved direct metadata imports for TSX files, while ensuring Vue files continue to work with their original loader configuration.

The `?definePage` import of `.vue` pages still matched the `.vue` rule in
webpack and rspack, so vue-loader tried to compile the extracted route data
as an SFC and failed. Use an inline match resource so the module goes
through the tsx rules instead.

See posva/unplugin-vue-router#531
@netlify

netlify Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vue-router canceled.

Name Link
🔨 Latest commit 47f1742
🔍 Latest deploy log https://app.netlify.com/projects/vue-router/deploys/6ac77243adf6290008d85f5d

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: vuejs/router/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0c57e9f8-c579-4470-a3c2-3d3233922e67
📥 Commits

Reviewing files that changed from the base of the PR and between 596d481 and 47f1742.

📒 Files selected for processing (6)
  • packages/router/src/unplugin/codegen/generateRouteRecords.spec.ts
  • packages/router/src/unplugin/codegen/generateRouteRecords.ts
  • packages/router/src/unplugin/codegen/generateRouteResolver.ts
  • packages/router/src/unplugin/core/moduleConstants.ts
  • packages/router/src/unplugin/index.ts
  • packages/router/src/unplugin/options.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

Walkthrough

The plugin now uses framework metadata when generating definePage import IDs. Vue and non-Vue files receive IDs based on their language and framework, and the definePage query matcher accepts queries containing definePage.

Changes

Framework-aware definePage imports

Layer / File(s) Summary
Framework context and import ID generation
packages/router/src/unplugin/options.ts, packages/router/src/unplugin/index.ts, packages/router/src/unplugin/core/moduleConstants.ts
Options carries the framework value. The plugin passes it to option resolution. getDefinePageImportId builds IDs from the framework and file language, and the query matcher accepts any query containing definePage.
Generated definePage imports
packages/router/src/unplugin/codegen/generateRouteRecords.ts, packages/router/src/unplugin/codegen/generateRouteResolver.ts, packages/router/src/unplugin/codegen/generateRouteRecords.spec.ts
Both code generators use getDefinePageImportId. Tests check direct TSX imports and Vue match-resource requests for webpack and rspack.

Priority: ➖ Normal

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Unplugin
  participant RouteCodegen
  participant getDefinePageImportId
  participant WebpackRspackLoaders
  Unplugin->>RouteCodegen: Pass framework in resolved options
  RouteCodegen->>getDefinePageImportId: Request ID for a Vue file
  getDefinePageImportId-->>RouteCodegen: Return .definePage.tsx match-resource ID
  RouteCodegen->>WebpackRspackLoaders: Emit request chained through original Vue loader request
  WebpackRspackLoaders->>WebpackRspackLoaders: Match extracted module against TSX loader rules
Loading

Suggested reviewers: posva

Merge Risk: ⚪ Minimal · up to 47f17

This change fixes definePage handling under webpack and rspack by routing the extracted module through TSX rules. No actionable merge-blocking risk was found in the supplied context.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 6 files. 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 clearly and concisely describes the main change: fixing definePage handling for the webpack and rspack frameworks.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · 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 8, 2026

Copy link
Copy Markdown

Open in StackBlitz

pnpm add https://pkg.pr.new/vue-router@2829
npm i https://pkg.pr.new/vue-router@2829
yarn add https://pkg.pr.new/vue-router@2829.tgz

commit: 47f1742

@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.23%. Comparing base (596d481) to head (47f1742).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2829      +/-   ##
==========================================
+ Coverage   88.07%   88.23%   +0.16%     
==========================================
  Files          78       78              
  Lines        6289     6292       +3     
  Branches     2041     2041              
==========================================
+ Hits         5539     5552      +13     
+ Misses        661      651      -10     
  Partials       89       89              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@posva
posva merged commit 8c91f98 into vuejs:main Oct 8, 2026
10 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