fix(mdx): skip ?raw and other query imports in vinext:mdx transform#357
fix(mdx): skip ?raw and other query imports in vinext:mdx transform#357james-elicx merged 1 commit intomainfrom
Conversation
@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.
commit: |
|
|
/bigbonk please review this pr |
There was a problem hiding this comment.
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:
-
The integration test (line 879) exercises the real
vinext:mdxproxy against?raw,?url, and?inline— good coverage of the common Vite query suffixes. -
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.
|
PR #357 reviewed and approved. Summary: The fix is correct. |
Summary
@mdx-js/rollupstrips the query string before matching file extensions, so without a guard it compilesfoo.mdx?rawas MDX and returns compiled JSX instead of raw textvinext:mdxproxy'stransformhook now returns early whenid.includes('?'), matching Vite's own convention for query importsChanges
packages/vinext/src/index.tsif (id.includes('?')) return;as the first line of thevinext:mdxtransformhooktests/pages-router.test.ts"vinext:mdx transform skips ids that contain a query string (regression: ?raw)"— asserts the real proxy plugin returnsundefinedfor?raw,?url, and?inlineids"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)