import type { Plugin } from "rollup"; import type { FilterPattern } from "@rollup/pluginutils"; import { createFilter } from "@rollup/pluginutils"; import { getSwc } from "../swc/loadSwc.js"; import { buildSourceLoc } from "../utils/source-loc"; import type * as swcTypes from "@swc/wasm-web"; export interface JsxSourceOptions { /** @default ["**\/*.jsx", "**\/*.tsx"] */ include?: FilterPattern; exclude?: FilterPattern; /** @default process.cwd() */ rootDir?: string; } const DUMMY_SPAN: swcTypes.Span = { start: 0, end: 0, ctxt: 0 }; function buildLineTable(code: string): number[] { const table: number[] = [0]; for (let i = 0; i < code.length; i++) { if (code[i] === "\n") table.push(i + 1); } return table; } function offsetToLineCol( table: number[], offset: number, ): { line: number; col: number } { let lo = 0; let hi = table.length - 1; while (lo < hi) { const mid = (lo + hi + 1) >> 1; if (table[mid] <= offset) lo = mid; else hi = mid - 1; } return { line: lo + 1, col: offset - table[lo] + 1 }; } function walkMutate( node: swcTypes.Node, visit: (node: swcTypes.Node) => void, ): void { if (!node || typeof node !== "object") return; if (Array.isArray(node)) { for (const item of node) walkMutate(item as swcTypes.Node, visit); return; } visit(node); for (const key of Object.keys(node)) { if (key === "span") continue; walkMutate( (node as unknown as Record)[key] as swcTypes.Node, visit, ); } } /** * Rollup plugin that injects `data-source-loc="file:line:col"` onto every JSX * opening element at build time. * * Place this plugin **before** `swc()` in your plugin list so it sees raw * TypeScript+JSX before compilation: * * ```ts * plugins: [jsxSource({ rootDir: 'src' }), swc()] * ``` */ export function jsxSource(options: JsxSourceOptions = {}): Plugin { const { rootDir = process.cwd(), include, exclude } = options; const filter = createFilter(include ?? ["**/*.jsx", "**/*.tsx"], exclude); return { name: "jsx-source", async transform(code, id) { if (!filter(id)) return null; const swc = await getSwc(); const isTs = /\.tsx?$/.test(id); const hasJsx = /\.[jt]sx$/.test(id); const program = await swc.parse( code, isTs ? { syntax: "typescript", tsx: hasJsx } : { syntax: "ecmascript", jsx: hasJsx }, ); const lineTable = buildLineTable(code); // Compute relative path for the data-source value const sep = id.includes("/") ? "/" : "\\"; const rootWithSep = rootDir.endsWith(sep) ? rootDir : rootDir + sep; const relPath = id.startsWith(rootWithSep) ? id.slice(rootWithSep.length) : id; const baseOffset: number = program.span?.start ?? 0; walkMutate(program as swcTypes.Node, (node) => { if (node.type !== "JSXOpeningElement") return; const jsxNode = node as swcTypes.JSXOpeningElement; const { line, col } = offsetToLineCol( lineTable, jsxNode.span.start - baseOffset, ); const locValue = buildSourceLoc(relPath, line, col); const sourceAttr: swcTypes.JSXAttribute = { type: "JSXAttribute", span: DUMMY_SPAN, name: { type: "Identifier", span: DUMMY_SPAN, value: "data-source-loc", optional: false, }, value: { type: "StringLiteral", span: DUMMY_SPAN, value: locValue, raw: JSON.stringify(locValue), }, }; if (!Array.isArray(jsxNode.attributes)) jsxNode.attributes = []; jsxNode.attributes.unshift(sourceAttr); }); const result = await swc.print(program, { filename: id, sourceMaps: true, }); return { code: result.code, map: result.map }; }, }; }