Convert default exports to named exports in your TypeScript codebase
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.
Here’s how to convert default exports to named exports:
Copy
Ask AI
for file in codebase.files: target_file = file.filepath if not target_file: print(f"⚠️ Target file not found: {filepath}") continue # Get corresponding non-shared file non_shared_path = target_file.filepath.replace('/shared/', '/') if not codebase.has_file(non_shared_path): print(f"⚠️ No matching non-shared file for: {filepath}") continue non_shared_file = codebase.get_file(non_shared_path) print(f"📄 Processing {target_file.filepath}") # Process individual exports for export in target_file.exports: # Handle default exports if export.is_reexport() and export.is_default_export(): print(f" 🔄 Converting default export '{export.name}'") default_export = next((e for e in non_shared_file.default_exports), None) if default_export: default_export.make_non_default() print(f"✨ Fixed exports in {target_file.filepath}")
Here’s the complete codemod that you can copy and use directly:
Copy
Ask AI
for file in codebase.files: target_file = file.filepath if not target_file: print(f"⚠️ Target file not found: {filepath}") continue # Get corresponding non-shared file non_shared_path = target_file.filepath.replace('/shared/', '/') if not codebase.has_file(non_shared_path): print(f"⚠️ No matching non-shared file for: {filepath}") continue non_shared_file = codebase.get_file(non_shared_path) print(f"📄 Processing {target_file.filepath}") # Process individual exports for export in target_file.exports: # Handle default exports if export.is_reexport() and export.is_default_export(): print(f" 🔄 Converting default export '{export.name}'") default_export = next((e for e in non_shared_file.default_exports), None) if default_export: default_export.make_non_default() print(f"✨ Fixed exports in {target_file.filepath}")