superdev-tagger
Version:
Vite plugin for component tagging
128 lines (94 loc) • 2.98 kB
Markdown
A lightweight utility for tagging React components in Vite-based applications with custom data attributes.
```bash
npm install superdev-tagger
yarn add superdev-tagger
```
This package provides two main features:
1. **Component Tagging HOC** - A higher-order component that wraps your React components with custom data attributes
2. **Vite Plugin** - A plugin that automatically tags JSX/TSX components in your Vite application
```tsx
import { tagComponent } from 'superdev-tagger';
import MyComponent from './MyComponent';
// Tag a component with default options
const TaggedComponent = tagComponent(MyComponent);
// Usage
function App() {
return <TaggedComponent />;
// Renders as: <div data-component="MyComponent">...</div>
}
```
```tsx
import { tagComponent } from 'superdev-tagger';
import MyComponent from './MyComponent';
// Tag with custom options
const TaggedComponent = tagComponent(MyComponent, {
id: 'unique-id',
attributes: {
'feature': 'search',
'version': '1.2.0'
}
});
// Usage
function App() {
return <TaggedComponent />;
// Renders as: <div data-component="MyComponent" data-component-id="unique-id" data-feature="search" data-version="1.2.0">...</div>
}
```
```tsx
function App() {
return (
<TaggedComponent
tagOptions={{
id: 'runtime-id',
attributes: {
'feature': 'dynamic-search'
}
}}
/>
);
}
```
Add the plugin to your Vite configuration:
```ts
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { componentTagger } from 'superdev-tagger';
export default defineConfig({
plugins: [
react(),
componentTagger()
],
});
```
This will automatically add data attributes to your JSX/TSX components:
```tsx
// Your component
function Button() {
return <button>Click me</button>;
}
// After processing by the plugin, it becomes:
// <button data-lov-id="Button.tsx:2:10" data-lov-name="button" data-component-path="Button.tsx" data-component-line="2" data-component-file="Button.tsx" data-component-name="button" data-component-content="{\"text\":\"Click me\"}">Click me</button>
```
Creates a higher-order component that wraps the provided component with additional data attributes.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `id` | `string` | `undefined` | A unique identifier for the component |
| `attributes` | `Record<string, string>` | `{}` | Custom data attributes to add |
| `includeComponentName` | `boolean` | `true` | Whether to include component name |
Creates a Vite plugin that automatically tags JSX/TSX components with data attributes.
MIT