# hezhuoyi/babel-plugin-rn-remove-unused-code architecture

> Architecture diagram of the GitHub repository hezhuoyi/babel-plugin-rn-remove-unused-code, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hezhuoyi/babel-plugin-rn-remove-unused-code
- Repository: https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code

## Overview

##### Project type and purpose
- This is an open-source Babel plugin (babel-plugin-rn-remove-unused-code) written in TypeScript.
- Its purpose is to integrate into a React Native build (via Babel) and eliminate unused imports, styles, and PropTypes from JSX/JavaScript source files.

##### File structure
- Top level:
- babel.config.js – example of how the plugin is applied
- tsconfig.json, package.json – build/packaging
- README.md – usage and options
- Source code under babel-project/:
- index.ts – main plugin entrypoint (defines visitor methods for AST traversal)
- util.ts – helper functions for detecting and removing nodes
- types.d.ts – plugin options type definitions
- Build artifacts:
- The TS in babel-project is compiled (via tsc) to a dist/ folder when published.

##### README for additional insights
- Describes plugin options (imports.styles.propTypes), customization hooks, and example input/output.
- Reveals integration point (specified in a user’s babel.config.js).

##### Components
a. Input code module (JSX/JS files from React Native project)
b. Babel core (parser + traverse + generator)
c. Our plugin module:
– index.ts (visitor functions for ImportDeclaration, StyleSheet.create, ClassProperty(propTypes))
– util.ts (helpers to decide what to remove)
– types.d.ts (options interface)
d. Configuration loader (reads user’s plugin options from babel.config.js)
e. Output code module (transformed code)
f. Build pipeline (TypeScript compiler → dist/ folder → NPM publish)

##### Relationships and interactions
- User’s source code → fed into Babel parser
- Configuration loader injects plugin options into Babel
- Babel traverse invokes plugin visitor methods (index.ts)
- Visitors call util.ts functions to mark or remove AST nodes
- Modified AST is passed to Babel generator → output code
- The TypeScript compiler transforms TS sources into a distributable JS package

##### Architectural patterns and principles
- Plugin pattern (Babel’s visitor API)
- AST transformation pipeline
- Separation of concerns: index.ts (core logic) vs util.ts (helpers) vs types.d.ts (contracts)
- Build-step isolation: TS→JS compile before publish

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Development Time
    subgraph "Development Time"
        tsConfig["tsconfig.json"]:::config
        TSCompiler["TypeScript Compiler"]:::build
        dist["dist/ folder"]:::build
        pkgManifest["package.json"]:::config
        readme["README.md"]:::config
        tsConfig -->|"configures"| TSCompiler
        TSCompiler -->|"compiles TS→JS"| dist
        dist -->|"packaged by"| pkgManifest
        pkgManifest -->|"documents usage"| readme
    end

    %% Runtime Transformation
    subgraph "Runtime Transformation"
        src["React Native Source Code (JSX/JS)"]:::external
        configLoader["babel.config.js"]:::config
        parser["Babel Parser"]:::external
        traverse["Babel Traverse"]:::external
        pluginIndex["index.ts"]:::plugin
        util["util.ts"]:::plugin
        typesDef["types.d.ts"]:::plugin
        generator["Babel Generator"]:::external
        output["Transformed Code"]:::external

        src -->|"code"| parser
        parser -->|"AST"| traverse
        configLoader -->|"plugin options"| traverse
        traverse -->|"visits"| pluginIndex
        pluginIndex -->|"calls helpers"| util
        pluginIndex -->|"uses types"| typesDef
        traverse -->|"modified AST"| generator
        generator -->|"code"| output
    end

    %% Click Events
    click pluginIndex "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/babel-project/index.ts"
    click util "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/babel-project/util.ts"
    click typesDef "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/babel-project/types.d.ts"
    click configLoader "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/babel.config.js"
    click tsConfig "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/tsconfig.json"
    click pkgManifest "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/package.json"
    click readme "https://github.com/hezhuoyi/babel-plugin-rn-remove-unused-code/blob/master/README.md"

    %% Styles
    classDef plugin fill:#D6EAF8,stroke:#2E86C1,color:#1B4F72
    classDef external fill:#D5F5E3,stroke:#27AE60,color:#145A32
    classDef config fill:#FADBD8,stroke:#E74C3C,color:#641E16
    classDef build fill:#FCF3CF,stroke:#F1C40F,color:#7D6608
```
