Convert default exports to named exports in your TypeScript codebase
Graph-sitter 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}")