Skip to content

router-generator: lazy-load Prettier (costs ~300–450ms per build even when formatting is off) #8887

Description

@benpsnyder

Which project does this relate to?

Router (@tanstack/router-generator, used by @tanstack/router-plugin and TanStack Start)

Describe the problem

packages/router-generator/src/utils.ts imports Prettier at module top level:

import * as prettier from 'prettier'

Prettier is only used by format(), which runs when enableRouteTreeFormatting is true. Because the import is static, every process that loads the generator pays Prettier's module evaluation, even when formatting is disabled. That includes every vite build and vite dev of a TanStack Start app.

Evidence

V8 CPU profiles (vite build --profile, Vite 8.3.6, @tanstack/[email protected]) of two TanStack Start apps in a large monorepo:

App Build wall time Prettier self time Share
A 13.5s 435ms 3.2%
B 7.7s 282ms 3.6%

Almost all of that time is module evaluation with no JS caller, i.e. it's the import, not formatting work.

Suggested change

  1. Load Prettier lazily inside format():
    async function format(source: string, config: Config) {
      const prettier = await import('prettier')
      // ...
    }
    This makes the cost zero when formatting is off, and moves it out of startup when it's on.
  2. Optionally, make formatting pluggable (a formatter option, or a documented stable unformatted output) so projects that format generated files with another tool (oxfmt, Biome, dprint) can turn off Prettier without their committed routeTree.gen.ts churning on every build.

Happy to send a PR for (1) if that direction works for you.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions