diff --git a/packages/babel-plugin-transform-react-jsx-source/src/index.js b/packages/babel-plugin-transform-react-jsx-source/src/index.js
index b02ad1bc97..c9220faaeb 100644
--- a/packages/babel-plugin-transform-react-jsx-source/src/index.js
+++ b/packages/babel-plugin-transform-react-jsx-source/src/index.js
@@ -10,30 +10,39 @@
*
* becomes:
*
- *
+ * var __jsxFileName = 'this/file.js';
+ *
*/
-import path from "path";
-
const TRACE_ID = "__source";
+const FILE_NAME_VAR = "__jsxFileName";
export default function ({ types: t }) {
- function makeTrace(fileName, lineNumber) {
- const fileNameLiteral = fileName != null ? t.stringLiteral(fileName) : t.nullLiteral();
+ function makeTrace(lineNumber) {
const fileLineLiteral = lineNumber != null ? t.numericLiteral(lineNumber) : t.nullLiteral();
- const fileNameProperty = t.objectProperty(t.identifier("fileName"), fileNameLiteral);
+ const fileNameProperty = t.objectProperty(t.identifier("fileName"), t.identifier(FILE_NAME_VAR));
const lineNumberProperty = t.objectProperty(t.identifier("lineNumber"), fileLineLiteral);
return t.objectExpression([fileNameProperty, lineNumberProperty]);
}
+ function makeFileNameConst(fileName) {
+ const declaration = t.variableDeclarator(t.identifier(FILE_NAME_VAR), t.stringLiteral(fileName));
+ return t.variableDeclaration("var", [declaration]);
+ }
+
let visitor = {
- JSXOpeningElement(node, state) {
- const id = t.jSXIdentifier(TRACE_ID);
- const fileName = state.file.log.filename !== "unknown"
- ? path.relative(__dirname, state.file.log.filename)
+ Program(node, state) {
+ const fileName = state.file.log.filename !== "unknown"
+ ? state.file.log.filename
: null;
- const trace = makeTrace(fileName, node.container.openingElement.loc.start.line);
+
+ node.container.program.body.unshift(makeFileNameConst(fileName));
+ },
+
+ JSXOpeningElement(node) {
+ const id = t.jSXIdentifier(TRACE_ID);
+ const trace = makeTrace(node.container.openingElement.loc.start.line);
node.container.openingElement.attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
}
diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/expected.js b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/expected.js
index 9f07aedd41..caa05e2a3e 100644
--- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/expected.js
+++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/expected.js
@@ -1,4 +1,5 @@
+var __jsxFileName = "/I/am/not/sure/how/to/get/path/to/test/fixtures/react-source/basic-sample/actual.js";
var x = ;
\ No newline at end of file
+}} />;