Skip to content

docs: fixed up export docs #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2025
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
109 changes: 69 additions & 40 deletions docs/building-with-codegen/exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ iconType: "solid"

The [Export](/api-reference/core/Export) API provides tools for managing exports and module boundaries in TypeScript codebases.

<Note>Exports are a TS-only language feature</Note>

## Export Statements vs Exports

Similar to imports, Codegen provides two levels of abstraction for working with exports:
Expand All @@ -30,19 +32,76 @@ export function process() {}
You can access these through your file's collections:

```python
# Access all exports in the codebase
for export in codebase.exports:
...

# Access all export statements
for stmt in file.export_statements:
print(f"Statement: {stmt.source}")

# Access individual exports in the statement
for exp in stmt.exports:
print(f" Export: {exp.name}")
...
```

<Note>
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.
</Note>

## Common Operations

Here are common operations for working with exports:

```python
# Add exports from source code
file.add_export_from_source("export { MyComponent };")
file.add_export_from_source("export type { MyType } from './types';")

# Export existing symbols
component = file.get_function("MyComponent")
file.add_export(component) # export { MyComponent }
file.add_export(component, alias="default") # export { MyComponent as default }

# Convert to type export
export = file.get_export("MyType")
export.make_type_export()

# Remove exports
export = file.get_export("MyComponent")
export.remove() # Removes export but keeps the symbol

# Remove multiple exports
for export in file.exports:
if not export.is_type_export():
export.remove()

# Update export properties
export.update(
name="NewName",
is_type=True,
is_default=False
)

# Export from another file
other_file = codebase.get_file("./components.ts")
component = other_file.get_class("Button")
file.add_export(component, from_file=other_file) # export { Button } from './components';

# Analyze symbols being exported
for export in file.exports:
if isinstance(export.exported_symbol, ExternalModule):
print('Exporting ExternalModule')
else:
...
```

<Note>
When adding exports, you can:
- Add from source code with `add_export_from_source()`
- Export existing symbols with `add_export()`
- Re-export from other files by specifying `from_file`

The export will automatically handle adding any required imports.
</Note>

## Export Types

Codegen supports several types of exports:
Expand All @@ -68,9 +127,13 @@ export { foo as default }; // Default export alias
export { bar as baz } from './other-file'; // Re-export with alias
```

## Working with Exports
## Identifying Export Types

The Export API provides methods to identify and filter exports:
- [.is_type_export()](/api-reference/typescript/TSExport#is-type-export)
- [.is_default_export()](/api-reference/typescript/TSExport#is-default-export)
- [.is_wildcard_export()](/api-reference/typescript/TSExport#is-wildcard-export)


```python
# Check export types
Expand All @@ -81,14 +144,6 @@ for exp in file.exports:
print(f"Default export: {exp.name}")
elif exp.is_wildcard_export():
print(f"Wildcard export from: {exp.from_file.filepath}")

# Work with re-exports
for exp in file.exports:
if exp.is_reexport():
if exp.is_external_export:
print(f"External re-export: {exp.name} from {exp.from_file.filepath}")
else:
print(f"Internal re-export: {exp.name}")
```

## Export Resolution
Expand All @@ -106,35 +161,9 @@ for exp in file.exports:
print(f"Through: {' -> '.join(e.file.filepath for e in exp.export_chain)}")
```

## Common Operations

Here are common operations for working with exports:

```python
# Add new export
file.add_export("MyComponent")

# Add export with alias
file.add_export("MyComponent", alias="default")

# Convert to type export
export = file.get_export("MyType")
export.make_type_export()

# Remove export
export.remove() # Removes export but keeps symbol

# Update export properties
export.update(
name="NewName",
is_type=True,
is_default=False
)
```

## Managing Re-exports

Common patterns for working with re-exports:
You can manage re-exports with the [TSExport.is_reexport()](/api-reference/typescript/TSExport#is-reexport) API:

```python
# Create public API
Expand Down
Loading
Loading