UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

1 lines 6.16 kB
{"version":3,"file":"router-generator-plugin.cjs","names":[],"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"],"mappings":";;;;;AASA,IAAM,cAAc;AAEpB,IAAa,kCAER,UAAU,EAAE,KAAK;CACpB,IAAI,OAAe,QAAQ,KAAK;CAChC,IAAI;CACJ,IAAI;CAEJ,MAAM,gCACJ,WAAW,0BAA0B;CACvC,MAAM,+BAA+B;AACnC,UAAA,GAAA,UAAA,YAAkB,WAAW,gBAAgB,GACzC,WAAW,mBAAA,GAAA,UAAA,MACN,MAAM,WAAW,gBAAgB;;CAG5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