Skip to content

fix: skip fallback sourcemap generation for ?raw imports#22148

Merged
sapphi-red merged 4 commits intovitejs:mainfrom
semimikoh:fix/raw-fallback-sourcemap-oom
Apr 20, 2026
Merged

fix: skip fallback sourcemap generation for ?raw imports#22148
sapphi-red merged 4 commits intovitejs:mainfrom
semimikoh:fix/raw-fallback-sourcemap-oom

Conversation

@semimikoh
Copy link
Copy Markdown
Contributor

@semimikoh semimikoh commented Apr 6, 2026

Summary

Skip dev-server fallback sourcemap generation for ?raw imports.

?raw responses already embed the full source text in the generated JS module. Injecting an additional fallback inline sourcemap
duplicates that content and can lead to severe memory amplification for large newline-heavy files.

This change keeps ?raw import behavior intact, but disables fallback sourcemap injection for raw requests only.

Changes

  • add skipFallbackSourcemap support to send()
  • pass skipFallbackSourcemap: true for ?raw requests in transform middleware
  • add regression tests for normal fallback sourcemap injection and the raw-import skip case

Why

The reported OOM happens in the dev-server fallback sourcemap path rather than in the raw file read alone. This is a narrow fix that
avoids changing ?raw semantics while removing the highest-cost duplication path.

Validation

  • pnpm exec eslint packages/vite/src/node/server/send.ts packages/vite/src/node/server/middlewares/transform.ts packages/vite/src/node/ server/__tests__/send.spec.ts
  • pnpm exec tsc -p packages/vite/src/node/tsconfig.json --noEmit
  • pnpm exec vitest run packages/vite/src/node/server/__tests__/send.spec.ts packages/vite/src/node/server/__tests__/ transformRequest.spec.ts

Fixes part of #22132

@bluwy
Copy link
Copy Markdown
Member

bluwy commented Apr 13, 2026

What if we fix it this way?

return {
code: `export default ${JSON.stringify(
await fsp.readFile(file, 'utf-8'),
)}`,
moduleType: 'js', // NOTE: needs to be set to avoid double `export default` in `?raw&.txt`s
}

We can add map: { mappings: '' } so that we generate an empty sourcemap on behalf, then later send.ts won't generate a sourcemap for it.

Honestly, I still think generating a fallback sourcemap in general doesn't make sense at all, but this could be an easy workaround for now. ref #13514. cc @sapphi-red

@sapphi-red
Copy link
Copy Markdown
Member

What if we fix it this way?
[code block]
We can add map: { mappings: '' } so that we generate an empty sourcemap on behalf, then later send.ts won't generate a sourcemap for it.

That sounds better to me.

@semimikoh
Copy link
Copy Markdown
Contributor Author

@bluwy @sapphi-red Updated to emit map: { mappings: '' } from the asset plugin instead of adding skipFallbackSourcemap flag.

Comment thread packages/vite/src/node/server/__tests__/send.spec.ts Outdated
@semimikoh semimikoh force-pushed the fix/raw-fallback-sourcemap-oom branch from 61bd45b to 401b3c0 Compare April 17, 2026 06:57
@sapphi-red sapphi-red added the p2-edge-case Bug, but has workaround or limited in scope (priority) label Apr 20, 2026
@sapphi-red sapphi-red changed the title fix: skip fallback sourcemap generation for ?raw imports fix: skip fallback sourcemap generation for ?raw imports Apr 20, 2026
@sapphi-red sapphi-red merged commit 3ec9cda into vitejs:main Apr 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-edge-case Bug, but has workaround or limited in scope (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants