{"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 './hmr'\nimport { debug, normalizePath, routeFactoryCallCodeFilter } from './utils'\nimport { getConfig } from './config'\nimport { createRouterPluginContext } from './router-plugin-context'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\nimport type { RouterPluginContext } from './router-plugin-context'\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\nexport function createRouterHmrPlugin(\n  options: Partial<Config | (() => Config)> | undefined = {},\n  routerPluginContext: RouterPluginContext,\n): ReturnType<UnpluginFactory<Partial<Config> | undefined>> {\n  let ROOT: string = process.cwd()\n\n  const resolveUserConfig = () => {\n    return getConfig(typeof options === 'function' ? options() : options, ROOT)\n  }\n\n  let userConfig = resolveUserConfig()\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: routeFactoryCallCodeFilter,\n        },\n      },\n      handler(code, id) {\n        const normalizedId = normalizePath(id)\n        const routeEntry = routerPluginContext.routesByFile.get(normalizedId)\n        if (!routeEntry) {\n          return null\n        }\n\n        if (debug) console.info('Adding HMR handling to route ', normalizedId)\n\n        const hmrStyle = userConfig.plugin?.hmr?.style ?? 'vite'\n\n        if (userConfig.target === 'react') {\n          const compilerPlugins = getReferenceRouteCompilerPlugins({\n            targetFramework: 'react',\n            addHmr: true,\n            hmrStyle,\n          })\n          const compiled = compileCodeSplitReferenceRoute({\n            code,\n            filename: normalizedId,\n            id: normalizedId,\n            addHmr: true,\n            hmrStyle,\n            hmrRouteId: routeEntry.routeId,\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, filename: normalizedId })\n        ast.program.body.push(\n          ...createRouteHmrStatement([], {\n            hmrStyle,\n            targetFramework: userConfig.target,\n            routeId: routeEntry.routeId,\n          }),\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 = resolveUserConfig()\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\nexport const unpluginRouterHmrFactory: UnpluginFactory<\n  Partial<Config> | undefined\n> = (options = {}) => {\n  return createRouterHmrPlugin(options, createRouterPluginContext())\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,SAAgB,sBACd,UAAwD,CAAC,GACzD,qBAC0D;CAC1D,IAAI,OAAe,QAAQ,IAAI;CAE/B,MAAM,0BAA0B;EAC9B,OAAO,eAAA,UAAU,OAAO,YAAY,aAAa,QAAQ,IAAI,SAAS,IAAI;CAC5E;CAEA,IAAI,aAAa,kBAAkB;CAEnC,OAAO;EACL,MAAM;EACN,SAAS;EACT,WAAW;GACT,QAAQ;IAEN,IAAI;IACJ,MAAM,EACJ,SAAS,cAAA,2BACX;GACF;GACA,QAAQ,MAAM,IAAI;IAChB,MAAM,eAAe,cAAA,cAAc,EAAE;IACrC,MAAM,aAAa,oBAAoB,aAAa,IAAI,YAAY;IACpE,IAAI,CAAC,YACH,OAAO;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