@tanstack/router-plugin 1.121.6 → 1.121.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/core/code-splitter/compilers.cjs +17 -18
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +0 -1
- package/dist/cjs/core/config.d.cts +4 -4
- package/dist/cjs/core/route-autoimport-plugin.cjs +51 -45
- package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +65 -58
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-composed-plugin.cjs +7 -1
- package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +18 -19
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +26 -24
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/utils.cjs +0 -7
- package/dist/cjs/core/utils.cjs.map +1 -1
- package/dist/cjs/core/utils.d.cts +0 -1
- package/dist/esm/core/code-splitter/compilers.d.ts +0 -1
- package/dist/esm/core/code-splitter/compilers.js +17 -18
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/config.d.ts +4 -4
- package/dist/esm/core/route-autoimport-plugin.js +52 -46
- package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +67 -60
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-composed-plugin.js +7 -1
- package/dist/esm/core/router-composed-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +19 -20
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +27 -25
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/core/utils.d.ts +0 -1
- package/dist/esm/core/utils.js +1 -8
- package/dist/esm/core/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/core/code-splitter/compilers.ts +25 -24
- package/src/core/route-autoimport-plugin.ts +57 -53
- package/src/core/router-code-splitter-plugin.ts +75 -79
- package/src/core/router-composed-plugin.ts +7 -1
- package/src/core/router-generator-plugin.ts +23 -24
- package/src/core/router-hmr-plugin.ts +24 -23
- package/src/core/utils.ts +0 -15
- package/src/global.d.ts +7 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-code-splitter-plugin.js","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["/**\n * It is important to familiarize yourself with how the code-splitting works in this plugin.\n * https://github.com/TanStack/router/pull/3355\n */\n\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { logDiff } from '@tanstack/router-utils'\nimport { getConfig, splitGroupingsSchema } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n detectCodeSplitGroupingsFromRoute,\n} from './code-splitter/compilers'\nimport {\n defaultCodeSplitGroupings,\n splitRouteIdentNodes,\n tsrSplit,\n} from './constants'\nimport { decodeIdentifier } from './code-splitter/path-ids'\nimport { debug, fileIsInRoutesDirectory } from './utils'\nimport type { CodeSplitGroupings, SplitRouteIdentNodes } from './constants'\n\nimport type { Config } from './config'\nimport type {\n UnpluginContextMeta,\n UnpluginFactory,\n TransformResult as UnpluginTransformResult,\n} from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(externalPlugin: BannedBeforeExternalPlugin, framework: string) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const isProduction = process.env.NODE_ENV === 'production'\n\n const getGlobalCodeSplitGroupings = () => {\n return (\n userConfig.codeSplittingOptions?.defaultBehavior ||\n defaultCodeSplitGroupings\n )\n }\n const getShouldSplitFn = () => {\n return userConfig.codeSplittingOptions?.splitBehavior\n }\n\n const handleCompilingReferenceFile = (\n code: string,\n id: string,\n ): UnpluginTransformResult => {\n if (debug) console.info('Compiling Route: ', id)\n\n const fromCode = detectCodeSplitGroupingsFromRoute({\n code,\n })\n\n if (fromCode.groupings) {\n const res = splitGroupingsSchema.safeParse(fromCode.groupings)\n if (!res.success) {\n const message = res.error.errors.map((e) => e.message).join('. ')\n throw new Error(\n `The groupings for the route \"${id}\" are invalid.\\n${message}`,\n )\n }\n }\n\n const userShouldSplitFn = getShouldSplitFn()\n\n const pluginSplitBehavior = userShouldSplitFn?.({\n routeId: fromCode.routeId,\n }) as CodeSplitGroupings | undefined\n\n if (pluginSplitBehavior) {\n const res = splitGroupingsSchema.safeParse(pluginSplitBehavior)\n if (!res.success) {\n const message = res.error.errors.map((e) => e.message).join('. ')\n throw new Error(\n `The groupings returned when using \\`splitBehavior\\` for the route \"${id}\" are invalid.\\n${message}`,\n )\n }\n }\n\n const splitGroupings: CodeSplitGroupings =\n fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings()\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n runtimeEnv: isProduction ? 'prod' : 'dev',\n codeSplitGroupings: splitGroupings,\n targetFramework: userConfig.target,\n filename: id,\n id,\n })\n\n if (debug) {\n logDiff(code, compiledReferenceRoute.code)\n console.log('Output:\\n', compiledReferenceRoute.code + '\\n\\n')\n }\n\n return compiledReferenceRoute\n }\n\n const handleCompilingVirtualFile = (\n code: string,\n id: string,\n ): UnpluginTransformResult => {\n if (debug) console.info('Splitting Route: ', id)\n\n const [_, ...pathnameParts] = id.split('?')\n\n const searchParams = new URLSearchParams(pathnameParts.join('?'))\n const splitValue = searchParams.get(tsrSplit)\n\n if (!splitValue) {\n throw new Error(\n `The split value for the virtual route \"${id}\" was not found.`,\n )\n }\n\n const rawGrouping = decodeIdentifier(splitValue)\n const grouping = [...new Set(rawGrouping)].filter((p) =>\n splitRouteIdentNodes.includes(p as any),\n ) as Array<SplitRouteIdentNodes>\n\n const result = compileCodeSplitVirtualRoute({\n code,\n filename: id,\n splitTargets: grouping,\n })\n\n if (debug) {\n logDiff(code, result.code)\n console.log('Output:\\n', result.code + '\\n\\n')\n }\n\n return result\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n\n transform(code, id) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(tsrSplit)) {\n return handleCompilingVirtualFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingReferenceFile(code, id)\n }\n\n return null\n },\n\n transformInclude(id) {\n if (!userConfig.autoCodeSplitting) {\n return undefined\n }\n\n if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||\n id.includes(tsrSplit)\n ) {\n return true\n }\n return false\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack(_compiler) {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n\n if (\n userConfig.autoCodeSplitting &&\n compiler.options.mode === 'production'\n ) {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":[],"mappings":";;;;;;;AA6BA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AASA,MAAM,8BAAiE;AAAA,EACrE;AAAA,IACE,YAAY;AAAA,IACZ,KAAK;AAAA,IACL,OAAO;AAAA,IACP,YAAY,CAAC,MAAM;AAAA,EAAA;AAEvB;AAEA,MAAM,gCAAgC,MAAM;AAAA,EAC1C,YAAY,gBAA4C,WAAmB;AACzE,UAAM,yBAAyB,eAAe,GAAG,oHAAoH,eAAe,GAAG;AAAA;AAAA;AAAA,kBAGzK,gBAAgB,SAAS,CAAC,4BAA4B,eAAe,KAAK;AAAA,IACxF,eAAe,KAAK;AAAA;AAAA,CAEvB;AAAA,EAAA;AAED;AAEA,MAAM,cAAc;AAEb,MAAM,oCAET,CAAC,UAAU,IAAI,EAAE,gBAAgB;AAC/B,MAAA,OAAe,QAAQ,IAAI;AAC/B,MAAI,aAAa;AAEX,QAAA,eAAe,QAAQ,IAAI,aAAa;AAE9C,QAAM,8BAA8B,MAAM;;AAEtC,aAAA,gBAAW,yBAAX,mBAAiC,oBACjC;AAAA,EAEJ;AACA,QAAM,mBAAmB,MAAM;;AAC7B,YAAO,gBAAW,yBAAX,mBAAiC;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QAC7D;AAGK,eAAA,6BAA6B,MAAM,EAAE;AAAA,MAAA;AAGvC,aAAA;AAAA,IACT;AAAA,IAEA,iBAAiB,IAAI;AACf,UAAA,CAAC,WAAW,mBAAmB;AAC1B,eAAA;AAAA,MAAA;AAIP,UAAA,wBAAwB,IAAI,WAAW,eAAe,KACtD,GAAG,SAAS,QAAQ,GACpB;AACO,eAAA;AAAA,MAAA;AAEF,aAAA;AAAA,IACT;AAAA,IAEA,MAAM;AAAA,MACJ,eAAe,QAAQ;AACrB,eAAO,OAAO;AAED,qBAAA,UAAU,SAAS,IAAI;AAAA,MAAA;AAAA,IAExC;AAAA,IAEA,OAAO,WAAW;AAChB,aAAO,QAAQ,IAAI;AACN,mBAAA,UAAU,SAAS,IAAI;AAAA,IACtC;AAAA,IAEA,QAAQ,UAAU;AAChB,aAAO,QAAQ,IAAI;AACN,mBAAA,UAAU,SAAS,IAAI;AAEpC,UACE,WAAW,qBACX,SAAS,QAAQ,SAAS,cAC1B;AACA,iBAAS,MAAM,KAAK,IAAI,aAAa,MAAM;AACjC,kBAAA,KAAK,OAAO,cAAc,wBAAwB;AAC1D,qBAAW,MAAM;AACf,oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MAAA;AAAA,IACH;AAAA,EAEJ;AACF;"}
|
|
1
|
+
{"version":3,"file":"router-code-splitter-plugin.js","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["/**\n * It is important to familiarize yourself with how the code-splitting works in this plugin.\n * https://github.com/TanStack/router/pull/3355\n */\n\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { logDiff } from '@tanstack/router-utils'\nimport { getConfig, splitGroupingsSchema } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n detectCodeSplitGroupingsFromRoute,\n} from './code-splitter/compilers'\nimport {\n defaultCodeSplitGroupings,\n splitRouteIdentNodes,\n tsrSplit,\n} from './constants'\nimport { decodeIdentifier } from './code-splitter/path-ids'\nimport { debug } from './utils'\nimport type { CodeSplitGroupings, SplitRouteIdentNodes } from './constants'\nimport type { GetRoutesByFileMapResultValue } from '@tanstack/router-generator'\nimport type { Config } from './config'\nimport type {\n UnpluginContextMeta,\n UnpluginFactory,\n TransformResult as UnpluginTransformResult,\n} from 'unplugin'\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(\n externalPlugin: BannedBeforeExternalPlugin,\n pluginFramework: string,\n ) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin/${pluginFramework}'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n tanstackRouter(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const isProduction = process.env.NODE_ENV === 'production'\n\n const getGlobalCodeSplitGroupings = () => {\n return (\n userConfig.codeSplittingOptions?.defaultBehavior ||\n defaultCodeSplitGroupings\n )\n }\n const getShouldSplitFn = () => {\n return userConfig.codeSplittingOptions?.splitBehavior\n }\n\n const handleCompilingReferenceFile = (\n code: string,\n id: string,\n generatorNodeInfo: GetRoutesByFileMapResultValue,\n ): UnpluginTransformResult => {\n if (debug) console.info('Compiling Route: ', id)\n\n const fromCode = detectCodeSplitGroupingsFromRoute({\n code,\n })\n\n if (fromCode.groupings) {\n const res = splitGroupingsSchema.safeParse(fromCode.groupings)\n if (!res.success) {\n const message = res.error.errors.map((e) => e.message).join('. ')\n throw new Error(\n `The groupings for the route \"${id}\" are invalid.\\n${message}`,\n )\n }\n }\n\n const userShouldSplitFn = getShouldSplitFn()\n\n const pluginSplitBehavior = userShouldSplitFn?.({\n routeId: generatorNodeInfo.routePath,\n }) as CodeSplitGroupings | undefined\n\n if (pluginSplitBehavior) {\n const res = splitGroupingsSchema.safeParse(pluginSplitBehavior)\n if (!res.success) {\n const message = res.error.errors.map((e) => e.message).join('. ')\n throw new Error(\n `The groupings returned when using \\`splitBehavior\\` for the route \"${id}\" are invalid.\\n${message}`,\n )\n }\n }\n\n const splitGroupings: CodeSplitGroupings =\n fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings()\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n runtimeEnv: isProduction ? 'prod' : 'dev',\n codeSplitGroupings: splitGroupings,\n targetFramework: userConfig.target,\n filename: id,\n id,\n })\n\n if (debug) {\n logDiff(code, compiledReferenceRoute.code)\n console.log('Output:\\n', compiledReferenceRoute.code + '\\n\\n')\n }\n\n return compiledReferenceRoute\n }\n\n const handleCompilingVirtualFile = (\n code: string,\n id: string,\n ): UnpluginTransformResult => {\n if (debug) console.info('Splitting Route: ', id)\n\n const [_, ...pathnameParts] = id.split('?')\n\n const searchParams = new URLSearchParams(pathnameParts.join('?'))\n const splitValue = searchParams.get(tsrSplit)\n\n if (!splitValue) {\n throw new Error(\n `The split value for the virtual route \"${id}\" was not found.`,\n )\n }\n\n const rawGrouping = decodeIdentifier(splitValue)\n const grouping = [...new Set(rawGrouping)].filter((p) =>\n splitRouteIdentNodes.includes(p as any),\n ) as Array<SplitRouteIdentNodes>\n\n const result = compileCodeSplitVirtualRoute({\n code,\n filename: id,\n splitTargets: grouping,\n })\n\n if (debug) {\n logDiff(code, result.code)\n console.log('Output:\\n', result.code + '\\n\\n')\n }\n\n return result\n }\n\n return [\n {\n name: 'tanstack-router:code-splitter:compile-reference-file',\n enforce: 'pre',\n\n transform: {\n filter: {\n id: {\n exclude: tsrSplit,\n },\n code: 'createFileRoute(',\n },\n handler(code, id) {\n const generatorFileInfo = globalThis.TSR_ROUTES_BY_ID_MAP?.get(id)\n if (generatorFileInfo && code.includes('createFileRoute(')) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingReferenceFile(code, id, generatorFileInfo)\n }\n\n return null\n },\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack() {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n\n if (compiler.options.mode === 'production') {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n },\n {\n name: 'tanstack-router:code-splitter:compile-virtual-file',\n enforce: 'pre',\n\n transform: {\n filter: {\n id: /tsr-split/,\n },\n handler(code, id) {\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n return handleCompilingVirtualFile(code, id)\n },\n },\n },\n ]\n}\n"],"names":[],"mappings":";;;;;;;AAoCA,MAAM,8BAAiE;AAAA,EACrE;AAAA,IACE,YAAY;AAAA,IACZ,KAAK;AAAA,IACL,OAAO;AAAA,IACP,YAAY,CAAC,MAAM;AAAA,EAAA;AAEvB;AAEA,MAAM,gCAAgC,MAAM;AAAA,EAC1C,YACE,gBACA,iBACA;AACA,UAAM,yBAAyB,eAAe,GAAG,gDAAgD,eAAe,wEAAwE,eAAe,GAAG;AAAA;AAAA;AAAA,2CAGnK,eAAe,KAAK;AAAA,IAC3D,eAA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outerComposedFactory = (options = {}, meta) => {
|
|
|
13
13
|
const routerGenerator = getPlugin(unpluginRouterGeneratorFactory);
|
|
14
14
|
const routerCodeSplitter = getPlugin(unpluginRouterCodeSplitterFactory);
|
|
15
15
|
const routeAutoImport = getPlugin(unpluginRouteAutoImportFactory);
|
|
16
|
-
const result = [...routerGenerator
|
|
16
|
+
const result = [...routerGenerator];
|
|
17
|
+
if (options.autoCodeSplitting) {
|
|
18
|
+
result.push(...routerCodeSplitter);
|
|
19
|
+
}
|
|
20
|
+
if (options.verboseFileRoutes === false) {
|
|
21
|
+
result.push(...routeAutoImport);
|
|
22
|
+
}
|
|
17
23
|
const isProduction = process.env.NODE_ENV === "production";
|
|
18
24
|
if (!isProduction && !options.autoCodeSplitting) {
|
|
19
25
|
const routerHmr = getPlugin(unpluginRouterHmrFactory);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-composed-plugin.js","sources":["../../../src/core/router-composed-plugin.ts"],"sourcesContent":["import { unpluginRouterGeneratorFactory } from './router-generator-plugin'\nimport { unpluginRouterCodeSplitterFactory } from './router-code-splitter-plugin'\nimport { unpluginRouterHmrFactory } from './router-hmr-plugin'\nimport { unpluginRouteAutoImportFactory } from './route-autoimport-plugin'\nimport type { Config } from './config'\nimport type { UnpluginFactory } from 'unplugin'\n\nexport const unpluginRouterComposedFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, meta) => {\n const getPlugin = (pluginFactory: UnpluginFactory<Partial<Config>>) => {\n const plugin = pluginFactory(options, meta)\n if (!Array.isArray(plugin)) {\n return [plugin]\n }\n return plugin\n }\n\n const routerGenerator = getPlugin(unpluginRouterGeneratorFactory)\n const routerCodeSplitter = getPlugin(unpluginRouterCodeSplitterFactory)\n const routeAutoImport = getPlugin(unpluginRouteAutoImportFactory)\n\n const result = [...routerGenerator
|
|
1
|
+
{"version":3,"file":"router-composed-plugin.js","sources":["../../../src/core/router-composed-plugin.ts"],"sourcesContent":["import { unpluginRouterGeneratorFactory } from './router-generator-plugin'\nimport { unpluginRouterCodeSplitterFactory } from './router-code-splitter-plugin'\nimport { unpluginRouterHmrFactory } from './router-hmr-plugin'\nimport { unpluginRouteAutoImportFactory } from './route-autoimport-plugin'\nimport type { Config } from './config'\nimport type { UnpluginFactory } from 'unplugin'\n\nexport const unpluginRouterComposedFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, meta) => {\n const getPlugin = (pluginFactory: UnpluginFactory<Partial<Config>>) => {\n const plugin = pluginFactory(options, meta)\n if (!Array.isArray(plugin)) {\n return [plugin]\n }\n return plugin\n }\n\n const routerGenerator = getPlugin(unpluginRouterGeneratorFactory)\n const routerCodeSplitter = getPlugin(unpluginRouterCodeSplitterFactory)\n const routeAutoImport = getPlugin(unpluginRouteAutoImportFactory)\n\n const result = [...routerGenerator]\n if (options.autoCodeSplitting) {\n result.push(...routerCodeSplitter)\n }\n if (options.verboseFileRoutes === false) {\n result.push(...routeAutoImport)\n }\n\n const isProduction = process.env.NODE_ENV === 'production'\n\n if (!isProduction && !options.autoCodeSplitting) {\n const routerHmr = getPlugin(unpluginRouterHmrFactory)\n result.push(...routerHmr)\n }\n return result\n}\n"],"names":[],"mappings":";;;;AAOO,MAAM,gCAET,CAAC,UAAU,IAAI,SAAS;AACpB,QAAA,YAAY,CAAC,kBAAoD;AAC/D,UAAA,SAAS,cAAc,SAAS,IAAI;AAC1C,QAAI,CAAC,MAAM,QAAQ,MAAM,GAAG;AAC1B,aAAO,CAAC,MAAM;AAAA,IAAA;AAET,WAAA;AAAA,EACT;AAEM,QAAA,kBAAkB,UAAU,8BAA8B;AAC1D,QAAA,qBAAqB,UAAU,iCAAiC;AAChE,QAAA,kBAAkB,UAAU,8BAA8B;AAE1D,QAAA,SAAS,CAAC,GAAG,eAAe;AAClC,MAAI,QAAQ,mBAAmB;AACtB,WAAA,KAAK,GAAG,kBAAkB;AAAA,EAAA;AAE/B,MAAA,QAAQ,sBAAsB,OAAO;AAChC,WAAA,KAAK,GAAG,eAAe;AAAA,EAAA;AAG1B,QAAA,eAAe,QAAQ,IAAI,aAAa;AAE9C,MAAI,CAAC,gBAAgB,CAAC,QAAQ,mBAAmB;AACzC,UAAA,YAAY,UAAU,wBAAwB;AAC7C,WAAA,KAAK,GAAG,SAAS;AAAA,EAAA;AAEnB,SAAA;AACT;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join, isAbsolute
|
|
1
|
+
import { normalize, join, isAbsolute } from "node:path";
|
|
2
2
|
import { Generator, resolveConfigPath } from "@tanstack/router-generator";
|
|
3
3
|
import { getConfig } from "./config.js";
|
|
4
4
|
const PLUGIN_NAME = "unplugin:router-generator";
|
|
@@ -17,35 +17,34 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
17
17
|
root: ROOT
|
|
18
18
|
});
|
|
19
19
|
};
|
|
20
|
-
const generate = async () => {
|
|
20
|
+
const generate = async (opts) => {
|
|
21
21
|
if (routeGenerationDisabled()) {
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (filePath === resolveConfigPath({ configDirectory: ROOT })) {
|
|
33
|
-
initConfigAndGenerator();
|
|
34
|
-
return;
|
|
24
|
+
let generatorEvent = void 0;
|
|
25
|
+
if (opts) {
|
|
26
|
+
const filePath = normalize(opts.file);
|
|
27
|
+
if (filePath === resolveConfigPath({ configDirectory: ROOT })) {
|
|
28
|
+
initConfigAndGenerator();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
generatorEvent = { path: filePath, type: opts.event };
|
|
35
32
|
}
|
|
36
33
|
try {
|
|
37
|
-
await generator.run(
|
|
34
|
+
await generator.run(generatorEvent);
|
|
35
|
+
globalThis.TSR_ROUTES_BY_ID_MAP = generator.getRoutesByFileMap();
|
|
38
36
|
} catch (e) {
|
|
39
37
|
console.error(e);
|
|
40
38
|
}
|
|
41
39
|
};
|
|
42
40
|
return {
|
|
43
|
-
name: "router-generator
|
|
41
|
+
name: "tanstack:router-generator",
|
|
44
42
|
enforce: "pre",
|
|
45
43
|
async watchChange(id, { event }) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
await generate({
|
|
45
|
+
file: id,
|
|
46
|
+
event
|
|
47
|
+
});
|
|
49
48
|
},
|
|
50
49
|
async buildStart() {
|
|
51
50
|
await generate();
|
|
@@ -65,7 +64,7 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
65
64
|
rspack(compiler) {
|
|
66
65
|
initConfigAndGenerator();
|
|
67
66
|
let handle = null;
|
|
68
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate);
|
|
67
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate());
|
|
69
68
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
70
69
|
if (handle) {
|
|
71
70
|
return;
|
|
@@ -84,7 +83,7 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
84
83
|
webpack(compiler) {
|
|
85
84
|
initConfigAndGenerator();
|
|
86
85
|
let handle = null;
|
|
87
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate);
|
|
86
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate());
|
|
88
87
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
89
88
|
if (handle) {
|
|
90
89
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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'\
|
|
1
|
+
{"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> | undefined\n> = (options = {}) => {\n const ROOT: string = process.cwd()\n let userConfig = options as 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 = () => {\n userConfig = getConfig(options, ROOT)\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 async buildStart() {\n await generate()\n },\n vite: {\n configResolved() {\n initConfigAndGenerator()\n },\n async buildStart() {\n if (this.environment.config.consumer === 'server') {\n // When building in environment mode, we only need to generate routes\n // for the client environment\n return\n }\n await generate()\n },\n sharedDuringBuild: true,\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', generate)\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', generate)\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 }\n}\n"],"names":[],"mappings":";;;AASA,MAAM,cAAc;AAEb,MAAM,iCAET,CAAC,UAAU,OAAO;AACd,QAAA,OAAe,QAAQ,IAAI;AACjC,MAAI,aAAa;AACb,MAAA;AAEE,QAAA,0BAA0B,MAC9B,WAAW,0BAA0B;AACvC,QAAM,yBAAyB,MAAM;AAC5B,WAAA,WAAW,WAAW,eAAe,IACxC,WAAW,kBACX,KAAK,MAAM,WAAW,eAAe;AAAA,EAC3C;AAEA,QAAM,yBAAyB,MAAM;AACtB,iBAAA,UAAU,SAAS,IAAI;AACpC,gBAAY,IAAI,UAAU;AAAA,MACxB,QAAQ;AAAA,MACR,MAAM;AAAA,IAAA,CACP;AAAA,EACH;AAEM,QAAA,WAAW,OAAO,SAGlB;AACJ,QAAI,2BAA2B;AAC7B;AAAA,IAAA;AAEF,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MAAM;AAAA,QAAA;AAAA,MACrB,CACD;AAAA,IACH;AAAA,IACA,QAAQ,UAAU;AACO,6BAAA;AAEvB,UAAI,SAA2B;AAE/B,eAAS,MAAM,UAAU,WAAW,aAAa,MAAM,UAAU;AAEjE,eAAS,MAAM,SAAS,WAAW,aAAa,YAAY;AAC1D,YAAI,QAAQ;AACV;AAAA,QAAA;AAIF,cAAM,sBAAsB,uBAAuB;AAC7C,cAAA,WAAW,MAAM,OAAO,UAAU;AAC/B,iBAAA,SACN,MAAM,qBAAqB,EAAE,eAAe,MAAM,EAClD,GAAG,OAAO,QAAQ;AAErB,cAAM,SAAS;AAAA,MAAA,CAChB;AAED,eAAS,MAAM,WAAW,IAAI,aAAa,YAAY;AACrD,YAAI,QAAQ;AACV,gBAAM,OAAO,MAAM;AAAA,QAAA;AAAA,MACrB,CACD;AAED,eAAS,MAAM,KAAK,IAAI,aAAa,MAAM;AACjC,gBAAA,KAAK,OAAO,cAAc,8BAA8B;AAAA,MAAA,CACjE;AAAA,IAAA;AAAA,EAEL;AACF;"}
|
|
@@ -1,47 +1,49 @@
|
|
|
1
1
|
import { parseAst, generateFromAst, logDiff } from "@tanstack/router-utils";
|
|
2
2
|
import { getConfig } from "./config.js";
|
|
3
3
|
import { routeHmrStatement } from "./route-hmr-statement.js";
|
|
4
|
-
import {
|
|
4
|
+
import { debug } from "./utils.js";
|
|
5
5
|
const unpluginRouterHmrFactory = (options = {}) => {
|
|
6
6
|
let ROOT = process.cwd();
|
|
7
|
-
let userConfig = options;
|
|
8
7
|
return {
|
|
9
|
-
name: "router
|
|
8
|
+
name: "tanstack-router:hmr",
|
|
10
9
|
enforce: "pre",
|
|
11
|
-
transform
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
transform: {
|
|
11
|
+
filter: {
|
|
12
|
+
code: "createFileRoute("
|
|
13
|
+
},
|
|
14
|
+
handler(code, id) {
|
|
15
|
+
var _a;
|
|
16
|
+
if (!((_a = globalThis.TSR_ROUTES_BY_ID_MAP) == null ? void 0 : _a.has(id))) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
if (debug) console.info("Adding HMR handling to route ", id);
|
|
20
|
+
const ast = parseAst({ code });
|
|
21
|
+
ast.program.body.push(routeHmrStatement);
|
|
22
|
+
const result = generateFromAst(ast, {
|
|
23
|
+
sourceMaps: true,
|
|
24
|
+
filename: id,
|
|
25
|
+
sourceFileName: id
|
|
26
|
+
});
|
|
27
|
+
if (debug) {
|
|
28
|
+
logDiff(code, result.code);
|
|
29
|
+
console.log("Output:\n", result.code + "\n\n");
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
14
32
|
}
|
|
15
|
-
if (debug) console.info("Adding HMR handling to route ", id);
|
|
16
|
-
const ast = parseAst({ code });
|
|
17
|
-
ast.program.body.push(routeHmrStatement);
|
|
18
|
-
const result = generateFromAst(ast, {
|
|
19
|
-
sourceMaps: true,
|
|
20
|
-
filename: id,
|
|
21
|
-
sourceFileName: id
|
|
22
|
-
});
|
|
23
|
-
if (debug) {
|
|
24
|
-
logDiff(code, result.code);
|
|
25
|
-
console.log("Output:\n", result.code + "\n\n");
|
|
26
|
-
}
|
|
27
|
-
return result;
|
|
28
|
-
},
|
|
29
|
-
transformInclude(id) {
|
|
30
|
-
return fileIsInRoutesDirectory(id, userConfig.routesDirectory);
|
|
31
33
|
},
|
|
32
34
|
vite: {
|
|
33
35
|
configResolved(config) {
|
|
34
36
|
ROOT = config.root;
|
|
35
|
-
|
|
37
|
+
getConfig(options, ROOT);
|
|
36
38
|
}
|
|
37
39
|
},
|
|
38
40
|
rspack() {
|
|
39
41
|
ROOT = process.cwd();
|
|
40
|
-
|
|
42
|
+
getConfig(options, ROOT);
|
|
41
43
|
},
|
|
42
44
|
webpack() {
|
|
43
45
|
ROOT = process.cwd();
|
|
44
|
-
|
|
46
|
+
getConfig(options, ROOT);
|
|
45
47
|
}
|
|
46
48
|
};
|
|
47
49
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-hmr-plugin.js","sources":["../../../src/core/router-hmr-plugin.ts"],"sourcesContent":["import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'\nimport { getConfig } from './config'\nimport { routeHmrStatement } from './route-hmr-statement'\nimport { debug
|
|
1
|
+
{"version":3,"file":"router-hmr-plugin.js","sources":["../../../src/core/router-hmr-plugin.ts"],"sourcesContent":["import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'\nimport { getConfig } from './config'\nimport { routeHmrStatement } from './route-hmr-statement'\nimport { debug } from './utils'\nimport type { Config } from './config'\nimport type { UnpluginFactory } from 'unplugin'\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 */\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\n transform: {\n filter: {\n code: 'createFileRoute(',\n },\n handler(code, id) {\n if (!globalThis.TSR_ROUTES_BY_ID_MAP?.has(id)) {\n return null\n }\n\n if (debug) console.info('Adding HMR handling to route ', id)\n\n const ast = parseAst({ code })\n ast.program.body.push(routeHmrStatement)\n const result = generateFromAst(ast, {\n sourceMaps: true,\n filename: id,\n sourceFileName: id,\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\n vite: {\n configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack() {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n },\n\n webpack() {\n ROOT = process.cwd()\n userConfig = getConfig(options, ROOT)\n },\n }\n}\n"],"names":[],"mappings":";;;;AAYO,MAAM,2BAET,CAAC,UAAU,OAAO;AAChB,MAAA,OAAe,QAAQ,IAAI;AAGxB,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,WAAW;AAAA,MACT,QAAQ;AAAA,QACN,MAAM;AAAA,MACR;AAAA,MACA,QAAQ,MAAM,IAAI;;AAChB,YAAI,GAAC,gBAAW,yBAAX,mBAAiC,IAAI,MAAK;AACtC,iBAAA;AAAA,QAAA;AAGT,YAAI,MAAO,SAAQ,KAAK,iCAAiC,EAAE;AAE3D,cAAM,MAAM,SAAS,EAAE,MAAM;AACzB,YAAA,QAAQ,KAAK,KAAK,iBAAiB;AACjC,cAAA,SAAS,gBAAgB,KAAK;AAAA,UAClC,YAAY;AAAA,UACZ,UAAU;AAAA,UACV,gBAAgB;AAAA,QAAA,CACjB;AACD,YAAI,OAAO;AACD,kBAAA,MAAM,OAAO,IAAI;AACzB,kBAAQ,IAAI,aAAa,OAAO,OAAO,MAAM;AAAA,QAAA;AAExC,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IAEA,MAAM;AAAA,MACJ,eAAe,QAAQ;AACrB,eAAO,OAAO;AACD,kBAAU,SAAS,IAAI;AAAA,MAAA;AAAA,IAExC;AAAA,IAEA,SAAS;AACP,aAAO,QAAQ,IAAI;AACN,gBAAU,SAAS,IAAI;AAAA,IACtC;AAAA,IAEA,UAAU;AACR,aAAO,QAAQ,IAAI;AACN,gBAAU,SAAS,IAAI;AAAA,IAAA;AAAA,EAExC;AACF;"}
|
package/dist/esm/core/utils.d.ts
CHANGED
package/dist/esm/core/utils.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import { isAbsolute, join, normalize } from "node:path";
|
|
2
1
|
const debug = process.env.TSR_VITE_DEBUG && ["true", "router-plugin"].includes(process.env.TSR_VITE_DEBUG);
|
|
3
|
-
function fileIsInRoutesDirectory(filePath, routesDirectory) {
|
|
4
|
-
const routesDirectoryPath = isAbsolute(routesDirectory) ? routesDirectory : join(process.cwd(), routesDirectory);
|
|
5
|
-
const path = normalize(filePath);
|
|
6
|
-
return path.startsWith(routesDirectoryPath);
|
|
7
|
-
}
|
|
8
2
|
export {
|
|
9
|
-
debug
|
|
10
|
-
fileIsInRoutesDirectory
|
|
3
|
+
debug
|
|
11
4
|
};
|
|
12
5
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../../../src/core/utils.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../../src/core/utils.ts"],"sourcesContent":["export const debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'router-plugin'].includes(process.env.TSR_VITE_DEBUG)\n"],"names":[],"mappings":"AAAa,MAAA,QACX,QAAQ,IAAI,kBACZ,CAAC,QAAQ,eAAe,EAAE,SAAS,QAAQ,IAAI,cAAc;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/router-plugin",
|
|
3
|
-
"version": "1.121.
|
|
3
|
+
"version": "1.121.9",
|
|
4
4
|
"description": "Modern and scalable routing for React applications",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -98,9 +98,9 @@
|
|
|
98
98
|
"unplugin": "^2.1.2",
|
|
99
99
|
"zod": "^3.24.2",
|
|
100
100
|
"@tanstack/router-core": "^1.121.2",
|
|
101
|
-
"@tanstack/router-generator": "^1.121.6",
|
|
102
101
|
"@tanstack/router-utils": "^1.121.0",
|
|
103
|
-
"@tanstack/virtual-file-routes": "^1.120.17"
|
|
102
|
+
"@tanstack/virtual-file-routes": "^1.120.17",
|
|
103
|
+
"@tanstack/router-generator": "^1.121.9"
|
|
104
104
|
},
|
|
105
105
|
"devDependencies": {
|
|
106
106
|
"@types/babel__core": "^7.20.5",
|
|
@@ -706,12 +706,9 @@ export function compileCodeSplitVirtualRoute(
|
|
|
706
706
|
*/
|
|
707
707
|
export function detectCodeSplitGroupingsFromRoute(opts: ParseAstOptions): {
|
|
708
708
|
groupings: CodeSplitGroupings | undefined
|
|
709
|
-
routeId: string
|
|
710
709
|
} {
|
|
711
710
|
const ast = parseAst(opts)
|
|
712
711
|
|
|
713
|
-
let routeId = ''
|
|
714
|
-
|
|
715
712
|
let codeSplitGroupings: CodeSplitGroupings | undefined = undefined
|
|
716
713
|
|
|
717
714
|
babel.traverse(ast, {
|
|
@@ -732,26 +729,11 @@ export function detectCodeSplitGroupingsFromRoute(opts: ParseAstOptions): {
|
|
|
732
729
|
return
|
|
733
730
|
}
|
|
734
731
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
if (t.isIdentifier(callee.callee)) {
|
|
741
|
-
const firstArg = callee.arguments[0]
|
|
742
|
-
if (t.isStringLiteral(firstArg)) {
|
|
743
|
-
routeId = firstArg.value
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
// Extracting the codeSplitGroupings
|
|
749
|
-
const options = resolveIdentifier(
|
|
750
|
-
path,
|
|
751
|
-
path.parentPath.node.arguments[0],
|
|
752
|
-
)
|
|
753
|
-
if (t.isObjectExpression(options)) {
|
|
754
|
-
options.properties.forEach((prop) => {
|
|
732
|
+
function babelHandleSplittingGroups(
|
|
733
|
+
routeOptions: t.Node | undefined,
|
|
734
|
+
) {
|
|
735
|
+
if (t.isObjectExpression(routeOptions)) {
|
|
736
|
+
routeOptions.properties.forEach((prop) => {
|
|
755
737
|
if (t.isObjectProperty(prop)) {
|
|
756
738
|
if (t.isIdentifier(prop.key)) {
|
|
757
739
|
if (prop.key.name === 'codeSplitGroupings') {
|
|
@@ -786,13 +768,32 @@ export function detectCodeSplitGroupingsFromRoute(opts: ParseAstOptions): {
|
|
|
786
768
|
})
|
|
787
769
|
}
|
|
788
770
|
}
|
|
771
|
+
|
|
772
|
+
// Extracting the codeSplitGroupings
|
|
773
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
774
|
+
// createFileRoute('/')({ ... })
|
|
775
|
+
const options = resolveIdentifier(
|
|
776
|
+
path,
|
|
777
|
+
path.parentPath.node.arguments[0],
|
|
778
|
+
)
|
|
779
|
+
|
|
780
|
+
babelHandleSplittingGroups(options)
|
|
781
|
+
} else if (t.isVariableDeclarator(path.parentPath.node)) {
|
|
782
|
+
// createFileRoute({ ... })
|
|
783
|
+
const caller = resolveIdentifier(path, path.parentPath.node.init)
|
|
784
|
+
|
|
785
|
+
if (t.isCallExpression(caller)) {
|
|
786
|
+
const options = resolveIdentifier(path, caller.arguments[0])
|
|
787
|
+
babelHandleSplittingGroups(options)
|
|
788
|
+
}
|
|
789
|
+
}
|
|
789
790
|
},
|
|
790
791
|
})
|
|
791
792
|
},
|
|
792
793
|
},
|
|
793
794
|
})
|
|
794
795
|
|
|
795
|
-
return { groupings: codeSplitGroupings
|
|
796
|
+
return { groupings: codeSplitGroupings }
|
|
796
797
|
}
|
|
797
798
|
|
|
798
799
|
function getImportSpecifierAndPathFromLocalName(
|
|
@@ -2,7 +2,7 @@ import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'
|
|
|
2
2
|
import babel from '@babel/core'
|
|
3
3
|
import * as template from '@babel/template'
|
|
4
4
|
import { getConfig } from './config'
|
|
5
|
-
import { debug
|
|
5
|
+
import { debug } from './utils'
|
|
6
6
|
import type { Config } from './config'
|
|
7
7
|
import type { UnpluginFactory } from 'unplugin'
|
|
8
8
|
|
|
@@ -16,70 +16,74 @@ export const unpluginRouteAutoImportFactory: UnpluginFactory<
|
|
|
16
16
|
let userConfig = options as Config
|
|
17
17
|
|
|
18
18
|
return {
|
|
19
|
-
name: 'router
|
|
19
|
+
name: 'tanstack-router:autoimport',
|
|
20
20
|
enforce: 'pre',
|
|
21
21
|
|
|
22
|
-
transform
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
transform: {
|
|
23
|
+
filter: {
|
|
24
|
+
code: /createFileRoute\(|createLazyFileRoute\(/,
|
|
25
|
+
},
|
|
26
|
+
handler(code, id) {
|
|
27
|
+
if (!globalThis.TSR_ROUTES_BY_ID_MAP?.has(id)) {
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
let routeType: 'createFileRoute' | 'createLazyFileRoute'
|
|
31
|
+
if (code.includes('createFileRoute(')) {
|
|
32
|
+
routeType = 'createFileRoute'
|
|
33
|
+
} else if (code.includes('createLazyFileRoute(')) {
|
|
34
|
+
routeType = 'createLazyFileRoute'
|
|
35
|
+
} else {
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
31
38
|
|
|
32
|
-
|
|
39
|
+
const routerImportPath = `@tanstack/${userConfig.target}-router`
|
|
33
40
|
|
|
34
|
-
|
|
41
|
+
const ast = parseAst({ code })
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
let isCreateRouteFunctionImported = false as boolean
|
|
37
44
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
babel.traverse(ast, {
|
|
46
|
+
Program: {
|
|
47
|
+
enter(programPath) {
|
|
48
|
+
programPath.traverse({
|
|
49
|
+
ImportDeclaration(path) {
|
|
50
|
+
const importedSpecifiers = path.node.specifiers.map(
|
|
51
|
+
(specifier) => specifier.local.name,
|
|
52
|
+
)
|
|
53
|
+
if (
|
|
54
|
+
importedSpecifiers.includes(routeType) &&
|
|
55
|
+
path.node.source.value === routerImportPath
|
|
56
|
+
) {
|
|
57
|
+
isCreateRouteFunctionImported = true
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
},
|
|
54
62
|
},
|
|
55
|
-
}
|
|
56
|
-
})
|
|
63
|
+
})
|
|
57
64
|
|
|
58
|
-
|
|
59
|
-
|
|
65
|
+
if (!isCreateRouteFunctionImported) {
|
|
66
|
+
if (debug) console.info('Adding autoimports to route ', id)
|
|
60
67
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
const autoImportStatement = template.statement(
|
|
69
|
+
`import { ${routeType} } from '${routerImportPath}'`,
|
|
70
|
+
)()
|
|
71
|
+
ast.program.body.unshift(autoImportStatement)
|
|
65
72
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
const result = generateFromAst(ast, {
|
|
74
|
+
sourceMaps: true,
|
|
75
|
+
filename: id,
|
|
76
|
+
sourceFileName: id,
|
|
77
|
+
})
|
|
78
|
+
if (debug) {
|
|
79
|
+
logDiff(code, result.code)
|
|
80
|
+
console.log('Output:\n', result.code + '\n\n')
|
|
81
|
+
}
|
|
82
|
+
return result
|
|
74
83
|
}
|
|
75
|
-
return result
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return null
|
|
79
|
-
},
|
|
80
84
|
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
return null
|
|
86
|
+
},
|
|
83
87
|
},
|
|
84
88
|
|
|
85
89
|
vite: {
|