Repository navigation
Description
Description
When vite's module runner loads a module, the server sends the code back with the source map appended as a base64 comment (inlineSourceMap in packages/vite/src/node/ssr/fetchModule.ts). The runner keeps that whole string as meta.code and the evaluator hands all of it to v8.
The comment is often as big as the code. V8 scans every byte of it on compile, and it sits in memory for the life of the module.
With sourcemapInterceptor: 'prepareStackTrace' the only thing in vite that reads the comment is getModuleSourceMapById, and it reads it out of meta.code, not out of what v8 compiled.
On a private 100 file typescript backend suite under vitest, 8 workers, leaving just the comment out of what v8 compiles cut wall time about 4.6% (95% interval 2.4 to 6.5) and cpu 4.5%.
I'd like to send a PR for this. Branch is ready:
main...maschwenk:vite:feat/module-runner-separate-source-map
Suggested solution
Send the map as data next to the code instead of inside it.
FetchFunctionOptions.separateSourceMap?: boolean. When set,fetchModulereturns the map in a new optionalViteFetchResult.mapand leaves it out ofcode.//# sourceURLstays incodesince stack frames get the file name from it, andstartOffsetapplies to the map as it does today.ModuleRunnerOptions.separateSourceMap?: boolean, defaultfalse. The runner sends the flag unlesssourcemapInterceptoris'node', because node reads the inline comment itself.getModuleSourceMapByIdusesmeta.mapwhen it's there and falls back to the comment.
Compatibility:
- Old runners don't send the flag and get inline code.
- Old servers ignore the flag, so
mapis missing and the runner reads the comment. remoteRunner.inlineSourceMap: falsestill wins: no map either way.- Externalized and cached results don't change. Invalidation already clears
metaandmap, so hmr picks up the new map. - Runners share the transform result. Inline mode writes the map into it; the separate path doesn't, and drops a map an inline-mode runner already appended.
It's opt-in rather than the default for 'prepareStackTrace' because a debugger attached to the process reads the map from the comment in the compiled script. Without it, breakpoints land in generated code.
Vitest asks fetchModule for code without a map and appends its own, so it would fill map itself to benefit. The vitest issue for the same problem:
Alternative
Strip the comment in the evaluator right before compile. Smaller change, but the server still builds and sends the base64 string and the runner still has to search for it.
Additional context
Related, faster extraction of the inline map:
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Activity
[-]module runner: send source maps separately from module code[/-][+]Module runner: send source maps separately from module code[/+]sapphi-red commentedon Nov 5, 2026
Sounds good to me. Feel free to open a PR.
I think we should add a new option in
DevEnvironmentOptionsthat indicates whether the sourcemap should be available for the debugger becauseFetchFunctionOptions.separateSourceMapandModuleRunnerOptions.separateSourceMapis not exposed to the end-user.If that option is disabled, use
sourcemapInterceptoris'prepareStackTrace'by default and also avoid appendingsourceMappingURL=.Regardless of that option, I think it is better to append
sourceMappingURL=on the module runner side as that will likely to reduce the size of the payload sent to the module runner. I think it can happen right beforethis.evaluator.runInlinedModulecall.We probably would still need
FetchFunctionOptions.separateSourceMapfor backward compat. But I guessModuleRunnerOptions.separateSourceMapwon't be needed as the new option inDevEnvironmentOptionscovers it.