From 9f76cf7c424f4ade774ddb021a154adbf69eaeaa Mon Sep 17 00:00:00 2001 From: Alex Kotliarskyi Date: Tue, 19 Jan 2016 20:10:53 -0800 Subject: [PATCH] Hoist current file name for transform-react-jsx-source For better tooling support it's important to include absolute file name for JSX elements. However, having them inline will inflate resulting file size, so we move the file name to a constant declared at the beginning of the file and reference it from `__source` attribute. --- .../src/index.js | 31 ++++++++++++------- .../react-source/basic-sample/expected.js | 5 +-- 2 files changed, 23 insertions(+), 13 deletions(-) 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 +}} />;