Automatically add file and line number data attributes to JSX elements for easier debugging and development
A universal plugin that automatically injects data attributes into your JSX elements, making it easy to trace DOM elements back to their source code. Perfect for debugging, testing, and development tools.
Supports: Vite β‘ | Rollup π¦ | Webpack π§ | Esbuild π
- π Automatic attribute injection - No manual work required
- π File location tracking - Know exactly where each element came from
- π― Selective attributes - Choose which attributes to include
- β‘ Zero runtime overhead - Build-time transformation only
- π§ TypeScript support - Works seamlessly with TypeScript
- π¨ Customizable - Configure which attributes to add
npm install --save-dev react-data-attributes-plugin @babel/corepnpm add -D react-data-attributes-plugin @babel/coreyarn add -D react-data-attributes-plugin @babel/coreNote:
@babel/coreis required as a peer dependency. Make sure to install it.
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import reactDataAttributesPlugin from "react-data-attributes-plugin/vite";
export default defineConfig({
plugins: [reactDataAttributesPlugin(), react()],
});// rollup.config.js
import reactDataAttributesPlugin from "react-data-attributes-plugin/rollup";
export default {
plugins: [reactDataAttributesPlugin()],
};// webpack.config.js
import jsxDataAttributesLoader from "react-data-attributes-plugin/webpack";
export default {
module: {
rules: [
{
test: /\.(tsx|jsx)$/,
use: [
"babel-loader",
{
loader: jsxDataAttributesLoader,
options: {
addFileId: true,
addComponentLine: true,
},
},
],
},
],
},
};import { build } from "esbuild";
import reactDataAttributesPlugin from "react-data-attributes-plugin/esbuild";
await build({
plugins: [reactDataAttributesPlugin()],
});That's it! Your JSX elements will now automatically have data attributes added regardless of your bundler.
By default, the plugin adds the following attributes to each JSX element:
data-file-id- Full path with line and column:"src/components/Button.tsx:42:5"data-element-name- The element/component name:"Button"data-component-path- Relative file path:"src/components/Button.tsx"data-component-line- Line number:"42"data-component-file- Filename only:"Button.tsx"data-component-name- Component name (same as element-name):"Button"
<button
data-file-id="src/components/Button.tsx:42:5"
data-element-name="button"
data-component-path="src/components/Button.tsx"
data-component-line="42"
data-component-file="Button.tsx"
data-component-name="button"
>
Click me
</button>reactDataAttributesPlugin({
enabled: true, // Enable/disable plugin (default: true in development)
include: /\.(tsx|jsx)$/, // File pattern to include (default)
exclude: /\.test\./, // File pattern to exclude
});Control which attributes are added:
reactDataAttributesPlugin({
addFileId: true, // data-file-id (default: true)
addElementName: true, // data-element-name (default: true)
addComponentPath: true, // data-component-path (default: true)
addComponentLine: true, // data-component-line (default: true)
addComponentFile: true, // data-component-file (default: true)
addComponentName: true, // data-component-name (default: true)
addComponentContent: false, // data-component-content (default: false)
});Only add file location info:
reactDataAttributesPlugin({
addFileId: true,
addComponentLine: true,
addComponentFile: true,
// Disable others
addElementName: false,
addComponentPath: false,
addComponentName: false,
addComponentContent: false,
});Include URL-encoded props in data-component-content:
reactDataAttributesPlugin({
addComponentContent: true, // Adds URL-encoded JSON of props
});Quickly identify which component/file an element came from in the browser:
// In browser console
document.querySelector('[data-file-id*="Button.tsx"]');Use data attributes in your E2E tests:
// Playwright / Cypress
await page.click('[data-component-name="SubmitButton"]');Build browser extensions or dev tools that use the data attributes:
// Highlight elements from specific files
document
.querySelectorAll('[data-component-path*="components/"]')
.forEach((el) => (el.style.border = "2px solid red"));Trace production issues back to source code using the file and line information.
The plugin uses Babel to transform your JSX during the build process:
- Parse - Babel parses your JSX/TSX files
- Transform - Custom Babel plugin adds data attributes to each JSX element
- Generate - Modified JSX is passed to React's plugin for final transformation
This happens entirely at build time, with zero runtime overhead.
Only enable in development:
reactDataAttributesPlugin({
enabled: process.env.NODE_ENV === "development",
});reactDataAttributesPlugin({
exclude: /\.(test|spec)\.(tsx|jsx)$/,
});reactDataAttributesPlugin({
include: /\.(tsx|jsx|vue)$/, // Include Vue files too
});All plugins share the same configuration options:
| Option | Type | Default | Description | |
|---|---|---|---|---|
enabled |
boolean |
true (dev mode for Vite) |
Enable/disable the plugin | |
include |
RegExp |
`/.(tsx | jsx)$/` | File pattern to include |
exclude |
RegExp |
undefined |
File pattern to exclude | |
addFileId |
boolean |
true |
Add data-file-id attribute |
|
addElementName |
boolean |
true |
Add data-element-name attribute |
|
addComponentPath |
boolean |
true |
Add data-component-path attribute |
|
addComponentLine |
boolean |
true |
Add data-component-line attribute |
|
addComponentFile |
boolean |
true |
Add data-component-file attribute |
|
addComponentName |
boolean |
true |
Add data-component-name attribute |
|
addComponentContent |
boolean |
false |
Add data-component-content attribute |
- Vite:
react-data-attributes-plugin/vite - Rollup:
react-data-attributes-plugin/rollup - Webpack:
react-data-attributes-plugin/webpack - Esbuild:
react-data-attributes-plugin/esbuild - Core:
react-data-attributes-plugin/core(for custom implementations)
Contributions are welcome! Please feel free to submit a Pull Request.
MIT Β© M Arie Syukron
Inspired by tools like Lovable.dev and other development debugging utilities.
Made with β€οΈ for the developer experience