@deploya/marker-plugin
v0.0.19
Published
A marker plugin for Deploya
Readme
@deploya/marker-plugin
A Vite plugin that automatically adds debugging and tracking attributes to JSX/TSX components during development.
Overview
The Deploya Marker Plugin is a Vite plugin that enhances React/JSX development by automatically injecting tracking attributes into your components. It adds unique identifiers and source file paths to JSX elements, making it easier to debug, track, and analyze component behavior during development.
Features
- 🎯 Automatic Attribute Injection: Adds
d-idandd-sfattributes to JSX elements - 📁 Source File Tracking: Tracks which file each element comes from
- 🔧 Development Server Integration: Provides middleware to serve raw (untransformed) code
- ⚡ Vite Integration: Seamlessly integrates with Vite's transform pipeline
- 🎨 JSX/TSX Support: Works with
.jsx,.tsx,.cjsx,.mjsx,.ctsx, and.mtsxfiles
Installation
npm install @deploya/marker-pluginOr with your preferred package manager:
# yarn
yarn add @deploya/marker-plugin
# pnpm
pnpm add @deploya/marker-plugin
# bun
bun add @deploya/marker-pluginUsage
Add the plugin to your vite.config.js or vite.config.ts:
import { defineConfig } from 'vite'
import { markerPlugin } from '@deploya/marker-plugin'
export default defineConfig({
plugins: [
markerPlugin(),
// ... other plugins
],
})How It Works
The plugin automatically transforms your JSX/TSX code by adding two attributes to each JSX element:
d-id: A unique random identifier for the elementd-sf: The source file path relative to your project root
Example Transformation
Before (your source code):
function MyComponent() {
return (
<div>
<h1>Hello World</h1>
<button onClick={handleClick}>Click me</button>
</div>
)
}After (transformed by the plugin):
function MyComponent() {
return (
<div d-id="abc123def456" d-sf="/src/components/MyComponent.jsx">
<h1 d-id="ghi789jkl012" d-sf="/src/components/MyComponent.jsx">Hello World</h1>
<button d-id="mno345pqr678" d-sf="/src/components/MyComponent.jsx" onClick={handleClick}>Click me</button>
</div>
)
}Development Server Middleware
The plugin also provides a development server middleware that serves the original (untransformed) source code at the /__tagger_raw/ endpoint. This can be useful for debugging or analysis tools that need access to the original source.
For example:
- Original file:
/src/components/MyComponent.jsx - Raw source available at:
http://localhost:5173/__tagger_raw/src/components/MyComponent.jsx
API
markerPlugin()
Creates and returns a Vite plugin instance.
Returns: Plugin - A Vite plugin object
Example:
import { markerPlugin } from '@deploya/marker-plugin'
const plugin = markerPlugin()Requirements
- Vite: ^7.0.0
- Node.js: Compatible with ES2022+
Development
Setup
- Clone the repository
- Install dependencies:
npm install
Available Scripts
npm run build- Build the plugin for productionnpm run clean- Remove thedistdirectorynpm run format- Format code using Biomenpm run lint- Lint code using Biome
Building
npm run buildThis creates the distribution files in the dist/ directory.
Technical Details
- Transform Phase: The plugin runs in the
preenforce phase of Vite's transform pipeline - File Detection: Uses regex pattern
/\.([cm]?(j|t)sx)$/to identify JSX/TSX files - Element Detection: Uses regex pattern
/<([A-Za-z][A-Za-z0-9]*)\b(?![^>]*d-sf)/gto find elements without existingd-sfattributes - Code Transformation: Uses magic-string for efficient code modifications
- ID Generation: Generates random IDs using
Math.random().toString(36).substring(2, 15)
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Make your changes
- Run tests and linting:
npm run lint - Build the project:
npm run build - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/your-feature - Submit a pull request
License
UNLICENSED - This is proprietary software.
Author
Martin Jakobsson
Email: [email protected]
This plugin is part of the Deploya ecosystem for enhanced development tooling.
