|
1 | 1 | import { parse } from '@babel/parser' |
2 | 2 | import _generate from '@babel/generator' |
| 3 | +import * as t from '@babel/types' |
| 4 | +import { |
| 5 | + deadCodeElimination as _deadCodeElimination, |
| 6 | + findReferencedIdentifiers, |
| 7 | +} from 'babel-dead-code-elimination' |
3 | 8 | import type { GeneratorOptions, GeneratorResult } from '@babel/generator' |
4 | 9 | import type { ParseResult, ParserOptions } from '@babel/parser' |
5 | 10 | import type * as _babel_types from '@babel/types' |
@@ -36,3 +41,119 @@ export function generateFromAst( |
36 | 41 | ) |
37 | 42 | } |
38 | 43 | export type { GeneratorResult } from '@babel/generator' |
| 44 | + |
| 45 | +/** |
| 46 | + * Strips TypeScript type-only exports and imports from an AST. |
| 47 | + * |
| 48 | + * This is necessary because babel-dead-code-elimination doesn't handle |
| 49 | + * TypeScript type exports/imports. When a type export references an import |
| 50 | + * that pulls in server-only code, the dead code elimination won't remove |
| 51 | + * that import because it sees the type as still referencing it. |
| 52 | + * |
| 53 | + * This function removes: |
| 54 | + * - `export type Foo = ...` |
| 55 | + * - `export interface Foo { ... }` |
| 56 | + * - `export type { Foo } from './module'` |
| 57 | + * - Type specifiers in mixed exports: `export { value, type Foo }` -> `export { value }` |
| 58 | + * - `import type { Foo } from './module'` |
| 59 | + * - Type specifiers in mixed imports: `import { value, type Foo } from './module'` -> `import { value }` |
| 60 | + * - Top-level `type Foo = ...` declarations (non-exported) |
| 61 | + * - Top-level `interface Foo { ... }` declarations (non-exported) |
| 62 | + * |
| 63 | + * @param ast - The Babel AST (or ParseResult) to mutate |
| 64 | + */ |
| 65 | +export function stripTypeExports(ast: ParseResult<_babel_types.File>): void { |
| 66 | + // Filter the program body to remove type-only nodes |
| 67 | + ast.program.body = ast.program.body.filter((node) => { |
| 68 | + // Remove top-level type alias declarations: `type Foo = string` |
| 69 | + if (t.isTSTypeAliasDeclaration(node)) { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + // Remove top-level interface declarations: `interface Foo { ... }` |
| 74 | + if (t.isTSInterfaceDeclaration(node)) { |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + // Handle export declarations |
| 79 | + if (t.isExportNamedDeclaration(node)) { |
| 80 | + // Remove entire export if it's a type-only export |
| 81 | + // e.g., `export type Foo = string`, `export interface Bar {}`, `export type { X } from './y'` |
| 82 | + if (node.exportKind === 'type') { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + // For value exports with mixed specifiers, filter out type-only specifiers |
| 87 | + // e.g., `export { value, type TypeOnly }` -> `export { value }` |
| 88 | + if (node.specifiers.length > 0) { |
| 89 | + node.specifiers = node.specifiers.filter((specifier) => { |
| 90 | + if (t.isExportSpecifier(specifier)) { |
| 91 | + return specifier.exportKind !== 'type' |
| 92 | + } |
| 93 | + return true |
| 94 | + }) |
| 95 | + |
| 96 | + // If all specifiers were removed, remove the entire export declaration |
| 97 | + // (unless it has a declaration like `export const x = 1`) |
| 98 | + if (node.specifiers.length === 0 && !node.declaration) { |
| 99 | + return false |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Handle import declarations |
| 105 | + if (t.isImportDeclaration(node)) { |
| 106 | + // Remove entire import if it's a type-only import |
| 107 | + // e.g., `import type { Foo } from './module'` |
| 108 | + if (node.importKind === 'type') { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + // For value imports with mixed specifiers, filter out type-only specifiers |
| 113 | + // e.g., `import { value, type TypeOnly } from './module'` -> `import { value }` |
| 114 | + if (node.specifiers.length > 0) { |
| 115 | + node.specifiers = node.specifiers.filter((specifier) => { |
| 116 | + if (t.isImportSpecifier(specifier)) { |
| 117 | + return specifier.importKind !== 'type' |
| 118 | + } |
| 119 | + return true |
| 120 | + }) |
| 121 | + |
| 122 | + // If all specifiers were removed, remove the entire import declaration |
| 123 | + if (node.specifiers.length === 0) { |
| 124 | + return false |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return true |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +// Re-export findReferencedIdentifiers from babel-dead-code-elimination |
| 134 | +export { findReferencedIdentifiers } |
| 135 | + |
| 136 | +/** |
| 137 | + * Performs dead code elimination on the AST, with TypeScript type stripping. |
| 138 | + * |
| 139 | + * This is a wrapper around babel-dead-code-elimination that first strips |
| 140 | + * TypeScript type-only exports and imports. This is necessary because |
| 141 | + * babel-dead-code-elimination doesn't handle type exports, which can cause |
| 142 | + * imports to be retained when they're only referenced by type exports. |
| 143 | + * |
| 144 | + * @param ast - The Babel AST to mutate |
| 145 | + * @param candidates - Optional set of identifier paths to consider for removal. |
| 146 | + * If provided, only these identifiers will be candidates for removal. |
| 147 | + * This should be the result of `findReferencedIdentifiers(ast)` called |
| 148 | + * before any AST transformations. |
| 149 | + */ |
| 150 | +export function deadCodeElimination( |
| 151 | + ast: ParseResult<_babel_types.File>, |
| 152 | + candidates?: Set<any>, |
| 153 | +): void { |
| 154 | + // First strip TypeScript type-only exports and imports |
| 155 | + stripTypeExports(ast) |
| 156 | + |
| 157 | + // Then run the original dead code elimination |
| 158 | + _deadCodeElimination(ast, candidates) |
| 159 | +} |
0 commit comments