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.
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.
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.
To install the package, run the following command:
pnpm add -D @tanstack/vite-config| Framework | Dual Types | ESM only |
|---|---|---|
| Vanilla | tsdown | tsdown or tsc |
| Angular | Not required | ng-packagr (official tool) |
| React | tsdown | tsdown or tsc |
| Solid | Not required | tsc (preserves JSX, necessary for SSR) |
| Svelte | Not required | @sveltejs/package (official tool) |
| Vue | tsdown | tsdown or tsc |
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!
{
"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"
}
}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',
}),
)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:
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:
{
"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:
{
"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.