TanStack
Utilities

Vite

The Vite build setup was the result of several attempts to dual publish ESM and CJS for TanStack projects, while preserving compatibility with all Typescript module resolution options.

Do I need dual publishing?

ES Modules (ESM) is the standard for writing JavaScript modules. However, due to the historical dependency on CommonJS (CJS), many ecosystem tools and projects were initially incompatible with ESM. It is becoming exceedingly rare for this to be the case, and I would urge you to consider whether it is necessary to distribute CJS code at all. Sindre Sorhus has a good summary on this issue here.

Do I need this package?

Many alternatives have bene created recently, with the most notable being tsdown, built on top of rolldown. We will be adopting tsdown for future projects rather than continuing to use our custom Vite setup.

Installation

To install the package, run the following command:

shell
pnpm add -D @tanstack/vite-config

Frameworks

FrameworkDual TypesESM only
Vanillatsdowntsdown or tsc
AngularNot requiredng-packagr (official tool)
Reacttsdowntsdown or tsc
SolidNot requiredtsc (preserves JSX, necessary for SSR)
SvelteNot required@sveltejs/package (official tool)
Vuetsdowntsdown or tsc

Legacy Setup

The build config is opinionated, and was designed to work with our internal libraries. If you follow the below instructions, it may work for your library too!

package.json

  • Ensure "type": "module" is set.
  • Ensure you have Vite installed. Installing Publint is also recommended.
  • Change your build script to "build": "vite build && publint --strict"
  • Ensure you have an "exports" field. We use this, but you might have different requirements:
json
{
  "exports": {
    ".": {
      "import": {
        "types": "./dist/esm/index.d.ts",
        "default": "./dist/esm/index.js"
      },
      "require": {
        "types": "./dist/cjs/index.d.cts",
        "default": "./dist/cjs/index.cjs"
      }
    },
    "./package.json": "./package.json"
  }
}

tsconfig.json

  • Ensure your "include" field includes "vite.config.ts".
  • Set "moduleResolution" to "bundler".

vite.config.ts

  • Import mergeConfig and tanstackViteConfig.
  • Merge your custom config first, followed by tanstackViteConfig.
  • Please avoid modifying build in your custom config.
  • See an example below:
ts
import { tanstackViteConfig } from '@tanstack/vite-config'
import { defineConfig, mergeConfig } from 'vite'

const config = defineConfig({
  // Framework plugins, vitest config, etc.
})

export default mergeConfig(
  config,
  tanstackViteConfig({
    entry: './src/index.ts',
    srcDir: './src',
  }),
)

Internal declarations

With stripInternal: true in the tsconfig, members tagged /** @internal */ are removed from the emitted declarations. A package in the same workspace that reads one of those members then fails to type-check, because it resolves the trimmed declarations in dist. The internalDeclarations option writes one more set of declarations with stripInternal: false, which only those packages resolve:

ts
tanstackViteConfig({
  entry: './src/index.ts',
  srcDir: './src',
  // writes dist/internal; use { dir: 'internal-types' } for dist/internal-types
  internalDeclarations: true,
})

The set has the same layout and import extensions as dist/esm, and dist/esm and dist/cjs stay exactly as they are without the option. It is ESM only (.d.ts). beforeWriteDeclarationFile runs for these files too. The extra set costs one more TypeScript program per build.

@tanstack/vite-config does not edit package.json. Point a custom condition at the set, nested inside types so that types stays the first key, under both import and require:

json
{
  "exports": {
    ".": {
      "import": {
        "types": {
          "tanstack-internal": "./dist/internal/index.d.ts",
          "default": "./dist/esm/index.d.ts"
        },
        "default": "./dist/esm/index.js"
      },
      "require": {
        "types": {
          "tanstack-internal": "./dist/internal/index.d.ts",
          "default": "./dist/cjs/index.d.cts"
        },
        "default": "./dist/cjs/index.cjs"
      }
    }
  },
  "files": ["dist/esm", "dist/cjs"]
}

Wire the condition under every branch that has types. If a program resolves the untrimmed declarations through import and the trimmed ones through require, it sees two declarations of the same class, and a class with private members is then not assignable to itself (TS2322: Types have separate declarations of a private property). Since both branches point at the same .d.ts files, a CommonJS file that resolves them reads ESM declarations. TypeScript 6.0 accepts that with module: nodenext. With node16, or on TypeScript 5.6, it reports TS1479.

Then add the condition to the tsconfig of the packages that should see the internal members. customConditions needs moduleResolution set to bundler, node16 or nodenext:

json
{
  "compilerOptions": {
    "customConditions": ["tanstack-internal"]
  }
}

Put it in a base tsconfig that only those packages extend. A root tsconfig that apps, examples or benchmarks also extend would show them the internal members too.

Keep the internal set out of the published package, as in the files field above. The published exports still names dist/internal, but TypeScript falls back to the default types when that file is missing, so a consumer who sets the condition gets the trimmed declarations. You can ship the set by adding dist/internal to files, but that is risky: with project references, a referenced project resolves imports with its own customConditions. A project without the condition that references one with it loads both sets and fails with the same TS2322.