fix(assets): allow ?raw imports of public files#22495
Open
ATOM00blue wants to merge 1 commit into
Open
Conversation
Importing a non-asset file from publicDir with ?raw (e.g. `/_redirects?raw`) threw a hard error in import analysis, even though the asset plugin already reads public files for ?raw requests. The dev public middleware also served such files statically instead of letting them be transformed, so the import resolved to raw source rather than a string module. Treat ?raw like ?url for public files: skip the non-asset error, resolve them in the asset plugin, and let the public middleware fall through to the transform pipeline. Fixes vitejs#21277
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for importing files from publicDir using the ?raw query so they’re transformed into raw string exports (similar to existing ?url handling), and validates the behavior in the playground.
Changes:
- Allow
/public/*?rawrequests to bypass the public static middleware so they can be transformed. - Treat
?rawas a valid asset-style import query for absolute (/) public paths during import analysis and asset handling. - Add a playground demo and an E2E test for
?rawimports from/public.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| playground/assets/index.html | Adds UI output + JS import to display raw contents imported from /public |
| playground/assets/tests/assets.spec.ts | Adds a new test asserting ?raw import behavior for a /public JS file |
| packages/vite/src/node/server/middlewares/static.ts | Ensures ?raw public requests are passed to transform pipeline (like ?url) |
| packages/vite/src/node/plugins/importAnalysis.ts | Allows /public imports when ?raw is present (avoids public-file restriction) |
| packages/vite/src/node/plugins/asset.ts | Includes rawRE in asset filter/guard so /public/*?raw is handled by asset plugin |
Comment on lines
+210
to
+215
| expect(await page.textContent('.public-js-raw-import-content')) | ||
| .toMatchInlineSnapshot(` | ||
| "document.querySelector('.raw-js').textContent = | ||
| '[success] Raw js from /public loaded' | ||
| " | ||
| `) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Importing a non-asset file from
publicDirwith?rawcurrently throws a hard error during import analysis:This is inconsistent: the asset plugin's
loadhook already knows how to read public files for?rawrequests (checkPublicFile(id, config) || cleanUrl(id)), so?rawis a legitimate way to inline a public file's contents. The error even suggests?url&rawas a workaround, which doesn't actually make sense (you get the raw text, not a URL).There were two reasons
?rawdidn't work for public files:importAnalysisonly exempted?url(and asset types) from the "non-asset file inside /public" guard, so?rawon a non-asset public file (e.g./_redirects,/foo.js) threw before the asset plugin ran.?urlfall through to be transformed), so the import resolved to the raw source served as a module rather than a string.Fix
Treat
?rawthe same way?urlis already treated for public files:importAnalysis: don't throw the non-asset error when the request has?raw.resolveId: resolve public files for?rawso the existingloadhandler picks them up.?rawrequests fall through to the transform pipeline.The existing hard error is preserved for plain public imports without
?raw/?url(e.g.import x from '/foo.js'), which remain genuine mistakes.Testing
Added a
?rawpublic-file import to theassetsplayground (/raw.js?raw) asserting the file contents are inlined. Verified it fails before the change and passes after, in both serve and build modes. Also manually checked extensionless (/_redirects?raw), asset-extension (/foo.txt?raw), and additional-query (?raw&v=1) cases all returnexport default "<content>".Fixes #21277