Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/vite-config-internal-declarations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/vite-config': minor
---

Add the `internalDeclarations` option. It writes one more set of ESM declarations with `stripInternal: false` to `dist/internal` (or `dist/<dir>`), so packages in the same workspace can read members tagged `@internal` through a custom export condition while the published `dist/esm` and `dist/cjs` declarations stay trimmed.
57 changes: 57 additions & 0 deletions docs/vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,60 @@ export default mergeConfig(
}),
)
```

### 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`.
47 changes: 47 additions & 0 deletions integrations/internal-declarations-consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@tanstack/integration-internal-declarations-consumer",
"version": "1.0.0",
"private": true,
"type": "module",
"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"
},
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "vite build",
"test:build": "vitest && publint --strict"
},
"nx": {
"targets": {
"build": {
"dependsOn": [
"^build"
]
}
}
},
"dependencies": {
"@tanstack/integration-internal-declarations": "workspace:*"
},
"devDependencies": {
"@tanstack/vite-config": "workspace:*",
"typescript": "catalog:",
"vite": "catalog:",
"vitest": "catalog:"
}
}
14 changes: 14 additions & 0 deletions integrations/internal-declarations-consumer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Router } from '@tanstack/integration-internal-declarations'
import { extraInternal } from '@tanstack/integration-internal-declarations/extra'
import type { Match } from '@tanstack/integration-internal-declarations'

// Reads members that are tagged `@internal` in the library.
export function pendingCount(router: Router): number {
return router._pending.size
}

export function strictParams(match: Match): Record<string, string> {
return match._strictParams
}

export const extraName: string = extraInternal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Router } from '@tanstack/integration-internal-declarations'
import type { router as esmRouter } from './esm.mjs'

// Fails with "Types have separate declarations of a private property" if this
// file resolved a different declaration file for `Router` than `esm.mts`.
export const router: Router = {} as typeof esmRouter
export const pending: number = router._pending.size
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Router } from '@tanstack/integration-internal-declarations'

export const router: Router = new Router('/')
export const pending: number = router._pending.size
55 changes: 55 additions & 0 deletions integrations/internal-declarations-consumer/tests/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import ts from 'typescript'
import { describe, expect, it } from 'vitest'

const __dirname = dirname(fileURLToPath(import.meta.url))
const rootDir = resolve(__dirname, '..')

function typeCheck(
customConditions: Array<string>,
rootNames: Array<string> = [`${rootDir}/src/index.ts`],
overrides: ts.CompilerOptions = {},
) {
const { config } = ts.readConfigFile(
`${rootDir}/tsconfig.json`,
ts.sys.readFile,
)
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, rootDir)
const program = ts.createProgram(rootNames, {
...options,
...overrides,
customConditions,
})
return ts
.getPreEmitDiagnostics(program)
.map((diagnostic) =>
ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
)
}

describe('check a sibling that reads @internal members', () => {
it('should type-check with the custom condition', () => {
expect(typeCheck(['tanstack-internal'])).toEqual([])
})

it('should fail to type-check without the custom condition', () => {
expect(typeCheck([])).toEqual([
`Module '"@tanstack/integration-internal-declarations/extra"' has no exported member 'extraInternal'.`,
`Property '_pending' does not exist on type 'Router'.`,
`Property '_strictParams' does not exist on type 'Match'.`,
])
})

it('should load one declaration tree for ESM and CJS files', () => {
const mixed = [
`${rootDir}/tests/mixed/esm.mts`,
`${rootDir}/tests/mixed/cjs.cts`,
]
const nodeNext = {
module: ts.ModuleKind.NodeNext,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
}
expect(typeCheck(['tanstack-internal'], mixed, nodeNext)).toEqual([])
})
})
24 changes: 24 additions & 0 deletions integrations/internal-declarations-consumer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"useDefineForClassFields": true,
"module": "ESNext",

/* Bundler mode */
"moduleResolution": "bundler",
"customConditions": ["tanstack-internal"],
"resolveJsonModule": true,
"allowImportingTsExtensions": true,

/* Linting */
"strict": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true
},
"include": ["src", "tests", "vite.config.ts"]
}
17 changes: 17 additions & 0 deletions integrations/internal-declarations-consumer/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { tanstackViteConfig } from '@tanstack/vite-config'
import { defineConfig, mergeConfig } from 'vitest/config'

const config = defineConfig({
test: {
name: 'internal-declarations-consumer-integration',
watch: false,
},
})

export default mergeConfig(
config,
tanstackViteConfig({
entry: './src/index.ts',
srcDir: './src',
}),
)
57 changes: 57 additions & 0 deletions integrations/internal-declarations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@tanstack/integration-internal-declarations",
"version": "1.0.0",
"private": true,
"type": "module",
"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"
}
},
"./extra": {
"import": {
"types": {
"tanstack-internal": "./dist/internal/extra.d.ts",
"default": "./dist/esm/extra.d.ts"
},
"default": "./dist/esm/extra.js"
},
"require": {
"types": {
"tanstack-internal": "./dist/internal/extra.d.ts",
"default": "./dist/cjs/extra.d.cts"
},
"default": "./dist/cjs/extra.cjs"
}
},
"./package.json": "./package.json"
},
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"files": [
"dist/esm",
"dist/cjs"
],
"scripts": {
"build": "vite build",
"test:build": "vitest && publint --strict"
},
"devDependencies": {
"@tanstack/vite-config": "workspace:*",
"vite": "catalog:",
"vitest": "catalog:"
}
}
12 changes: 12 additions & 0 deletions integrations/internal-declarations/snap/cjs/extra.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/extra.ts
function matchId(match) {
return match.id;
}
/** @internal */
var extraInternal = "extra";
//#endregion
exports.extraInternal = extraInternal;
exports.matchId = matchId;

//# sourceMappingURL=extra.cjs.map
1 change: 1 addition & 0 deletions integrations/internal-declarations/snap/cjs/extra.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions integrations/internal-declarations/snap/cjs/extra.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Match } from './index.cjs';
export declare function matchId(match: Match): string;
32 changes: 32 additions & 0 deletions integrations/internal-declarations/snap/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_paths = require("./nested/paths.cjs");
//#region src/index.ts
var Router = class {
basepath;
seen = 0;
/** @internal */
_pending = /* @__PURE__ */ new Map();
constructor(basepath) {
this.basepath = basepath;
}
resolve(path) {
this.seen++;
return require_paths.joinPaths(this.basepath, path);
}
/** @internal */
_commit(match) {
this._pending.set(match.id, match);
}
};
/** @internal */
function createMatch(id) {
return {
id,
_strictParams: {}
};
}
//#endregion
exports.Router = Router;
exports.createMatch = createMatch;

//# sourceMappingURL=index.cjs.map
1 change: 1 addition & 0 deletions integrations/internal-declarations/snap/cjs/index.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions integrations/internal-declarations/snap/cjs/index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Match {
id: string;
}
export declare class Router {
basepath: string;
private seen;
constructor(basepath: string);
resolve(path: string): string;
}
8 changes: 8 additions & 0 deletions integrations/internal-declarations/snap/cjs/nested/paths.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//#region src/nested/paths.ts
function joinPaths(...paths) {
return paths.join("/").replace(/\/{2,}/g, "/");
}
//#endregion
exports.joinPaths = joinPaths;

//# sourceMappingURL=paths.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function joinPaths(...paths: Array<string>): string;
2 changes: 2 additions & 0 deletions integrations/internal-declarations/snap/esm/extra.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Match } from './index.js';
export declare function matchId(match: Match): string;
10 changes: 10 additions & 0 deletions integrations/internal-declarations/snap/esm/extra.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integrations/internal-declarations/snap/esm/extra.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading