Skip to content

Module runner: send source maps separately from module code #23890

In progress

Description

@maschwenk

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, fetchModule returns the map in a new optional ViteFetchResult.map and leaves it out of code. //# sourceURL stays in code since stack frames get the file name from it, and startOffset applies to the map as it does today.
  • ModuleRunnerOptions.separateSourceMap?: boolean, default false. The runner sends the flag unless sourcemapInterceptor is 'node', because node reads the inline comment itself.
  • getModuleSourceMapById uses meta.map when 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 map is missing and the runner reads the comment.
  • remoteRunner.inlineSourceMap: false still wins: no map either way.
  • Externalized and cached results don't change. Invalidation already clears meta and map, 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

Activity

  1. changed the title [-]module runner: send source maps separately from module code[/-] [+]Module runner: send source maps separately from module code[/+] on Oct 25, 2026
  2. added
    p2-nice-to-have
    on Nov 5, 2026
  3. sapphi-red commented on Nov 5, 2026

    @sapphi-red
    Member

    Sounds good to me. Feel free to open a PR.

    I think we should add a new option in DevEnvironmentOptions that indicates whether the sourcemap should be available for the debugger becauseFetchFunctionOptions.separateSourceMap and ModuleRunnerOptions.separateSourceMap is not exposed to the end-user.

    If that option is disabled, use sourcemapInterceptor is 'prepareStackTrace' by default and also avoid appending sourceMappingURL=.
    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 before this.evaluator.runInlinedModule call.

    We probably would still need FetchFunctionOptions.separateSourceMap for backward compat. But I guess ModuleRunnerOptions.separateSourceMap won't be needed as the new option in DevEnvironmentOptions covers it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp2-nice-to-have

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions