{"version":3,"sources":["../src/cartographer.ts","../src/constants.ts","../package.json"],"sourcesContent":["import fs from 'fs/promises';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport { parse } from '@babel/parser';\nimport type {\n Node as BabelNode,\n JSXIdentifier,\n JSXMemberExpression,\n JSXOpeningElement,\n} from '@babel/types';\nimport MagicString from 'magic-string';\nimport type { Plugin } from 'vite';\n\nimport { DATA_ATTRIBUTES } from './constants';\n\nconst validExtensions = new Set(['.jsx', '.tsx']);\n\nexport function cartographer(): Plugin {\n let clientScript: string;\n let configuredRoot: string;\n let configuredRootName: string;\n\n return {\n name: '@replit/vite-plugin-cartographer',\n enforce: 'pre' as const,\n\n async configResolved(config) {\n configuredRoot = config.root;\n // The name of the directory that contains the configured root\n configuredRootName = path.basename(configuredRoot);\n\n // Resolve the client script path in both CJS and ESM environments\n const currentFileUrl =\n typeof __dirname === 'string'\n ? path.join(__dirname, '../dist/beacon/index.global.js')\n : fileURLToPath(\n new URL('../dist/beacon/index.global.js', import.meta.url),\n );\n\n try {\n clientScript = await fs.readFile(currentFileUrl, 'utf-8');\n } catch (error) {\n // eslint-disable-next-line no-console -- This is an error in the plugin\n console.error(\n '[replit-cartographer] Failed to load client script:',\n error,\n );\n }\n },\n\n resolveId(_source, _importer) {\n // Let Vite handle the resolution\n return null;\n },\n\n async transform(code: string, id: string) {\n if (\n !validExtensions.has(path.extname(id)) ||\n id.includes('node_modules')\n ) {\n return null;\n }\n\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n });\n\n const magicString = new MagicString(code);\n let currentElement: BabelNode | null = null;\n\n // One day the JS ecosystem will decide on one standard to use, until then\n // We need to dynamically import this so that we don't mess with vite resolving this module\n const traverse = await import('@babel/traverse').then((m) => m.default);\n\n traverse(ast, {\n JSXElement: {\n enter(elementPath) {\n currentElement = elementPath.node;\n },\n exit() {\n currentElement = null;\n },\n },\n JSXOpeningElement(elementPath) {\n if (currentElement) {\n const jsxNode = elementPath.node;\n const elementName = getElementName(jsxNode);\n\n if (!elementName) {\n return;\n }\n\n const { line = 0, column: col = 0 } = jsxNode.loc?.start ?? {};\n\n const relativeToConfigured = path.relative(configuredRoot, id);\n const componentPath = path.join(\n configuredRootName,\n relativeToConfigured,\n );\n\n const componentMetadata =\n col === 0\n ? `${componentPath}:${line}`\n : `${componentPath}:${line}:${col}`;\n\n magicString.appendLeft(\n jsxNode.name.end ?? 0,\n ` ${DATA_ATTRIBUTES.METADATA}=\"${componentMetadata}\" ${DATA_ATTRIBUTES.COMPONENT_NAME}=\"${elementName}\"`,\n );\n }\n },\n });\n\n return {\n code: magicString.toString(),\n map: magicString.generateMap({ hires: true }),\n };\n } catch (error) {\n // eslint-disable-next-line no-console -- I want my errors to be logged :D\n console.error(`[replit-cartographer] Error processing ${id}:`, error);\n\n return null;\n }\n },\n\n transformIndexHtml() {\n if (!clientScript) {\n return [];\n }\n\n return [\n {\n tag: 'script',\n attrs: { type: 'module' },\n children: clientScript,\n injectTo: 'head',\n },\n ];\n },\n };\n}\n\n// Helper function to extract element name from JSX node\nfunction getElementName(jsxNode: JSXOpeningElement): string | null {\n if (jsxNode.name.type === 'JSXIdentifier') {\n return jsxNode.name.name;\n }\n\n if (jsxNode.name.type === 'JSXMemberExpression') {\n const memberExpr = jsxNode.name as JSXMemberExpression;\n const object = memberExpr.object as JSXIdentifier;\n const property = memberExpr.property as JSXIdentifier;\n\n return `${object.name}.${property.name}`;\n }\n\n return null;\n}\n","export const DATA_ATTRIBUTES = {\n METADATA: 'data-replit-metadata',\n COMPONENT_NAME: 'data-component-name',\n} as const;\n","{\n \"name\": \"@replit/vite-plugin-cartographer\",\n \"version\": \"0.0.11\",\n \"private\": false,\n \"devDependencies\": {\n \"@replit/tsconfig\": \"workspace:*\",\n \"@types/babel__core\": \"^7.20.5\",\n \"@types/babel__traverse\": \"^7.20.6\",\n \"@types/node\": \"^22.5.5\",\n \"@typescript-eslint/eslint-plugin\": \"^6.7.0\",\n \"@typescript-eslint/parser\": \"^6.7.0\",\n \"tsup\": \"^8.3.5\",\n \"tsx\": \"^4.9.5\",\n \"vite\": \"^5.4.10\"\n },\n \"main\": \"./src/index.ts\",\n \"files\": [\n \"src\"\n ],\n \"scripts\": {\n \"build\": \"tsup\",\n \"lint\": \"eslint src\",\n \"format\": \"prettier --write \\\"src/**/*.ts\\\"\"\n },\n \"dependencies\": {\n \"@babel/parser\": \"^7.26.9\",\n \"@babel/traverse\": \"^7.26.9\",\n \"@babel/types\": \"^7.26.9\",\n \"magic-string\": \"^0.30.12\",\n \"modern-screenshot\": \"^4.6.0\"\n }\n}\n"],"mappings":";AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAC9B,SAAS,aAAa;AAOtB,OAAO,iBAAiB;;;ACVjB,IAAM,kBAAkB;AAAA,EAC7B,UAAU;AAAA,EACV,gBAAgB;AAClB;;;ADYA,IAAM,kBAAkB,oBAAI,IAAI,CAAC,QAAQ,MAAM,CAAC;AAEzC,SAAS,eAAuB;AACrC,MAAI;AACJ,MAAI;AACJ,MAAI;AAEJ,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,MAAM,eAAe,QAAQ;AAC3B,uBAAiB,OAAO;AAExB,2BAAqB,KAAK,SAAS,cAAc;AAGjD,YAAM,iBACJ,OAAO,cAAc,WACjB,KAAK,KAAK,WAAW,gCAAgC,IACrD;AAAA,QACE,IAAI,IAAI,kCAAkC,YAAY,GAAG;AAAA,MAC3D;AAEN,UAAI;AACF,uBAAe,MAAM,GAAG,SAAS,gBAAgB,OAAO;AAAA,MAC1D,SAAS,OAAO;AAEd,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,UAAU,SAAS,WAAW;AAE5B,aAAO;AAAA,IACT;AAAA,IAEA,MAAM,UAAU,MAAc,IAAY;AACxC,UACE,CAAC,gBAAgB,IAAI,KAAK,QAAQ,EAAE,CAAC,KAC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