# george-yg9ea/figma-export-images-with-metadata architecture

> Architecture diagram of the GitHub repository george-yg9ea/figma-export-images-with-metadata, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/george-yg9ea/figma-export-images-with-metadata
- Repository: https://github.com/george-yg9ea/figma-export-images-with-metadata

## Overview

1. **Project shape**
   - This is a **Figma plugin**, not a standalone app.
   - The repository is split into a **plugin main thread** (`src/code.ts`) and a **UI layer** (`src/ui.ts`, `src/ui.html`), with a small build script and metadata parsers.

2. **Top-level runtime boundary**
   - The plugin runs inside Figma’s plugin sandbox.
   - The UI runs in the plugin’s embedded iframe.
   - These two halves communicate through the standard Figma plugin message channel, which is the main architectural boundary in the repo.

3. **Main controller: `src/code.ts`**
   - This is the orchestrator for the plugin’s behavior.
   - It handles selection context, reads the selected node’s image data, triggers JPEG export from Figma, and coordinates the metadata-preservation workflow.
   - It is the place where Figma API calls and binary image processing come together.

4. **UI subsystem: `src/ui.html` + `src/ui.ts`**
   - The UI is minimal and action-oriented: it presents the export control surface to the user.
   - `ui.html` provides the embedded document shell, while `ui.ts` supplies client-side behavior and message handling.
   - The UI does not own the export logic; it only initiates user actions and displays plugin state.

5. **Image export pipeline**
   - The workflow starts from a selected Figma node that has an **IMAGE fill**.
   - Figma renders the cropped/resized result as a new JPEG.
   - In parallel, the plugin retrieves the original image bytes associated with the image hash so metadata can be preserved instead of lost during rendering.

6. **Metadata parsing layer**
   - `src/exif-parser.ts` handles JPEG metadata structures for **EXIF/XMP/ICC-style APP segments**.
   - `src/iptc-parser.ts` handles **IPTC** data in JPEG APP13 segments.
   - These modules are focused on reading and preserving existing metadata blocks rather than interpreting image pixels.

7. **Metadata preservation strategy**
   - The plugin merges metadata from the original source JPEG into the newly rendered export.
   - Per the README, it preserves **APP1 (EXIF/XMP), APP2 (ICC), APP13 (IPTC), and COM** while keeping the rendered file’s **JFIF** segment.
   - This is a byte-level JPEG segment composition problem, not a bitmap re-encode problem.

8. **Data flow across the system**
   - User selects node → UI triggers export → main thread exports rendered JPEG → main thread fetches original bytes → parsers extract metadata segments → metadata is merged into exported JPEG → final file is downloaded.
   - The important architectural point is that the **rendered image and metadata source are separate inputs** to the final artifact.

9. **Tooling and build boundary**
   - `scripts/build.mjs` is the packaging step that turns the TypeScript sources into a Figma-loadable plugin bundle.
   - `package.json`, `tsconfig.json`, and `manifest.json` define the plugin runtime setup, compilation targets, and Figma entry points.
   - The build output is meant for manual loading into Figma during development.

10. **Repository constraints that affect architecture**
   - The implementation is currently centered on **JPEG export only**.
   - It depends on Figma exposing the original image bytes for the selected asset; if Figma returns a recompressed or metadata-stripped asset, preservation is limited.
   - The repo’s design reflects that constraint: it is a **binary-preservation plugin** with a narrow, JPEG-specific scope rather than a generic image pipeline.

11. **What to draw in an architecture diagram**
   - Show Figma as both the host environment and the source of the selected node/image data.
   - Show two internal plugin zones: **UI** and **main plugin logic**.
   - Show the main logic depending on **JPEG metadata parsers** and on **Figma export APIs**.
   - Show a final output path to a downloaded JPEG file, with metadata merged back in.

Main entry points: [`src/code.ts`](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/code.ts) (Main thread), [`scripts/build.mjs`](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/scripts/build.mjs) (Build script), [`src/ui.html`](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.html) (UI shell), [`src/ui.ts`](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.ts) (UI logic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Figma host"]
  node_figma_host["Figma host<br/>runtime"]
  node_selected_image(("Selected image<br/>figma node"))
end

subgraph group_plugin["Plugin runtime"]
  node_plugin_main["Main thread<br/>controller<br/>[code.ts]"]
  node_message_bridge{{"Message bridge<br/>ipc"}}
  node_ui_shell["UI shell<br/>document<br/>[ui.html]"]
  node_ui_logic["UI logic<br/>view logic<br/>[ui.ts]"]
end

subgraph group_processing["JPEG pipeline"]
  node_export_render["Rendered JPEG<br/>export step"]
  node_original_bytes[("Source bytes<br/>asset bytes")]
  node_exif_parser["EXIF/XMP parser<br/>jpeg parser<br/>[exif-parser.ts]"]
  node_iptc_parser["IPTC parser<br/>jpeg parser<br/>[iptc-parser.ts]"]
  node_segment_merge{{"Segment merge<br/>jpeg composer"}}
  node_final_jpeg["Final JPEG<br/>output file"]
end

subgraph group_tooling["Tooling"]
  node_build_script["Build script<br/>packaging<br/>[build.mjs]"]
  node_manifest["Manifest<br/>[manifest.json]"]
  node_tsconfig["TS config<br/>compiler config<br/>[tsconfig.json]"]
  node_package_config["Package config<br/>npm config<br/>[package.json]"]
end

node_figma_host -->|"selection"| node_selected_image
node_selected_image -->|"context"| node_plugin_main
node_plugin_main -->|"state/events"| node_message_bridge
node_message_bridge -->|"iframe"| node_ui_shell
node_ui_shell -->|"behavior"| node_ui_logic
node_ui_logic -->|"export request"| node_message_bridge
node_plugin_main -->|"export API"| node_figma_host
node_plugin_main -->|"rendered output"| node_export_render
node_plugin_main -->|"fetch bytes"| node_original_bytes
node_original_bytes -->|"metadata read"| node_exif_parser
node_original_bytes -->|"metadata read"| node_iptc_parser
node_exif_parser -->|"preserved segments"| node_segment_merge
node_iptc_parser -->|"preserved segments"| node_segment_merge
node_export_render -->|"base JPEG"| node_segment_merge
node_segment_merge -->|"download"| node_final_jpeg
node_build_script -->|"package"| node_manifest
node_build_script -->|"scripts"| node_package_config
node_build_script -->|"compile"| node_tsconfig

click node_plugin_main "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/code.ts"
click node_ui_shell "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.html"
click node_ui_logic "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.ts"
click node_exif_parser "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/exif-parser.ts"
click node_iptc_parser "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/iptc-parser.ts"
click node_build_script "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/scripts/build.mjs"
click node_manifest "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/manifest.json"
click node_tsconfig "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/tsconfig.json"
click node_package_config "https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_figma_host,node_selected_image toneBlue
class node_plugin_main,node_message_bridge,node_ui_shell,node_ui_logic toneAmber
class node_export_render,node_original_bytes,node_exif_parser,node_iptc_parser,node_segment_merge,node_final_jpeg toneMint
class node_build_script,node_manifest,node_tsconfig,node_package_config toneRose
```

## Components

### Figma host

Figma runtime provides selection, export APIs, and the original image bytes.

- **Figma host**: runtime · Plugin sandbox host and source of selection/export APIs.
- **Selected image**: figma node · User-selected node with an IMAGE fill that seeds the export workflow.

### Plugin runtime

The plugin’s main thread and embedded UI communicate through Figma’s message channel.

- [Main thread](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/code.ts): controller · Orchestrates selection handling, Figma exports, byte fetches, and JPEG assembly. · `src/code.ts`
- **Message bridge**: ipc · Standard Figma plugin messaging between main thread and UI iframe.
- [UI shell](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.html): document · Embedded iframe document that hosts the plugin interface. · `src/ui.html`
- [UI logic](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/ui.ts): view logic · Client-side behavior that initiates export actions and receives plugin state. · `src/ui.ts`

### JPEG pipeline

Export, byte retrieval, metadata parsing, and segment merging into the final downloadable JPEG.

- **Rendered JPEG**: export step · Fresh JPEG produced by Figma from the selected node’s current appearance.
- **Source bytes**: asset bytes · Original image bytes retrieved by image hash for metadata preservation.
- [EXIF/XMP parser](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/exif-parser.ts): jpeg parser · Reads JPEG APP1-style metadata blocks, including EXIF/XMP/ICC-oriented structures. · `src/exif-parser.ts`
- [IPTC parser](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/src/iptc-parser.ts): jpeg parser · Reads IPTC data from JPEG APP13 segments. · `src/iptc-parser.ts`
- **Segment merge**: jpeg composer · Combines preserved metadata segments with the freshly rendered JPEG while keeping JFIF from the export.
- **Final JPEG**: output file · Downloaded image artifact with metadata preserved where possible.

### Tooling

Build and plugin metadata files that package the repo for loading in Figma.

- [Build script](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/scripts/build.mjs): packaging · Bundles the TypeScript sources into a Figma-loadable plugin package. · `scripts/build.mjs`
- [Manifest](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/manifest.json): plugin manifest · Declares the plugin entry points and runtime wiring for Figma. · `manifest.json`
- [TS config](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/tsconfig.json): compiler config · TypeScript compilation settings for the plugin sources. · `tsconfig.json`
- [Package config](https://github.com/george-yg9ea/figma-export-images-with-metadata/blob/main/package.json): npm config · Dependency and script metadata for the repository. · `package.json`

## Connections

- Figma host → Selected image: selection · User chooses a node with an image fill.
- Selected image → Main thread: context · Selection context enters the plugin main thread.
- Main thread → Message bridge: state/events · Main thread exchanges messages with the UI.
- Message bridge → UI shell: iframe · The plugin UI runs inside the embedded document shell.
- UI shell → UI logic: behavior · The document hosts client-side UI behavior.
- UI logic → Message bridge: export request · UI initiates the export workflow through plugin messaging.
- Main thread → Figma host: export API · Main thread calls Figma APIs to render the selected node as JPEG.
- Main thread → Rendered JPEG: rendered output · Receives the freshly rendered JPEG artifact.
- Main thread → Source bytes: fetch bytes · Retrieves the original image bytes associated with the image hash.
- Source bytes → EXIF/XMP parser: metadata read · EXIF/XMP/ICC-oriented segments are parsed from the source bytes.
- Source bytes → IPTC parser: metadata read · IPTC APP13 data is parsed from the source bytes.
- EXIF/XMP parser → Segment merge: preserved segments · Parsed metadata blocks are prepared for reinsertion.
- IPTC parser → Segment merge: preserved segments · Parsed IPTC blocks are prepared for reinsertion.
- Rendered JPEG → Segment merge: base JPEG · Rendered JPEG provides the preserved JFIF-bearing base image.
- Segment merge → Final JPEG: download · Merged JPEG is produced as the final downloadable file.
- Build script → Manifest: package · Build output is wired to the Figma plugin entry points.
- Build script → Package config: scripts · Packaging and development scripts live in the npm config.
- Build script → TS config: compile · TypeScript build settings guide the bundle process.
