|
1 | | - |
| 1 | +--- |
| 2 | +title: "The Export API" |
| 3 | +sidebarTitle: "Exports" |
| 4 | +icon: "file-export" |
| 5 | +iconType: "solid" |
| 6 | +--- |
| 7 | + |
| 8 | +The [Export](/api-reference/core/Export) API provides tools for managing exports and module boundaries in TypeScript codebases. |
| 9 | + |
| 10 | +## Export Statements vs Exports |
| 11 | + |
| 12 | +Similar to imports, Codegen provides two levels of abstraction for working with exports: |
| 13 | + |
| 14 | +- [ExportStatement](/api-reference/core/ExportStatement) - Represents a complete export statement |
| 15 | +- [Export](/api-reference/core/Export) - Represents individual exported symbols |
| 16 | + |
| 17 | +```typescript |
| 18 | +// One ExportStatement containing multiple Export objects |
| 19 | +export { foo, bar as default, type User }; |
| 20 | +// Creates: |
| 21 | +// - Export for 'foo' |
| 22 | +// - Export for 'bar' as default |
| 23 | +// - Export for 'User' as a type |
| 24 | + |
| 25 | +// Direct exports create one ExportStatement per export |
| 26 | +export const value = 42; |
| 27 | +export function process() {} |
| 28 | +``` |
| 29 | + |
| 30 | +You can access these through your file's collections: |
| 31 | + |
| 32 | +```python |
| 33 | +# Access all export statements |
| 34 | +for stmt in file.export_statements: |
| 35 | + print(f"Statement: {stmt.source}") |
| 36 | + |
| 37 | + # Access individual exports in the statement |
| 38 | + for exp in stmt.exports: |
| 39 | + print(f" Export: {exp.name}") |
| 40 | +``` |
| 41 | + |
| 42 | +<Note> |
| 43 | +ExportStatement inherits from [Statement](/building-with-codegen/statements-and-code-blocks), providing operations like `remove()` and `insert_before()`. This is particularly useful when you want to manipulate the entire export declaration. |
| 44 | +</Note> |
| 45 | + |
| 46 | +## Export Types |
| 47 | + |
| 48 | +Codegen supports several types of exports: |
| 49 | + |
| 50 | +```typescript |
| 51 | +// Direct exports |
| 52 | +export const value = 42; // Value export |
| 53 | +export function myFunction() {} // Function export |
| 54 | +export class MyClass {} // Class export |
| 55 | +export type MyType = string; // Type export |
| 56 | +export interface MyInterface {} // Interface export |
| 57 | +export enum MyEnum {} // Enum export |
| 58 | + |
| 59 | +// Re-exports |
| 60 | +export { foo, bar } from './other-file'; // Named re-exports |
| 61 | +export type { Type } from './other-file'; // Type re-exports |
| 62 | +export * from './other-file'; // Wildcard re-exports |
| 63 | +export * as utils from './other-file'; // Namespace re-exports |
| 64 | + |
| 65 | +// Aliased exports |
| 66 | +export { foo as foop }; // Basic alias |
| 67 | +export { foo as default }; // Default export alias |
| 68 | +export { bar as baz } from './other-file'; // Re-export with alias |
| 69 | +``` |
| 70 | + |
| 71 | +## Working with Exports |
| 72 | + |
| 73 | +The Export API provides methods to identify and filter exports: |
| 74 | + |
| 75 | +```python |
| 76 | +# Check export types |
| 77 | +for exp in file.exports: |
| 78 | + if exp.is_type_export(): |
| 79 | + print(f"Type export: {exp.name}") |
| 80 | + elif exp.is_default_export(): |
| 81 | + print(f"Default export: {exp.name}") |
| 82 | + elif exp.is_wildcard_export(): |
| 83 | + print(f"Wildcard export from: {exp.from_file.filepath}") |
| 84 | + |
| 85 | +# Work with re-exports |
| 86 | +for exp in file.exports: |
| 87 | + if exp.is_reexport(): |
| 88 | + if exp.is_external_export: |
| 89 | + print(f"External re-export: {exp.name} from {exp.from_file.filepath}") |
| 90 | + else: |
| 91 | + print(f"Internal re-export: {exp.name}") |
| 92 | +``` |
| 93 | + |
| 94 | +## Export Resolution |
| 95 | + |
| 96 | +You can trace exports to their original symbols: |
| 97 | + |
| 98 | +```python |
| 99 | +for exp in file.exports: |
| 100 | + if exp.is_reexport(): |
| 101 | + # Get original and current symbols |
| 102 | + current = exp.exported_symbol |
| 103 | + original = exp.resolved_symbol |
| 104 | + |
| 105 | + print(f"Re-exporting {original.name} from {exp.from_file.filepath}") |
| 106 | + print(f"Through: {' -> '.join(e.file.filepath for e in exp.export_chain)}") |
| 107 | +``` |
| 108 | + |
| 109 | +## Common Operations |
| 110 | + |
| 111 | +Here are common operations for working with exports: |
| 112 | + |
| 113 | +```python |
| 114 | +# Add new export |
| 115 | +file.add_export("MyComponent") |
| 116 | + |
| 117 | +# Add export with alias |
| 118 | +file.add_export("MyComponent", alias="default") |
| 119 | + |
| 120 | +# Convert to type export |
| 121 | +export = file.get_export("MyType") |
| 122 | +export.make_type_export() |
| 123 | + |
| 124 | +# Remove export |
| 125 | +export.remove() # Removes export but keeps symbol |
| 126 | + |
| 127 | +# Update export properties |
| 128 | +export.update( |
| 129 | + name="NewName", |
| 130 | + is_type=True, |
| 131 | + is_default=False |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +## Managing Re-exports |
| 136 | + |
| 137 | +Common patterns for working with re-exports: |
| 138 | + |
| 139 | +```python |
| 140 | +# Create public API |
| 141 | +index_file = codebase.get_file("index.ts") |
| 142 | + |
| 143 | +# Re-export from internal files |
| 144 | +for internal_file in codebase.files: |
| 145 | + if internal_file.name != "index": |
| 146 | + for symbol in internal_file.symbols: |
| 147 | + if symbol.is_public: |
| 148 | + index_file.add_export( |
| 149 | + symbol, |
| 150 | + from_file=internal_file |
| 151 | + ) |
| 152 | + |
| 153 | +# Convert default to named exports |
| 154 | +for exp in file.exports: |
| 155 | + if exp.is_default_export(): |
| 156 | + exp.make_named_export() |
| 157 | + |
| 158 | +# Consolidate re-exports |
| 159 | +from collections import defaultdict |
| 160 | + |
| 161 | +file_exports = defaultdict(list) |
| 162 | +for exp in file.exports: |
| 163 | + if exp.is_reexport(): |
| 164 | + file_exports[exp.from_file].append(exp) |
| 165 | + |
| 166 | +for from_file, exports in file_exports.items(): |
| 167 | + if len(exports) > 1: |
| 168 | + # Create consolidated re-export |
| 169 | + names = [exp.name for exp in exports] |
| 170 | + file.add_export_from_source( |
| 171 | + f"export {{ {', '.join(names)} }} from '{from_file.filepath}'" |
| 172 | + ) |
| 173 | + # Remove individual exports |
| 174 | + for exp in exports: |
| 175 | + exp.remove() |
| 176 | +``` |
| 177 | + |
| 178 | +<Note> |
| 179 | +When managing exports, consider the impact on your module's public API. Not all symbols that can be exported should be exported. |
| 180 | +</Note> |
0 commit comments