{"version":3,"file":"router-generator-plugin.js","sources":["../../../src/core/router-generator-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize } from 'node:path'\nimport { Generator, resolveConfigPath } from '@tanstack/router-generator'\nimport { getConfig } from './config'\n\nimport type { GeneratorEvent } from '@tanstack/router-generator'\nimport type { FSWatcher } from 'chokidar'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\nconst PLUGIN_NAME = 'unplugin:router-generator'\n\nexport const unpluginRouterGeneratorFactory: UnpluginFactory<\n  Partial<Config | (() => Config)> | undefined\n> = (options = {}) => {\n  let ROOT: string = process.cwd()\n  let userConfig: Config\n  let generator: Generator\n\n  const routeGenerationDisabled = () =>\n    userConfig.enableRouteGeneration === false\n  const getRoutesDirectoryPath = () => {\n    return isAbsolute(userConfig.routesDirectory)\n      ? userConfig.routesDirectory\n      : join(ROOT, userConfig.routesDirectory)\n  }\n\n  const initConfigAndGenerator = (opts?: { root?: string }) => {\n    if (opts?.root) {\n      ROOT = opts.root\n    }\n    if (typeof options === 'function') {\n      userConfig = options()\n    } else {\n      userConfig = getConfig(options, ROOT)\n    }\n    generator = new Generator({\n      config: userConfig,\n      root: ROOT,\n    })\n  }\n\n  const generate = async (opts?: {\n    file: string\n    event: 'create' | 'update' | 'delete'\n  }) => {\n    if (routeGenerationDisabled()) {\n      return\n    }\n    let generatorEvent: GeneratorEvent | undefined = undefined\n    if (opts) {\n      const filePath = normalize(opts.file)\n      if (filePath === resolveConfigPath({ configDirectory: ROOT })) {\n        initConfigAndGenerator()\n        return\n      }\n      generatorEvent = { path: filePath, type: opts.event }\n    }\n\n    try {\n      await generator.run(generatorEvent)\n      globalThis.TSR_ROUTES_BY_ID_MAP = generator.getRoutesByFileMap()\n    } catch (e) {\n      console.error(e)\n    }\n  }\n\n  return {\n    name: 'tanstack:router-generator',\n    enforce: 'pre',\n    async watchChange(id, { event }) {\n      await generate({\n        file: id,\n        event,\n      })\n    },\n    vite: {\n      async configResolved(config) {\n        initConfigAndGenerator({ root: config.root })\n        await generate()\n      },\n    },\n    rspack(compiler) {\n      initConfigAndGenerator()\n\n      let handle: FSWatcher | null = null\n\n      compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())\n\n      compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n        if (handle) {\n          return\n        }\n\n        // rspack watcher doesn't register newly created files\n        const routesDirectoryPath = getRoutesDirectoryPath()\n        const chokidar = await import('chokidar')\n        handle = chokidar\n          .watch(routesDirectoryPath, { ignoreInitial: true })\n          .on('add', (file) => generate({ file, event: 'create' }))\n\n        await generate()\n      })\n\n      compiler.hooks.watchClose.tap(PLUGIN_NAME, async () => {\n        if (handle) {\n          await handle.close()\n        }\n      })\n    },\n    webpack(compiler) {\n      initConfigAndGenerator()\n\n      let handle: FSWatcher | null = null\n\n      compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())\n\n      compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n        if (handle) {\n          return\n        }\n\n        // webpack watcher doesn't register newly created files\n        const routesDirectoryPath = getRoutesDirectoryPath()\n        const chokidar = await import('chokidar')\n        handle = chokidar\n          .watch(routesDirectoryPath, { ignoreInitial: true })\n          .on('add', (file) => generate({ file, event: 'create' }))\n\n        await generate()\n      })\n\n      compiler.hooks.watchClose.tap(PLUGIN_NAME, async () => {\n        if (handle) {\n          await handle.close()\n        }\n      })\n\n      compiler.hooks.done.tap(PLUGIN_NAME, () => {\n        console.info('✅ ' + PLUGIN_NAME + ': route-tree generation done')\n      })\n    },\n    esbuild: {\n      config() {\n        initConfigAndGenerator()\n      },\n    },\n  }\n}\n"],"names":[],"mappings":";;;AASA,MAAM,cAAc;AAEb,MAAM,iCAET,CAAC,UAAU,OAAO;AACpB,MAAI,OAAe,QAAQ,IAAA;AAC3B,MAAI;AACJ,MAAI;AAEJ,QAAM,0BAA0B,MAC9B,WAAW,0BAA0B;AACvC,QAAM,yBAAyB,MAAM;AACnC,WAAO,WAAW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