A Babel plugin that automatically adds source file and line number debugging information to JSX elements by injecting a __source prop containing the original source location where each JSX element is defined. This is primarily used for development debugging and React DevTools integration.
npm install --save-dev babel-plugin-transform-react-jsx-sourceThis is a Babel plugin, so it's not imported directly in application code. Instead, it's configured in Babel configuration files.
{
"plugins": ["transform-react-jsx-source"]
}babel --plugins transform-react-jsx-source script.jsrequire("babel-core").transform("code", {
plugins: ["transform-react-jsx-source"]
});Input JSX:
<div>
<span>Hello World</span>
</div>Output after transformation:
var _jsxFileName = "/path/to/file.js";
<div __source={{ fileName: _jsxFileName, lineNumber: 1 }}>
<span __source={{ fileName: _jsxFileName, lineNumber: 2 }}>Hello World</span>
</div>This plugin implements a Babel AST visitor that traverses JavaScript/JSX code and transforms JSX opening elements. It operates by:
__source attribute with location data to JSX elementsThe main plugin export that creates a Babel AST visitor for transforming JSX elements.
/**
* Main Babel plugin function that returns a visitor for transforming JSX elements
* @param {Object} param - Babel plugin parameter object
* @param {Object} param.types - Babel types (t) for AST node creation
* @returns {Object} Babel plugin object with visitor
*/
export default function ({ types: t }) {
// Returns { visitor: { JSXOpeningElement(path, state) { ... } } }
}The plugin uses the following internal constants:
/** The attribute name added to JSX elements */
const TRACE_ID = "__source";
/** The variable name used for storing filename */
const FILE_NAME_VAR = "_jsxFileName";The plugin includes an internal helper function for creating source trace objects:
/**
* Creates AST object expression for __source prop value
* @param {Object} fileNameIdentifier - Reference to filename variable
* @param {number} lineNumber - Line number where JSX element starts
* @returns {Object} AST object with fileName and lineNumber properties
*/
function makeTrace(fileNameIdentifier, lineNumber) {
// Returns t.objectExpression([fileNameProperty, lineNumberProperty])
}/**
* Babel plugin structure returned by the default export
*/
interface BabelPlugin {
/** AST visitor object containing transformation methods */
visitor: {
/** Visitor method for processing JSX opening elements */
JSXOpeningElement(path, state);
};
}
/**
* Structure of the __source prop value added to JSX elements
*/
interface SourceInfo {
/** The source file name where the JSX element is defined */
fileName: string | null;
/** The line number where the JSX element starts */
lineNumber: number | null;
}__source attribute are transformed_jsxFileName) is generated once per filenull is used instead__source prop is added to each qualifying JSX elementfileName and lineNumber propertiesfileName references the generated filename variablelineNumber contains the line number where the JSX element startsComplex JSX with nested elements:
// Input
function MyComponent() {
return (
<div className="container">
<h1>Title</h1>
<button onClick={handleClick}>
Click me
</button>
</div>
);
}
// Output
var _jsxFileName = "/src/components/MyComponent.js";
function MyComponent() {
return (
<div className="container" __source={{ fileName: _jsxFileName, lineNumber: 3 }}>
<h1 __source={{ fileName: _jsxFileName, lineNumber: 4 }}>Title</h1>
<button onClick={handleClick} __source={{ fileName: _jsxFileName, lineNumber: 5 }}>
Click me
</button>
</div>
);
}This plugin requires the following dependencies:
babel-runtime: Babel runtime supportbabel-plugin-syntax-jsx: JSX syntax parsing supportThis plugin is primarily used for:
Important: This plugin should typically be used only in development builds and excluded from production bundles for performance reasons.