Skip to content
Myr

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

11 files+77 added−6 removed

modified:adapters/aws-lambda/vite.config.ts+2 added
modified:adapters/bun/vite.config.ts+2 added
modified:adapters/cloudflare-workers/vite.config.ts+2 added
modified:adapters/deno/vite.config.ts+2 added
modified:adapters/lambda-edge/vite.config.ts+2 added
modified:adapters/netlify/vite.config.ts+2 added
modified:adapters/service-worker/vite.config.ts+2 added
modified:adapters/vercel/vite.config.ts+2 added
added:build/dts-plugins.ts+55 added
1import { parse } from 'rolldown/utils'2import type { TsdownPluginOption } from 'vite-plus/pack'34const DECLARATION_TYPES = new Set([5 'TSTypeAliasDeclaration',6 'TSInterfaceDeclaration',7 'TSDeclareFunction',8 'TSEnumDeclaration',9 'TSModuleDeclaration',10 'VariableDeclaration',11 'ClassDeclaration',12 'FunctionDeclaration',13])1415/**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 */23export 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 }2930 const { body } = (await parse(chunk.fileName, code)).program3132 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 }5051 if (isModule && hasPrivateDeclaration) {52 return `${code}\nexport {};\n`53 }54 },55}
modified:src/jsx/base.ts+4 added-5 removed
10 unmodified lines
11} from './context'12import type { Context } from './context'13import { domRenderers } from './intrinsic-element/common'14import * as intrinsicElementTags from './intrinsic-element/components'15import type {16 JSX as HonoJSX,17 IntrinsicElements as IntrinsicElementsDefined,18} from './intrinsic-elements'15import type { JSX as HonoJSX } from './intrinsic-elements'16import {17 isValidAttributeName,18 isValidTagName,19 normalizeIntrinsicElementKey,
20 unmodified lines
40 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 | undefined
439 unmodified lines
modified:vite.config.ts+2 added-1 removed
2 unmodified lines
3import { defineConfig } from 'vite-plus'4import type { TsdownPluginOption } from 'vite-plus/pack'5import oxfmtConfig from './.oxfmtrc.json' with { type: 'json' }6import oxlintConfig from './.oxlintrc.json' with { type: 'json' }7import { appendExportEmptyToDts } from './build/dts-plugins'89const entry = ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.test.tsx']10const common = {11 entry,
100 unmodified lines
112 ...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})