{"version":3,"file":"router-hmr-plugin.cjs","names":[],"sources":["../../../src/core/router-hmr-plugin.ts"],"sourcesContent":["import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'\nimport { compileCodeSplitReferenceRoute } from './code-splitter/compilers'\nimport { getReferenceRouteCompilerPlugins } from './code-splitter/plugins/framework-plugins'\nimport { createRouteHmrStatement } from './route-hmr-statement'\nimport { debug, normalizePath } from './utils'\nimport { getConfig } from './config'\nimport { resolveHmrHotExpression } from './hmr-hot-expression'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\n/**\n * This plugin adds HMR support for file routes.\n * It is only added to the composed plugin in dev when autoCodeSplitting is disabled, since the code splitting plugin\n * handles HMR for code-split routes itself.\n */\n\nconst includeCode = [\n  'createFileRoute(',\n  'createRootRoute(',\n  'createRootRouteWithContext(',\n]\nexport const unpluginRouterHmrFactory: UnpluginFactory<\n  Partial<Config> | undefined\n> = (options = {}) => {\n  let ROOT: string = process.cwd()\n  let userConfig = options as Config\n\n  return {\n    name: 'tanstack-router:hmr',\n    enforce: 'pre',\n    transform: {\n      filter: {\n        // this is necessary for webpack / rspack to avoid matching .html files\n        id: /\\.(m|c)?(j|t)sx?$/,\n        code: {\n          include: includeCode,\n        },\n      },\n      handler(code, id) {\n        const normalizedId = normalizePath(id)\n        if (!globalThis.TSR_ROUTES_BY_ID_MAP?.has(normalizedId)) {\n          return null\n        }\n\n        if (debug) console.info('Adding HMR handling to route ', normalizedId)\n\n        const hmrHotExpression = resolveHmrHotExpression(\n          userConfig.plugin?.hmr?.hotExpression,\n        )\n\n        if (userConfig.target === 'react') {\n          const compilerPlugins = getReferenceRouteCompilerPlugins({\n            targetFramework: 'react',\n            addHmr: true,\n            hmrHotExpression,\n          })\n          const compiled = compileCodeSplitReferenceRoute({\n            code,\n            filename: normalizedId,\n            id: normalizedId,\n            addHmr: true,\n            hmrHotExpression,\n            codeSplitGroupings: [],\n            targetFramework: 'react',\n            compilerPlugins,\n          })\n\n          if (compiled) {\n            if (debug) {\n              logDiff(code, compiled.code)\n              console.log('Output:\\n', compiled.code + '\\n\\n')\n            }\n\n            return compiled\n          }\n        }\n\n        const ast = parseAst({ code })\n        ast.program.body.push(\n          createRouteHmrStatement([], { hotExpression: hmrHotExpression }),\n        )\n        const result = generateFromAst(ast, {\n          sourceMaps: true,\n          filename: normalizedId,\n          sourceFileName: normalizedId,\n        })\n        if (debug) {\n          logDiff(code, result.code)\n          console.log('Output:\\n', result.code + '\\n\\n')\n        }\n        return result\n      },\n    },\n    vite: {\n      configResolved(config) {\n        ROOT = config.root\n        userConfig = getConfig(options, ROOT)\n      },\n      applyToEnvironment(environment) {\n        if (userConfig.plugin?.vite?.environmentName) {\n          return userConfig.plugin.vite.environmentName === environment.name\n        }\n        return true\n      },\n    },\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,IAAM,cAAc;CAClB;CACA;CACA;CACD;AACD,IAAa,4BAER,UAAU,EAAE,KAAK;CACpB,IAAI,OAAe,QAAQ,KAAK;CAChC,IAAI,aAAa;AAEjB,QAAO;EACL,MAAM;EACN,SAAS;EACT,WAAW;GACT,QAAQ;IAEN,IAAI;IACJ,MAAM,EACJ,SAAS,aACV;IACF;GACD,QAAQ,MAAM,IAAI;IAChB,MAAM,eAAe,cAAA,cAAc,GAAG;AACtC,QAAI,CAAC,WAAW,sBAAsB,IAAI,aAAa,CACrD,QAAO;AAGT,QAAI,cAAA,MAAO,SAAQ,KAAK,iCAAiC,aAAa;IAEtE,MAAM,mBAAmB,2BAAA,wBACvB,WAAW,QAAQ,KAAK,cACzB;AAED,QAAI,WAAW,WAAW,SAAS;KAMjC,MAAM,WAAW,kBAAA,+BAA+B;MAC9C;MACA,UAAU;MACV,IAAI;MACJ,QAAQ;MACR;MACA,oBAAoB,EAAE;MACtB,iBAAiB;MACjB,iBAbsB,0BAAA,iCAAiC;OACvD,iBAAiB;OACjB,QAAQ;OACR;OACD,CAAC;MAUD,CAAC;AAEF,SAAI,UAAU;AAC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