Skip to main content
Codegen provides tools to help you migrate away from default exports to named exports in your TypeScript codebase. This tutorial builds on the concepts covered in exports to show you how to automate this conversion process.

Overview

Default exports can make code harder to maintain and refactor. Converting them to named exports provides several benefits:
  • Better IDE support for imports and refactoring
  • More explicit and consistent import statements
  • Easier to track symbol usage across the codebase

Converting Default Exports

Here’s how to convert default exports to named exports:

Understanding the Process

Let’s break down how this works:
The code identifies default exports by checking:
  1. If it’s a re-export (is_reexport())
  2. If it’s a default export (is_default_export())
For each default export:
  1. Find the corresponding export in the non-shared file
  2. Convert it to a named export using make_non_default()
The code:
  1. Maps shared files to their non-shared counterparts
  2. Verifies the non-shared file exists
  3. Loads the non-shared file for processing

Best Practices

  1. Check for Missing Files: Always verify files exist before processing:
  1. Log Progress: Add logging to track the conversion process:
  1. Handle Missing Exports: Check that default exports exist before converting:

Next Steps

After converting default exports:
  1. Run your test suite to verify everything still works
  2. Update any import statements that were using default imports
  3. Review the changes to ensure all exports were converted correctly
  4. Consider adding ESLint rules to prevent new default exports
Remember to test thoroughly after converting default exports, as this change affects how other files import the converted modules.

Complete Codemod

Here’s the complete codemod that you can copy and use directly: