Skip to content

Latest commit

 

History

History
124 lines (84 loc) · 3.5 KB

File metadata and controls

124 lines (84 loc) · 3.5 KB

Transform Module Re-exports Organization

This example demonstrates how to use Codegen to automatically analyze and reorganize TypeScript module re-exports through shared directories. The script makes this process simple by handling all the tedious manual updates automatically.

Note

This codemod helps maintain clean module boundaries and improves code organization by centralizing shared exports.

How the Migration Script Works

The script automates the entire reorganization process in a few key steps:

  1. Export Analysis

    for export_stmt in file.export_statements:
        for export in export_stmt.exports:
            if export.is_reexport() and not export.is_external_export:
                all_reexports.append(export)
    • Automatically identifies re-exports in shared directories
    • Analyzes export patterns and dependencies
    • Uses Codegen's intelligent code analysis engine
  2. Shared File Management

    resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/")
    if not codebase.has_file(resolved_public_file):
        target_file = codebase.create_file(resolved_public_file, sync=True)
    • Creates or updates shared export files
    • Maintains proper file structure
    • Handles path resolution automatically
  3. Import Updates

    # Updates imports to use new shared paths
    new_path = usage.file.ts_config.translate_import_path(resolved_public_file)
    new_import = f'import {{ {name} }} from "{new_path}"'
    • Updates all import statements to use new paths
    • Maintains proper TypeScript path resolution
    • Handles different import types (normal, type)

Why This Makes Organization Easy

  1. Zero Manual Updates

    • Codegen SDK handles all file creation and updates
    • No tedious export management
  2. Consistent Structure

    • Ensures all shared exports follow the same pattern
    • Maintains clean module boundaries
  3. Safe Transformations

    • Validates changes before applying them
    • Preserves existing functionality

Common Re-export Patterns

Module to Shared Exports

// Before: Direct module import
import { validateEmail } from '../module_a/src/functions';

// After: Import through shared
import { validateEmail } from '../module_a/src/shared';

Export Consolidation

// Before: Multiple export files
export { foo } from './foo';
export { bar } from './bar';

// After: Consolidated in shared
export * from '../functions';

Key Benefits to Note

  1. Better Module Boundaries

    • Clear public API for each module
    • Centralized shared functionality
  2. Improved Maintainability

    • Easier to track dependencies
    • Simplified import paths
  3. Code Organization

    • Consistent export structure
    • Reduced import complexity

The script will:

  1. 🎯 Start the reexport organization
  2. 📁 Analyze shared directories
  3. 🔄 Process and update exports
  4. ✨ Create shared export files
  5. 🧹 Clean up redundant exports

Learn More

Contributing

Feel free to submit issues and enhancement requests!