Codegen’s export management tools provide a powerful way to reorganize and manage exports in your TypeScript codebase. This guide will walk you through the process of automating export management.

Common use cases include:

The codemod automatically handles dependencies and import updates, so you don’t need to manage them manually.

Import Management

There are two main aspects to managing imports: updating references and cleaning up old imports.

Handling Export Statements

When working with export statements, you need to handle both existing and new statements:

Best Practices

  1. Check for Wildcards First: Always check for existing wildcard exports before adding new ones:
if target_file.has_export_statement_for_path(relative_path, "WILDCARD"):
    has_wildcard = True
    continue
  1. Handle Path Translations: Use the TypeScript config for path translations:
new_path = usage.file.ts_config.translate_import_path(resolved_public_file)
  1. Clean Up Empty Files: Remove files that no longer contain exports or symbols:
if not file.export_statements and len(file.symbols) == 0:
    file.remove()

Complete Example

Here’s a complete example that puts all these concepts together:

Remember to test your changes incrementally and commit after major modifications to keep the codebase graph up-to-date.

Next Steps

After implementing these export management strategies:

  1. Run your test suite to verify everything still works

  2. Review the generated import statements

  3. Check for any empty files that should be removed

  4. Verify that all export types (wildcard, type, named) are working as expected

Remember that managing exports is an iterative process - you may need to run the codemod multiple times as your codebase evolves.

Was this page helpful?