@tanstack/router-generator 1.99.9 → 1.99.14
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/config.cjs +5 -18
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/config.d.cts +18 -10
- package/dist/cjs/generator.cjs +57 -51
- package/dist/cjs/generator.cjs.map +1 -1
- package/dist/cjs/template.cjs +72 -0
- package/dist/cjs/template.cjs.map +1 -0
- package/dist/cjs/template.d.cts +34 -0
- package/dist/esm/config.d.ts +18 -10
- package/dist/esm/config.js +5 -18
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/generator.js +57 -51
- package/dist/esm/generator.js.map +1 -1
- package/dist/esm/template.d.ts +34 -0
- package/dist/esm/template.js +72 -0
- package/dist/esm/template.js.map +1 -0
- package/package.json +2 -2
- package/src/config.ts +5 -22
- package/src/generator.ts +69 -57
- package/src/template.ts +111 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function fillTemplate(template, values) {
|
|
4
|
+
return template.replace(
|
|
5
|
+
/%%(\w+)%%/g,
|
|
6
|
+
(_, key) => values[key] || ""
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
function getTargetTemplate(target) {
|
|
10
|
+
switch (target) {
|
|
11
|
+
// TODO: Remove this disabled eslint rule when more target types are added.
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
13
|
+
case "react":
|
|
14
|
+
return {
|
|
15
|
+
fullPkg: "@tanstack/react-router",
|
|
16
|
+
subPkg: "react-router",
|
|
17
|
+
rootRoute: {
|
|
18
|
+
template: () => [
|
|
19
|
+
'import * as React from "react"\n',
|
|
20
|
+
"%%tsrImports%%",
|
|
21
|
+
"\n\n",
|
|
22
|
+
"%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n",
|
|
23
|
+
'function RootComponent() { return (<React.Fragment><div>Hello "%%tsrPath%%"!</div><Outlet /></React.Fragment>) };\n'
|
|
24
|
+
].join(""),
|
|
25
|
+
imports: {
|
|
26
|
+
tsrImports: () => "import { Outlet, createRootRoute } from '@tanstack/react-router';",
|
|
27
|
+
tsrExportStart: () => "export const Route = createRootRoute(",
|
|
28
|
+
tsrExportEnd: () => ");"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
route: {
|
|
32
|
+
template: () => [
|
|
33
|
+
"%%tsrImports%%",
|
|
34
|
+
"\n\n",
|
|
35
|
+
"%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n",
|
|
36
|
+
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
37
|
+
].join(""),
|
|
38
|
+
imports: {
|
|
39
|
+
tsrImports: () => "import { createFileRoute } from '@tanstack/react-router';",
|
|
40
|
+
tsrExportStart: (routePath) => `export const Route = createFileRoute('${routePath}')(`,
|
|
41
|
+
tsrExportEnd: () => ");"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
lazyRoute: {
|
|
45
|
+
template: () => [
|
|
46
|
+
"%%tsrImports%%",
|
|
47
|
+
"\n\n",
|
|
48
|
+
"%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n",
|
|
49
|
+
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
50
|
+
].join(""),
|
|
51
|
+
imports: {
|
|
52
|
+
tsrImports: () => "import { createLazyFileRoute } from '@tanstack/react-router';",
|
|
53
|
+
tsrExportStart: (routePath) => `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
54
|
+
tsrExportEnd: () => ");"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
default:
|
|
59
|
+
throw new Error(`router-generator: Unknown target type: ${target}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const defaultAPIRouteTemplate = [
|
|
63
|
+
'import { json } from "@tanstack/start";\n',
|
|
64
|
+
"%%tsrImports%%",
|
|
65
|
+
"\n\n",
|
|
66
|
+
`%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:'Hello "%%tsrPath%%"!' }) }}%%tsrExportEnd%%
|
|
67
|
+
`
|
|
68
|
+
].join("");
|
|
69
|
+
exports.defaultAPIRouteTemplate = defaultAPIRouteTemplate;
|
|
70
|
+
exports.fillTemplate = fillTemplate;
|
|
71
|
+
exports.getTargetTemplate = getTargetTemplate;
|
|
72
|
+
//# sourceMappingURL=template.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.cjs","sources":["../../src/template.ts"],"sourcesContent":["import type { Config } from './config'\n\ntype TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'\n\nexport function fillTemplate(\n template: string,\n values: Record<TemplateTag, string>,\n) {\n return template.replace(\n /%%(\\w+)%%/g,\n (_, key) => values[key as TemplateTag] || '',\n )\n}\n\ntype TargetTemplate = {\n fullPkg: string\n subPkg: string\n rootRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: () => string\n tsrExportEnd: () => string\n }\n }\n route: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n lazyRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n}\n\nexport function getTargetTemplate(target: Config['target']): TargetTemplate {\n switch (target) {\n // TODO: Remove this disabled eslint rule when more target types are added.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n case 'react':\n return {\n fullPkg: '@tanstack/react-router',\n subPkg: 'react-router',\n rootRoute: {\n template: () =>\n [\n 'import * as React from \"react\"\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RootComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RootComponent() { return (<React.Fragment><div>Hello \"%%tsrPath%%\"!</div><Outlet /></React.Fragment>) };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { Outlet, createRootRoute } from '@tanstack/react-router';\",\n tsrExportStart: () => 'export const Route = createRootRoute(',\n tsrExportEnd: () => ');',\n },\n },\n route: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { createFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n `export const Route = createFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n lazyRoute: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { createLazyFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n `export const Route = createLazyFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n }\n default:\n throw new Error(`router-generator: Unknown target type: ${target}`)\n }\n}\n\nexport const defaultAPIRouteTemplate = [\n 'import { json } from \"@tanstack/start\";\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:\\'Hello \"%%tsrPath%%\"!\\' }) }}%%tsrExportEnd%%\\n',\n].join('')\n"],"names":[],"mappings":";;AAIgB,SAAA,aACd,UACA,QACA;AACA,SAAO,SAAS;AAAA,IACd;AAAA,IACA,CAAC,GAAG,QAAQ,OAAO,GAAkB,KAAK;AAAA,EAC5C;AACF;AA+BO,SAAS,kBAAkB,QAA0C;AAC1E,UAAQ,QAAQ;AAAA;AAAA;AAAA,IAGd,KAAK;AACI,aAAA;AAAA,QACL,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV;AAAA,YACF,gBAAgB,MAAM;AAAA,YACtB,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,OAAO;AAAA,UACL,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV;AAAA,YACF,gBAAgB,CAAC,cACf,yCAAyC,SAAS;AAAA,YACpD,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV;AAAA,YACF,gBAAgB,CAAC,cACf,6CAA6C,SAAS;AAAA,YACxD,cAAc,MAAM;AAAA,UAAA;AAAA,QACtB;AAAA,MAEJ;AAAA,IACF;AACE,YAAM,IAAI,MAAM,0CAA0C,MAAM,EAAE;AAAA,EAAA;AAExE;AAEO,MAAM,0BAA0B;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AACF,EAAE,KAAK,EAAE;;;;"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Config } from './config.cjs';
|
|
2
|
+
type TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd';
|
|
3
|
+
export declare function fillTemplate(template: string, values: Record<TemplateTag, string>): string;
|
|
4
|
+
type TargetTemplate = {
|
|
5
|
+
fullPkg: string;
|
|
6
|
+
subPkg: string;
|
|
7
|
+
rootRoute: {
|
|
8
|
+
template: () => string;
|
|
9
|
+
imports: {
|
|
10
|
+
tsrImports: () => string;
|
|
11
|
+
tsrExportStart: () => string;
|
|
12
|
+
tsrExportEnd: () => string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
route: {
|
|
16
|
+
template: () => string;
|
|
17
|
+
imports: {
|
|
18
|
+
tsrImports: () => string;
|
|
19
|
+
tsrExportStart: (routePath: string) => string;
|
|
20
|
+
tsrExportEnd: () => string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
lazyRoute: {
|
|
24
|
+
template: () => string;
|
|
25
|
+
imports: {
|
|
26
|
+
tsrImports: () => string;
|
|
27
|
+
tsrExportStart: (routePath: string) => string;
|
|
28
|
+
tsrExportEnd: () => string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export declare function getTargetTemplate(target: Config['target']): TargetTemplate;
|
|
33
|
+
export declare const defaultAPIRouteTemplate: string;
|
|
34
|
+
export {};
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export declare const configSchema: z.ZodObject<{
|
|
3
|
+
target: z.ZodDefault<z.ZodOptional<z.ZodEnum<["react"]>>>;
|
|
3
4
|
virtualRouteConfig: z.ZodOptional<z.ZodUnion<[z.ZodType<import('@tanstack/virtual-file-routes').VirtualRootRoute, z.ZodTypeDef, import('@tanstack/virtual-file-routes').VirtualRootRoute>, z.ZodString]>>;
|
|
4
5
|
routeFilePrefix: z.ZodOptional<z.ZodString>;
|
|
5
6
|
routeFileIgnorePrefix: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
@@ -19,16 +20,19 @@ export declare const configSchema: z.ZodObject<{
|
|
|
19
20
|
indexToken: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
20
21
|
routeToken: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
21
22
|
pathParamsAllowedCharacters: z.ZodOptional<z.ZodArray<z.ZodEnum<[";", ":", "@", "&", "=", "+", "$", ","]>, "many">>;
|
|
22
|
-
customScaffolding: z.
|
|
23
|
-
routeTemplate: z.
|
|
24
|
-
|
|
23
|
+
customScaffolding: z.ZodOptional<z.ZodObject<{
|
|
24
|
+
routeTemplate: z.ZodOptional<z.ZodString>;
|
|
25
|
+
lazyRouteTemplate: z.ZodOptional<z.ZodString>;
|
|
26
|
+
apiTemplate: z.ZodOptional<z.ZodString>;
|
|
25
27
|
}, "strip", z.ZodTypeAny, {
|
|
26
|
-
routeTemplate
|
|
27
|
-
|
|
28
|
+
routeTemplate?: string | undefined;
|
|
29
|
+
lazyRouteTemplate?: string | undefined;
|
|
30
|
+
apiTemplate?: string | undefined;
|
|
28
31
|
}, {
|
|
29
32
|
routeTemplate?: string | undefined;
|
|
33
|
+
lazyRouteTemplate?: string | undefined;
|
|
30
34
|
apiTemplate?: string | undefined;
|
|
31
|
-
}
|
|
35
|
+
}>>;
|
|
32
36
|
experimental: z.ZodOptional<z.ZodObject<{
|
|
33
37
|
enableCodeSplitting: z.ZodOptional<z.ZodBoolean>;
|
|
34
38
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -37,6 +41,7 @@ export declare const configSchema: z.ZodObject<{
|
|
|
37
41
|
enableCodeSplitting?: boolean | undefined;
|
|
38
42
|
}>>;
|
|
39
43
|
}, "strip", z.ZodTypeAny, {
|
|
44
|
+
target: "react";
|
|
40
45
|
routeFileIgnorePrefix: string;
|
|
41
46
|
routesDirectory: string;
|
|
42
47
|
generatedRouteTree: string;
|
|
@@ -51,19 +56,21 @@ export declare const configSchema: z.ZodObject<{
|
|
|
51
56
|
routeTreeFileFooter: string[];
|
|
52
57
|
indexToken: string;
|
|
53
58
|
routeToken: string;
|
|
54
|
-
customScaffolding: {
|
|
55
|
-
routeTemplate: string;
|
|
56
|
-
apiTemplate: string;
|
|
57
|
-
};
|
|
58
59
|
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined;
|
|
59
60
|
routeFilePrefix?: string | undefined;
|
|
60
61
|
routeFileIgnorePattern?: string | undefined;
|
|
61
62
|
autoCodeSplitting?: boolean | undefined;
|
|
62
63
|
pathParamsAllowedCharacters?: (";" | ":" | "@" | "&" | "=" | "+" | "$" | ",")[] | undefined;
|
|
64
|
+
customScaffolding?: {
|
|
65
|
+
routeTemplate?: string | undefined;
|
|
66
|
+
lazyRouteTemplate?: string | undefined;
|
|
67
|
+
apiTemplate?: string | undefined;
|
|
68
|
+
} | undefined;
|
|
63
69
|
experimental?: {
|
|
64
70
|
enableCodeSplitting?: boolean | undefined;
|
|
65
71
|
} | undefined;
|
|
66
72
|
}, {
|
|
73
|
+
target?: "react" | undefined;
|
|
67
74
|
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined;
|
|
68
75
|
routeFilePrefix?: string | undefined;
|
|
69
76
|
routeFileIgnorePrefix?: string | undefined;
|
|
@@ -85,6 +92,7 @@ export declare const configSchema: z.ZodObject<{
|
|
|
85
92
|
pathParamsAllowedCharacters?: (";" | ":" | "@" | "&" | "=" | "+" | "$" | ",")[] | undefined;
|
|
86
93
|
customScaffolding?: {
|
|
87
94
|
routeTemplate?: string | undefined;
|
|
95
|
+
lazyRouteTemplate?: string | undefined;
|
|
88
96
|
apiTemplate?: string | undefined;
|
|
89
97
|
} | undefined;
|
|
90
98
|
experimental?: {
|
package/dist/esm/config.js
CHANGED
|
@@ -2,22 +2,8 @@ import path from "node:path";
|
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { virtualRootRouteSchema } from "./filesystem/virtual/config.js";
|
|
5
|
-
const defaultTemplate = {
|
|
6
|
-
routeTemplate: [
|
|
7
|
-
"%%tsrImports%%",
|
|
8
|
-
"\n\n",
|
|
9
|
-
"%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n",
|
|
10
|
-
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
11
|
-
].join(""),
|
|
12
|
-
apiTemplate: [
|
|
13
|
-
'import { json } from "@tanstack/start";\n',
|
|
14
|
-
"%%tsrImports%%",
|
|
15
|
-
"\n\n",
|
|
16
|
-
`%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:'Hello "%%tsrPath%%"!' }) }}%%tsrExportEnd%%
|
|
17
|
-
`
|
|
18
|
-
].join("")
|
|
19
|
-
};
|
|
20
5
|
const configSchema = z.object({
|
|
6
|
+
target: z.enum(["react"]).optional().default("react"),
|
|
21
7
|
virtualRouteConfig: virtualRootRouteSchema.or(z.string()).optional(),
|
|
22
8
|
routeFilePrefix: z.string().optional(),
|
|
23
9
|
routeFileIgnorePrefix: z.string().optional().default("-"),
|
|
@@ -42,9 +28,10 @@ const configSchema = z.object({
|
|
|
42
28
|
routeToken: z.string().optional().default("route"),
|
|
43
29
|
pathParamsAllowedCharacters: z.array(z.enum([";", ":", "@", "&", "=", "+", "$", ","])).optional(),
|
|
44
30
|
customScaffolding: z.object({
|
|
45
|
-
routeTemplate: z.string().optional()
|
|
46
|
-
|
|
47
|
-
|
|
31
|
+
routeTemplate: z.string().optional(),
|
|
32
|
+
lazyRouteTemplate: z.string().optional(),
|
|
33
|
+
apiTemplate: z.string().optional()
|
|
34
|
+
}).optional(),
|
|
48
35
|
experimental: z.object({
|
|
49
36
|
// TODO: Remove this option in the next major release (v2).
|
|
50
37
|
enableCodeSplitting: z.boolean().optional()
|
package/dist/esm/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sources":["../../src/config.ts"],"sourcesContent":["import path from 'node:path'\nimport { existsSync, readFileSync } from 'node:fs'\nimport { z } from 'zod'\nimport { virtualRootRouteSchema } from './filesystem/virtual/config'\n\
|
|
1
|
+
{"version":3,"file":"config.js","sources":["../../src/config.ts"],"sourcesContent":["import path from 'node:path'\nimport { existsSync, readFileSync } from 'node:fs'\nimport { z } from 'zod'\nimport { virtualRootRouteSchema } from './filesystem/virtual/config'\n\nexport const configSchema = z.object({\n target: z.enum(['react']).optional().default('react'),\n virtualRouteConfig: virtualRootRouteSchema.or(z.string()).optional(),\n routeFilePrefix: z.string().optional(),\n routeFileIgnorePrefix: z.string().optional().default('-'),\n routeFileIgnorePattern: z.string().optional(),\n routesDirectory: z.string().optional().default('./src/routes'),\n generatedRouteTree: z.string().optional().default('./src/routeTree.gen.ts'),\n quoteStyle: z.enum(['single', 'double']).optional().default('single'),\n semicolons: z.boolean().optional().default(false),\n disableTypes: z.boolean().optional().default(false),\n addExtensions: z.boolean().optional().default(false),\n disableLogging: z.boolean().optional().default(false),\n disableManifestGeneration: z.boolean().optional().default(false),\n apiBase: z.string().optional().default('/api'),\n routeTreeFileHeader: z\n .array(z.string())\n .optional()\n .default([\n '/* eslint-disable */',\n '// @ts-nocheck',\n '// noinspection JSUnusedGlobalSymbols',\n ]),\n routeTreeFileFooter: z.array(z.string()).optional().default([]),\n autoCodeSplitting: z.boolean().optional(),\n indexToken: z.string().optional().default('index'),\n routeToken: z.string().optional().default('route'),\n pathParamsAllowedCharacters: z\n .array(z.enum([';', ':', '@', '&', '=', '+', '$', ',']))\n .optional(),\n customScaffolding: z\n .object({\n routeTemplate: z.string().optional(),\n lazyRouteTemplate: z.string().optional(),\n apiTemplate: z.string().optional(),\n })\n .optional(),\n experimental: z\n .object({\n // TODO: Remove this option in the next major release (v2).\n enableCodeSplitting: z.boolean().optional(),\n })\n .optional(),\n})\n\nexport type Config = z.infer<typeof configSchema>\n\ntype ResolveParams = {\n configDirectory: string\n}\n\nexport function resolveConfigPath({ configDirectory }: ResolveParams) {\n return path.resolve(configDirectory, 'tsr.config.json')\n}\n\nexport function getConfig(\n inlineConfig: Partial<Config> = {},\n configDirectory?: string,\n): Config {\n if (configDirectory === undefined) {\n configDirectory = process.cwd()\n }\n const configFilePathJson = resolveConfigPath({ configDirectory })\n const exists = existsSync(configFilePathJson)\n\n let config: Config\n\n if (exists) {\n config = configSchema.parse({\n ...JSON.parse(readFileSync(configFilePathJson, 'utf-8')),\n ...inlineConfig,\n })\n } else {\n config = configSchema.parse(inlineConfig)\n }\n\n // If typescript is disabled, make sure the generated route tree is a .js file\n if (config.disableTypes) {\n config.generatedRouteTree = config.generatedRouteTree.replace(\n /\\.(ts|tsx)$/,\n '.js',\n )\n }\n\n // if a configDirectory is used, paths should be relative to that directory\n if (configDirectory) {\n // if absolute configDirectory is provided, use it as the root\n if (path.isAbsolute(configDirectory)) {\n config.routesDirectory = path.resolve(\n configDirectory,\n config.routesDirectory,\n )\n config.generatedRouteTree = path.resolve(\n configDirectory,\n config.generatedRouteTree,\n )\n } else {\n config.routesDirectory = path.resolve(\n process.cwd(),\n configDirectory,\n config.routesDirectory,\n )\n config.generatedRouteTree = path.resolve(\n process.cwd(),\n configDirectory,\n config.generatedRouteTree,\n )\n }\n }\n\n validateConfig(config)\n return config\n}\n\nfunction validateConfig(config: Config) {\n if (typeof config.experimental?.enableCodeSplitting !== 'undefined') {\n const message = `\n------\n⚠️ ⚠️ ⚠️\nERROR: The \"experimental.enableCodeSplitting\" flag has been made stable and is now \"autoCodeSplitting\". Please update your configuration file to use \"autoCodeSplitting\" instead of \"experimental.enableCodeSplitting\".\n------\n`\n console.error(message)\n throw new Error(message)\n }\n\n if (config.indexToken === config.routeToken) {\n throw new Error(\n `The \"indexToken\" and \"routeToken\" options must be different.`,\n )\n }\n\n if (\n config.routeFileIgnorePrefix &&\n config.routeFileIgnorePrefix.trim() === '_'\n ) {\n throw new Error(\n `The \"routeFileIgnorePrefix\" cannot be an underscore (\"_\"). This is a reserved character used to denote a pathless route. Please use a different prefix.`,\n )\n }\n\n return config\n}\n"],"names":[],"mappings":";;;;AAKa,MAAA,eAAe,EAAE,OAAO;AAAA,EACnC,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,SAAA,EAAW,QAAQ,OAAO;AAAA,EACpD,oBAAoB,uBAAuB,GAAG,E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;AACA,aAAO,qBAAqB,KAAK;AAAA,QAC/B,QAAQ,IAAI;AAAA,QACZ;AAAA,QACA,OAAO;AAAA,MACT;AAAA,IAAA;AAAA,EACF;AAGF,iBAAe,MAAM;AACd,SAAA;AACT;AAEA,SAAS,eAAe,QAAgB;;AACtC,MAAI,SAAO,YAAO,iBAAP,mBAAqB,yBAAwB,aAAa;AACnE,UAAM,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAMhB,YAAQ,MAAM,OAAO;AACf,UAAA,IAAI,MAAM,OAAO;AAAA,EAAA;AAGrB,MAAA,OAAO,eAAe,OAAO,YAAY;AAC3C,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EAAA;AAGF,MACE,OAAO,yBACP,OAAO,sBAAsB,WAAW,KACxC;AACA,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EAAA;AAGK,SAAA;AACT;"}
|
package/dist/esm/generator.js
CHANGED
|
@@ -6,6 +6,7 @@ import { logging, multiSortBy, replaceBackslash, removeExt, writeIfDifferent, de
|
|
|
6
6
|
import { getRouteNodes as getRouteNodes$1 } from "./filesystem/physical/getRouteNodes.js";
|
|
7
7
|
import { getRouteNodes } from "./filesystem/virtual/getRouteNodes.js";
|
|
8
8
|
import { rootPathId } from "./filesystem/physical/rootPathId.js";
|
|
9
|
+
import { getTargetTemplate, fillTemplate, defaultAPIRouteTemplate } from "./template.js";
|
|
9
10
|
const CONSTANTS = {
|
|
10
11
|
// When changing this, you'll want to update the import in `start/api/index.ts#defaultAPIFileRouteHandler`
|
|
11
12
|
APIRouteExportVariable: "APIRoute"
|
|
@@ -16,6 +17,7 @@ const possiblyNestedRouteGroupPatternRegex = /\([^/]+\)\/?/g;
|
|
|
16
17
|
let isFirst = false;
|
|
17
18
|
let skipMessage = false;
|
|
18
19
|
async function generator(config, root) {
|
|
20
|
+
const ROUTE_TEMPLATE = getTargetTemplate(config.target);
|
|
19
21
|
const logger = logging({ disabled: config.disableLogging });
|
|
20
22
|
logger.log("");
|
|
21
23
|
if (!isFirst) {
|
|
@@ -84,21 +86,13 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
84
86
|
}
|
|
85
87
|
const routeCode = fs.readFileSync(node.fullPath, "utf-8");
|
|
86
88
|
if (!routeCode) {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
].join(""),
|
|
95
|
-
{
|
|
96
|
-
tsrImports: "import { Outlet, createRootRoute } from '@tanstack/react-router';",
|
|
97
|
-
tsrPath: rootPathId,
|
|
98
|
-
tsrExportStart: `export const Route = createRootRoute(`,
|
|
99
|
-
tsrExportEnd: ");"
|
|
100
|
-
}
|
|
101
|
-
);
|
|
89
|
+
const _rootTemplate = ROUTE_TEMPLATE.rootRoute;
|
|
90
|
+
const replaced = fillTemplate(_rootTemplate.template(), {
|
|
91
|
+
tsrImports: _rootTemplate.imports.tsrImports(),
|
|
92
|
+
tsrPath: rootPathId,
|
|
93
|
+
tsrExportStart: _rootTemplate.imports.tsrExportStart(),
|
|
94
|
+
tsrExportEnd: _rootTemplate.imports.tsrExportEnd()
|
|
95
|
+
});
|
|
102
96
|
logger.log(`🟡 Creating ${node.fullPath}`);
|
|
103
97
|
fs.writeFileSync(
|
|
104
98
|
node.fullPath,
|
|
@@ -108,7 +102,7 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
108
102
|
};
|
|
109
103
|
await handleRootNode(rootRouteNode);
|
|
110
104
|
const handleNode = async (node) => {
|
|
111
|
-
var _a, _b;
|
|
105
|
+
var _a, _b, _c, _d, _e;
|
|
112
106
|
resetRegex(routeGroupPatternRegex);
|
|
113
107
|
let parentRoute = hasParentRoute(routeNodes, node, node.routePath);
|
|
114
108
|
if ((parentRoute == null ? void 0 : parentRoute.isVirtualParentRoute) && ((_a = parentRoute.children) == null ? void 0 : _a.length)) {
|
|
@@ -134,28 +128,39 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
134
128
|
const routeCode = fs.readFileSync(node.fullPath, "utf-8");
|
|
135
129
|
const escapedRoutePath = ((_b = node.routePath) == null ? void 0 : _b.replaceAll("$", "$$")) ?? "";
|
|
136
130
|
let replaced = routeCode;
|
|
131
|
+
const tRouteTemplate = ROUTE_TEMPLATE.route;
|
|
132
|
+
const tLazyRouteTemplate = ROUTE_TEMPLATE.lazyRoute;
|
|
137
133
|
if (!routeCode) {
|
|
138
134
|
if (node.isLazy) {
|
|
139
|
-
replaced = fillTemplate(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
135
|
+
replaced = fillTemplate(
|
|
136
|
+
(((_c = config.customScaffolding) == null ? void 0 : _c.lazyRouteTemplate) || ((_d = config.customScaffolding) == null ? void 0 : _d.routeTemplate)) ?? tLazyRouteTemplate.template(),
|
|
137
|
+
{
|
|
138
|
+
tsrImports: tLazyRouteTemplate.imports.tsrImports(),
|
|
139
|
+
tsrPath: escapedRoutePath,
|
|
140
|
+
tsrExportStart: tLazyRouteTemplate.imports.tsrExportStart(escapedRoutePath),
|
|
141
|
+
tsrExportEnd: tLazyRouteTemplate.imports.tsrExportEnd()
|
|
142
|
+
}
|
|
143
|
+
);
|
|
145
144
|
} else if (node.isRoute || !node.isComponent && !node.isErrorComponent && !node.isPendingComponent && !node.isLoader) {
|
|
146
|
-
replaced = fillTemplate(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
145
|
+
replaced = fillTemplate(
|
|
146
|
+
((_e = config.customScaffolding) == null ? void 0 : _e.routeTemplate) ?? tRouteTemplate.template(),
|
|
147
|
+
{
|
|
148
|
+
tsrImports: tRouteTemplate.imports.tsrImports(),
|
|
149
|
+
tsrPath: escapedRoutePath,
|
|
150
|
+
tsrExportStart: tRouteTemplate.imports.tsrExportStart(escapedRoutePath),
|
|
151
|
+
tsrExportEnd: tRouteTemplate.imports.tsrExportEnd()
|
|
152
|
+
}
|
|
153
|
+
);
|
|
152
154
|
}
|
|
153
155
|
} else {
|
|
154
156
|
replaced = routeCode.replace(
|
|
155
157
|
/(FileRoute\(\s*['"])([^\s]*)(['"],?\s*\))/g,
|
|
156
158
|
(_, p1, __, p3) => `${p1}${escapedRoutePath}${p3}`
|
|
157
159
|
).replace(
|
|
158
|
-
|
|
160
|
+
new RegExp(
|
|
161
|
+
`(import\\s*\\{.*)(create(Lazy)?FileRoute)(.*\\}\\s*from\\s*['"]@tanstack\\/${ROUTE_TEMPLATE.subPkg}['"])`,
|
|
162
|
+
"gs"
|
|
163
|
+
),
|
|
159
164
|
(_, p1, __, ___, p4) => `${p1}${node.isLazy ? "createLazyFileRoute" : "createFileRoute"}${p4}`
|
|
160
165
|
).replace(
|
|
161
166
|
/create(Lazy)?FileRoute(\(\s*['"])([^\s]*)(['"],?\s*\))/g,
|
|
@@ -250,16 +255,19 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
250
255
|
config
|
|
251
256
|
);
|
|
252
257
|
const handleAPINode = async (node) => {
|
|
253
|
-
var _a;
|
|
258
|
+
var _a, _b;
|
|
254
259
|
const routeCode = fs.readFileSync(node.fullPath, "utf-8");
|
|
255
260
|
const escapedRoutePath = ((_a = node.routePath) == null ? void 0 : _a.replaceAll("$", "$$")) ?? "";
|
|
256
261
|
if (!routeCode) {
|
|
257
|
-
const replaced = fillTemplate(
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
const replaced = fillTemplate(
|
|
263
|
+
((_b = config.customScaffolding) == null ? void 0 : _b.apiTemplate) ?? defaultAPIRouteTemplate,
|
|
264
|
+
{
|
|
265
|
+
tsrImports: "import { createAPIFileRoute } from '@tanstack/start/api';",
|
|
266
|
+
tsrPath: escapedRoutePath,
|
|
267
|
+
tsrExportStart: `export const ${CONSTANTS.APIRouteExportVariable} = createAPIFileRoute('${escapedRoutePath}')(`,
|
|
268
|
+
tsrExportEnd: ");"
|
|
269
|
+
}
|
|
270
|
+
);
|
|
263
271
|
logger.log(`🟡 Creating ${node.fullPath}`);
|
|
264
272
|
fs.writeFileSync(
|
|
265
273
|
node.fullPath,
|
|
@@ -363,7 +371,7 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
363
371
|
`// This file was automatically generated by TanStack Router.
|
|
364
372
|
// You should NOT make any changes in this file as it will be overwritten.
|
|
365
373
|
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.`,
|
|
366
|
-
imports.length ? `import { ${imports.join(", ")} } from '
|
|
374
|
+
imports.length ? `import { ${imports.join(", ")} } from '${ROUTE_TEMPLATE.fullPkg}'
|
|
367
375
|
` : "",
|
|
368
376
|
"// Import Routes",
|
|
369
377
|
[
|
|
@@ -434,7 +442,7 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
434
442
|
}).join("\n\n"),
|
|
435
443
|
...TYPES_DISABLED ? [] : [
|
|
436
444
|
"// Populate the FileRoutesByPath interface",
|
|
437
|
-
`declare module '
|
|
445
|
+
`declare module '${ROUTE_TEMPLATE.fullPkg}' {
|
|
438
446
|
interface FileRoutesByPath {
|
|
439
447
|
${routeNodes.map((routeNode) => {
|
|
440
448
|
var _a, _b;
|
|
@@ -518,13 +526,17 @@ Add the file in: "${config.routesDirectory}/${rootPathId}.${config.disableTypes
|
|
|
518
526
|
2
|
|
519
527
|
);
|
|
520
528
|
};
|
|
521
|
-
const routeConfigFileContent =
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
"
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
529
|
+
const routeConfigFileContent = (
|
|
530
|
+
// TODO: Remove this disabled eslint rule when more target types are added.
|
|
531
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
532
|
+
config.disableManifestGeneration || config.target !== "react" ? routeImports : [
|
|
533
|
+
routeImports,
|
|
534
|
+
"\n",
|
|
535
|
+
"/* ROUTE_MANIFEST_START",
|
|
536
|
+
createRouteManifest(),
|
|
537
|
+
"ROUTE_MANIFEST_END */"
|
|
538
|
+
].join("\n")
|
|
539
|
+
);
|
|
528
540
|
if (!checkLatest()) return;
|
|
529
541
|
const existingRouteTreeContent = await fsp.readFile(path.resolve(config.generatedRouteTree), "utf-8").catch((err) => {
|
|
530
542
|
if (err.code === "ENOENT") {
|
|
@@ -699,12 +711,6 @@ function startAPIRouteSegmentsFromTSRFilePath(src, config) {
|
|
|
699
711
|
});
|
|
700
712
|
return segments;
|
|
701
713
|
}
|
|
702
|
-
function fillTemplate(template, values) {
|
|
703
|
-
return template.replace(
|
|
704
|
-
/%%(\w+)%%/g,
|
|
705
|
-
(_, key) => values[key] || ""
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
714
|
export {
|
|
709
715
|
CONSTANTS,
|
|
710
716
|
createRouteNodesByFullPath,
|