fix(build): keep internal types private in bundled d.ts and avoid a self-referencing JSX.IntrinsicElements (#5485)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 37ce069 · Browse files · 11 files·+77 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+77 added−6 removed
- other:+77 added−6 removed
Provenance
- Commit
be1f749- Tree
964ab2a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
37ce069
11 files changed+77 added−6 removed
modified:adapters/aws-lambda/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesmodified:adapters/bun/vite.config.ts+2 added
1234567891011121314import { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), },})modified:adapters/cloudflare-workers/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesmodified:adapters/deno/vite.config.ts+2 added
1234567891011121314154 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,4 unmodified linesmodified:adapters/lambda-edge/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesmodified:adapters/netlify/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesmodified:adapters/service-worker/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesmodified:adapters/vercel/vite.config.ts+2 added
1234567891011121314152 unmodified linesimport { defineConfig } from 'vite-plus'import { appendExportEmptyToDts } from '../../build/dts-plugins'
export default defineConfig({ pack: { entry: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.d.ts'], tsconfig: 'tsconfig.json', unbundle: true, format: ['esm'], dts: true, plugins: [appendExportEmptyToDts], outExtensions: () => ({ js: '.js', dts: '.d.ts' }), }, test: { globals: true,2 unmodified linesadded:build/dts-plugins.ts+55 added
1+import { parse } from 'rolldown/utils'2+import type { TsdownPluginOption } from 'vite-plus/pack'3+4+const DECLARATION_TYPES = new Set([5+ 'TSTypeAliasDeclaration',6+ 'TSInterfaceDeclaration',7+ 'TSDeclareFunction',8+ 'TSEnumDeclaration',9+ 'TSModuleDeclaration',10+ 'VariableDeclaration',11+ 'ClassDeclaration',12+ 'FunctionDeclaration',13+])14+15+/**16+ * Append `export {}` to an emitted `.d.ts` module that has top-level declarations without `export`,17+ * exactly as tsc does when it emits declaration files.18+ *19+ * Without it TypeScript treats every top-level declaration of a `.d.ts` module as exported, so20+ * consumers' declaration emit may reference internal aliases through `<pkg>/dist/...`, which is21+ * not reachable via the package exports (TS2883). The bundled dts emit does not add it.22+ */23+export const appendExportEmptyToDts: TsdownPluginOption = {24+ name: 'hono:append-export-empty-to-dts',25+ async renderChunk(code, chunk) {26+ if (!chunk.fileName.endsWith('.d.ts')) {27+ return28+ }29+30+ const { body } = (await parse(chunk.fileName, code)).program31+32+ let isModule = false33+ let hasPrivateDeclaration = false34+ for (const statement of body) {35+ if (statement.type === 'ImportDeclaration' || statement.type.startsWith('Export')) {36+ isModule = true37+ // `export {}` or `export = x` already turns the implicit export off38+ if (39+ (statement.type === 'ExportNamedDeclaration' &&40+ !statement.declaration &&41+ statement.specifiers.length === 0) ||42+ statement.type === 'TSExportAssignment'43+ ) {44+ return45+ }46+ } else if (DECLARATION_TYPES.has(statement.type)) {47+ hasPrivateDeclaration = true48+ }49+ }50+51+ if (isModule && hasPrivateDeclaration) {52+ return `${code}\nexport {};\n`53+ }54+ },55+}modified:src/jsx/base.ts+4 added-5 removed
10 unmodified lines11 } from './context'12 import type { Context } from './context'13 import { domRenderers } from './intrinsic-element/common'14 import * as intrinsicElementTags from './intrinsic-element/components'15-import type {16- JSX as HonoJSX,17- IntrinsicElements as IntrinsicElementsDefined,18-} from './intrinsic-elements'15+import type { JSX as HonoJSX } from './intrinsic-elements'16 import {17 isValidAttributeName,18 isValidTagName,19 normalizeIntrinsicElementKey,20 unmodified lines40 export type ElementType = string | ((props: never) => FunctionComponentResult)41 export interface ElementChildrenAttribute {42 children: Child43 }44+ // `HonoJSX.IntrinsicElements`, not the top-level alias: the bundled dts emit renames the namespace45+ // import correctly but drops an import alias that collides with this member name (TS2310).47- export interface IntrinsicElements extends IntrinsicElementsDefined {46+ export interface IntrinsicElements extends HonoJSX.IntrinsicElements {47 [tagName: string]: Props48 }49 export interface IntrinsicAttributes {50 key?: string | number | bigint | null | undefined439 unmodified linesmodified:vite.config.ts+2 added-1 removed
2 unmodified lines3 import { defineConfig } from 'vite-plus'4 import type { TsdownPluginOption } from 'vite-plus/pack'5 import oxfmtConfig from './.oxfmtrc.json' with { type: 'json' }6 import oxlintConfig from './.oxlintrc.json' with { type: 'json' }7+import { appendExportEmptyToDts } from './build/dts-plugins'8 9 const entry = ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.test.tsx']10 const common = {11 entry,100 unmodified lines112 ...common,113 format: ['esm'],114 outDir: 'dist/types',115 dts: { emitDtsOnly: true },115- plugins: [removeDtsPrivateFieldsPlugin],116+ plugins: [removeDtsPrivateFieldsPlugin, appendExportEmptyToDts],117 outExtensions: () => ({ dts: '.d.ts' }),118 },119 ],120 })