Component Taggers
Relevant source files
The following files were used as context for generating this wiki page:- packages/@dyad-sh/react-vite-component-tagger/src/index.ts
- packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts
- packages/@dyad-sh/react-vite-component-tagger/README.md
- e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
- e2e-tests/snapshots/mention_app.spec.ts_mention-app-without-pro-1.txt
Component Taggers are a build-time utility within the Dyad ecosystem responsible for automatically injecting unique data-dyad-id attributes into JSX elements. This process happens during the application's build step, instrumenting the code before it reaches the browser.
The primary purpose of this tagging is to create a reliable link between the rendered HTML elements in the live preview and their corresponding source code. When a user interacts with an element in the UI, this unique ID allows the Dyad AI to pinpoint the exact file, line, and column where the component is defined. This enables highly precise, context-aware code modifications. The system provides packages for both Vite and Next.js (Webpack) environments.
Sources: packages/@dyad-sh/react-vite-component-tagger/README.md, packages/@dyad-sh/react-vite-component-tagger/src/index.ts, packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts
Architecture
Component Taggers integrate directly into the project's build toolchain as either a Vite plugin or a Webpack loader. At their core, both packages use Babel to parse and transform the source code. A custom Babel plugin traverses the Abstract Syntax Tree (AST) of each JSX/TSX file, identifies component declarations, and injects the data-dyad-id attribute.
The following diagram illustrates the high-level data flow for the component tagging process.
Implementations
Dyad provides two separate packages to handle component tagging for the most common React frameworks.
| Feature | @dyad-sh/react-vite-component-tagger | @dyad-sh/nextjs-webpack-component-tagger |
|---|---|---|
| Build Tool | Vite | Webpack (for Next.js) |
| Integration | Vite Plugin | Webpack Loader |
| Core Method | transform hook (asynchronous) | Loader function (synchronous) |
| File Types | .tsx, .jsx | Inferred as .tsx, .jsx |
Sources: packages/@dyad-sh/react-vite-component-tagger/src/index.ts:20-22, packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts:10, e2e-tests/snapshots/mention_app.spec.ts_mention-app-without-pro-1.txt
Vite Plugin: @dyad-sh/react-vite-component-tagger
This package is designed to be used in Vite-based projects. It is implemented as a standard Vite plugin.
Sources: packages/@dyad-sh/react-vite-component-tagger/README.md
Usage
To use the tagger, import dyadComponentTagger and add it to the plugins array in your vite.config.ts file.
// vite.config.ts
import { defineConfig } from "vite";
import dyadComponentTagger from "@dyad-sh/react-vite-component-tagger";
import react from "@vitejs/plugin-react-swc";
import path from "path";
export default defineConfig(() => ({
server: {
host: "::",
port: 8080,
},
plugins: [dyadComponentTagger(), react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
}));
Sources: e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt (vite.config.ts)
Implementation Details
The dyadComponentTagger function returns a Vite plugin object with a transform hook. This hook is executed by Vite for each source file. The plugin filters for .tsx and .jsx files and then uses Babel's transformAsync to apply the core tagging logic.
Sources: packages/@dyad-sh/react-vite-component-tagger/src/index.ts:16-36
Next.js (Webpack) Loader: @dyad-sh/nextjs-webpack-component-tagger
This package is a Webpack loader tailored for Next.js projects.
Usage
This module exports a function that conforms to the Webpack loader interface. It is intended to be configured within the webpack section of a next.config.js file to process .tsx and .jsx files.
Sources: packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts:10-24
Implementation Details
The loader receives the source code of a file and uses Babel's synchronous transform method to apply the tagging logic. The transformed code and its source map are then returned to Webpack for further processing.
Sources: packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts:10-24
Core Logic: The Babel Tagger Plugin
Both the Vite plugin and Webpack loader share the same core logic, which is encapsulated in a Babel plugin. This plugin defines a visitor that inspects every JSXOpeningElement in the code.
The following diagram shows the decision flow within the Babel visitor.
Transformation Process
- Identify Target: The visitor pattern targets every opening JSX tag (e.g.,
<MyComponent,<div). - Filter Elements: It filters out standard HTML elements (lowercase, e.g.,
div,p) and only processes React components (PascalCase, e.g.,MyComponent). It also checks if adata-dyad-idattribute already exists to prevent duplication. - Generate ID: If the element is a component, it retrieves the source file path, line number, and column number from the Babel parser's location data (
node.loc). It combines these into a unique string identifier:filePath:startLine:startColumn. - Inject Attribute: A new JSX attribute is created with the name
data-dyad-idand the generated ID as its value. This attribute is then prepended to the element's list of attributes.
Sources: packages/@dyad-sh/react-vite-component-tagger/src/index.ts:47-70, packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts:35-58
Example Transformation
The tagger transforms simple JSX into an instrumented version that Dyad can identify.
Before Transformation:
// In: src/components/LoginButton.tsx
function LoginButton() {
return <Button>Log In</Button>;
}
After Transformation:
// In: src/components/LoginButton.tsx
function LoginButton() {
return <Button data-dyad-id="src/components/LoginButton.tsx:3:10">Log In</Button>;
}
Sources: packages/@dyad-sh/react-vite-component-tagger/src/index.ts:58-63, packages/@dyad-sh/nextjs-webpack-component-tagger/src/index.ts:46-51
Summary
Component Taggers are a foundational feature of Dyad, acting as an invisible bridge between the application's rendered user interface and its underlying source code. By integrating into the build process with dedicated packages for Vite and Next.js, they automatically instrument React components with unique source-mapped identifiers. This instrumentation is critical for enabling the AI to perform precise and reliable real-time code modifications based on user interactions with the live preview.
Sources: packages/@dyad-sh/react-vite-component-tagger/README.md