Add data-source="path:line" to every JSX element in development, for Vite, Next.js and Babel. Zero runtime, works in any DevTools, and great with AI coding agents.

It's your first day on a new project. You don't know the codebase at all, and your first task is to fix a button on the checkout page. You open DevTools, click the button and see this:

<button class="MuiButtonBase-root MuiButton-root css-1x2y3z4"> 

Now find it in the code. You search for the class name: it's generated. You search for the text: it comes from translations. You search for the component name: it's used in 140 places. You follow imports from the router to the page, from the page to a layout, from the layout to a feature folder. Tens of minutes later, sometimes hours, you finally find the line you need.

I did exactly this for years, on every new project. Then a small Babel plugin turned up, and my everyday development changed for good: I stopped searching and started opening the right line straight away.

Since then it has moved with me from project to project. But projects moved on to Next.js and Vite, where a Babel plugin doesn't fit, and I didn't want to go back to searching. So I wrote an SWC plugin for Next.js and Rspack and a native Vite plugin, with the same options and the same output. Now it covers the three main React toolchains.

What it does

The plugin adds a data-source attribute with the file and line to every JSX element:

// src/components/Example.tsx <div className="wrapper"> <Pencil size={16} /> <Dialog open={isOpen}> <DialogContent>Hello</DialogContent> </Dialog> </div> 

becomes

<div className="wrapper" data-source="src/components/Example.tsx:2"> <Pencil size={16} data-source="src/components/Example.tsx:3" /> <Dialog open={isOpen} data-source="src/components/Example.tsx:4"> <DialogContent data-source="src/components/Example.tsx:5">Hello</DialogContent> </Dialog> </div> 

Inspect any node in the browser and the file and line are right there in the Elements panel. No extension, no runtime, no editor integration needed.

Why not React DevTools or a click-to-component tool?

Those are great, and I use them too. A plain attribute has a few properties they don't:

  • It's just HTML. It works in any browser, in Safari's Web Inspector, in a remote session, in document.querySelector('[data-source^="src/pages/checkout"]'), in a Playwright trace or a saved HTML snapshot.
  • No runtime. Nothing is injected into the bundle except the attribute strings, and you enable the plugin in development only, so production stays clean.
  • It doesn't depend on React internals. React 19 stopped keeping the __source location on elements and _debugSource on fibers, which broke click-to-source tools; they had to switch to other mechanisms. An attribute added at build time doesn't care.
  • It's copyable. QA can paste it into a bug report. You can paste it into a chat with an AI agent.

The AI agent use case

This turned out to be the main reason I use it today. When I work with Claude Code, Cursor or Copilot on UI, the hardest part of a prompt is often "which element do I mean". Now I inspect it, copy data-source="src/features/cart/Summary.tsx:58", and the agent opens the exact file and line. No guessing between three components with similar names, no "I've updated the wrong Button".

Setup

Vite

npm install -D vite-plugin-react-source-string 
// vite.config.ts import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import reactSourceString from "vite-plugin-react-source-string"; export default defineConfig(({ command }) => ({ plugins: [ command === "serve" && reactSourceString(), react(), ], })); 

It's a native Vite plugin written in TypeScript: it parses with oxc-parser, rewrites with magic-string (with source maps) and runs with enforce: "pre". No Babel, no WASM. It works with Vite 5 and later, including Rolldown-based Vite. Every Monday CI builds a test app with the plugin on Vite 5, 6, 7, the latest release and the current beta, so a breaking Vite release shows up before it reaches you.

Next.js (SWC)

npm install -D swc-plugin-react-source-string@next-16 
// next.config.ts const nextConfig = { experimental: { swcPlugins: process.env.NODE_ENV === "development" ? [["swc-plugin-react-source-string", { root: process.cwd() }]] : [], }, }; export default nextConfig; 

SWC plugins are WASM and tied to the swc_core version inside the bundler, so the package has dist-tags per bundler: next-16, next-15 and rspack. The README has the table. Every Monday a CI job checks for new Next.js and Rspack releases. When one ships with a new swc_core, the job rebuilds the plugin, runs a real next build with it and opens a pull request.

Babel and Webpack

npm install -D babel-plugin-react-source-string 
// babel.config.js module.exports = (api) => ({ plugins: api.env("development") ? ["babel-plugin-react-source-string"] : [], }); 

Babel 7 and Babel 8 are supported. CI runs the tests against both, and against the newest @babel/core, every week.

Options

All three plugins share the same options and produce the same output:

Option What it does
excluded Names to skip, e.g. ["Slot"]. Case-insensitive.
root Project root for relative paths.
libraries Tag HTML elements plus only the components imported from these packages, e.g. ["@mui/material"]. Less noise in the props of your own components.
preferCallSite For wrappers: point to where a component is used, not to the wrapper. More below.

A reader's question that became a feature

After my LinkedIn post about the plugins, a reader asked:

Does the Vite plugin handle components that pass props down to a UI library element, so the attribute points at your own file and not at node_modules?

The short answer was yes: the plugin never transforms node_modules, so the attribute always points to your code. By default it points to the innermost file in your code that renders the DOM node. Take a typical design-system wrapper:

// src/components/MyButton.tsx export function MyButton(props) { return <Button variant="contained" {...props} />; // line 2 } // src/pages/Checkout.tsx <MyButton onClick={pay}>Pay</MyButton> // line 42 

Both elements are tagged. <MyButton> gets Checkout.tsx:42 and passes it down through {...props}, but inside the wrapper the plugin adds its own attribute after the spread, so it wins. The DOM says MyButton.tsx:2.

That's right when you're working on MyButton. But the question made me think about the other case: MyButton is used 300 times, and what you actually want to know is which page rendered this particular button.

So all three plugins now have an opt-in flag:

reactSourceString({ preferCallSite: true }) 

With it, the plugin puts its attribute before spread props, so a value coming from the parent wins and the DOM says Checkout.tsx:42. It works through nested wrappers too: in Page → Toolbar → MyButton → Button, where each one spreads its props, the outermost call site wins.

Where it helps:

  • Design systems and UI kits. "This is MyButton" tells you little. "This is the Pay button on the checkout page" tells you everything.
  • Bug reports and QA. The attribute leads to the page code that configures the element: its props, text and handlers.
  • AI agents. The agent edits that one usage instead of changing a wrapper the whole app depends on.

It's off by default, because when you work on the wrappers themselves you want to land in the wrapper. One limitation: a wrapper that picks props explicitly (className, onClick) and doesn't spread them still points to itself, since the call-site value never reaches the DOM.

A few implementation notes

  • The line is the line of the opening <, so multi-line elements point to their start.
  • An element that already has data-source is left alone. That's exactly what makes preferCallSite possible.
  • Fragments are never tagged: <>, <Fragment> and <React.Fragment> accept only key and children, and React warns about any other prop.
  • Turbopack-style virtual paths like [project]/src/a.tsx are normalized, so the output is the same across bundlers.
  • Releases go through release-please and npm trusted publishing with provenance. No long-lived npm tokens.

Try it

preferCallSite came from a single comment, so feedback really does shape these plugins. Issues and PRs are welcome, and if a plugin saves you time, a ⭐ on GitHub helps other people find it.