Skip to content

fix(mdx): skip ?raw and other query imports in vinext:mdx transform#357

Merged
james-elicx merged 1 commit intomainfrom
fix/mdx-skip-query-imports
Mar 8, 2026
Merged

fix(mdx): skip ?raw and other query imports in vinext:mdx transform#357
james-elicx merged 1 commit intomainfrom
fix/mdx-skip-query-imports

Conversation

@james-elicx
Copy link
Copy Markdown
Collaborator

Summary

  • @mdx-js/rollup strips the query string before matching file extensions, so without a guard it compiles foo.mdx?raw as MDX and returns compiled JSX instead of raw text
  • The vinext:mdx proxy's transform hook now returns early when id.includes('?'), matching Vite's own convention for query imports

Changes

packages/vinext/src/index.ts

  • Added if (id.includes('?')) return; as the first line of the vinext:mdx transform hook

tests/pages-router.test.ts

  • Added "vinext:mdx transform skips ids that contain a query string (regression: ?raw)" — asserts the real proxy plugin returns undefined for ?raw, ?url, and ?inline ids
  • Added "vinext:mdx proxy logic — ?raw guard prevents delegate from compiling query imports" — self-contained unit test with a mock delegate that demonstrates both the broken behavior (without guard) and the fixed behavior (with guard)

@mdx-js/rollup strips the query before matching the file extension,
so it would compile 'foo.mdx?raw' as MDX and return compiled JSX
instead of the raw file text. The proxy now returns early when the
id contains '?', matching Vite's own convention for query imports.

Adds two regression tests: one unit test that reproduces the bug
inline (guard vs no-guard with a mock delegate), and one that
asserts the real vinext:mdx plugin returns undefined for query-param ids.
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 8, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@357

commit: 6a3d080

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 8, 2026

Example Preview Production Original
app-router-cloudflare preview production
pages-router-cloudflare preview production
app-router-playground preview production original
realworld-api-rest preview production
nextra-docs-template preview production
benchmarks preview production
hackernews preview production original

@james-elicx
Copy link
Copy Markdown
Collaborator Author

/bigbonk please review this pr

Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

The fix is correct and well-targeted. @mdx-js/rollup strips query strings before matching extensions, so import raw from './content.mdx?raw' would get compiled as MDX instead of being passed through as raw text. The one-line guard at the top of the transform hook is the right place and the right approach.

Code change

The if (id.includes("?")) return; guard at packages/vinext/src/index.ts:2413 is clean. It runs before the delegate null-check, which is correct — we want to bail even if a delegate is present. This matches Vite's own convention where query-suffixed IDs signal special import semantics (?raw, ?url, ?inline, ?worker, etc.).

The proxy only forwards config and transform to the delegate, and config doesn't receive module IDs, so no other hooks need the same treatment.

Tests

Both tests are well-structured:

  1. The integration test (line 879) exercises the real vinext:mdx proxy against ?raw, ?url, and ?inline — good coverage of the common Vite query suffixes.

  2. The mock-delegate test (line 892) is a nice addition that demonstrates the bug/fix independently. It proves the delegate is never called for query IDs and still works for plain IDs.

No issues found. Looks good to merge.

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Mar 8, 2026

PR #357 reviewed and approved.

Summary: The fix is correct. @mdx-js/rollup strips query strings before matching file extensions, so without this guard, import raw from './content.mdx?raw' would get compiled as MDX instead of returning raw text. The one-line if (id.includes("?")) return; at the top of the vinext:mdx transform hook is the right fix — it short-circuits before the delegate is called, matching Vite's convention for query-suffixed module IDs. The two tests cover both the real proxy plugin and the guard logic in isolation. No issues found.

github run

@james-elicx james-elicx merged commit a47d081 into main Mar 8, 2026
25 checks passed
@james-elicx james-elicx deleted the fix/mdx-skip-query-imports branch March 8, 2026 21:38
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.

1 participant