@tanstack/router-generator 1.121.16 → 1.121.18

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.
Files changed (97) hide show
  1. package/package.json +5 -5
  2. package/dist/cjs/config.cjs +0 -140
  3. package/dist/cjs/config.cjs.map +0 -1
  4. package/dist/cjs/config.d.cts +0 -155
  5. package/dist/cjs/filesystem/physical/getRouteNodes.cjs +0 -204
  6. package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +0 -1
  7. package/dist/cjs/filesystem/physical/getRouteNodes.d.cts +0 -15
  8. package/dist/cjs/filesystem/physical/rootPathId.cjs +0 -5
  9. package/dist/cjs/filesystem/physical/rootPathId.cjs.map +0 -1
  10. package/dist/cjs/filesystem/physical/rootPathId.d.cts +0 -1
  11. package/dist/cjs/filesystem/virtual/config.cjs +0 -37
  12. package/dist/cjs/filesystem/virtual/config.cjs.map +0 -1
  13. package/dist/cjs/filesystem/virtual/config.d.cts +0 -3
  14. package/dist/cjs/filesystem/virtual/getRouteNodes.cjs +0 -192
  15. package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +0 -1
  16. package/dist/cjs/filesystem/virtual/getRouteNodes.d.cts +0 -8
  17. package/dist/cjs/filesystem/virtual/loadConfigFile.cjs +0 -11
  18. package/dist/cjs/filesystem/virtual/loadConfigFile.cjs.map +0 -1
  19. package/dist/cjs/filesystem/virtual/loadConfigFile.d.cts +0 -1
  20. package/dist/cjs/generator.cjs +0 -970
  21. package/dist/cjs/generator.cjs.map +0 -1
  22. package/dist/cjs/generator.d.cts +0 -76
  23. package/dist/cjs/index.cjs +0 -35
  24. package/dist/cjs/index.cjs.map +0 -1
  25. package/dist/cjs/index.d.cts +0 -12
  26. package/dist/cjs/logger.cjs +0 -37
  27. package/dist/cjs/logger.cjs.map +0 -1
  28. package/dist/cjs/logger.d.cts +0 -10
  29. package/dist/cjs/plugin/default-generator-plugin.cjs +0 -101
  30. package/dist/cjs/plugin/default-generator-plugin.cjs.map +0 -1
  31. package/dist/cjs/plugin/default-generator-plugin.d.cts +0 -2
  32. package/dist/cjs/plugin/types.d.cts +0 -46
  33. package/dist/cjs/template.cjs +0 -110
  34. package/dist/cjs/template.cjs.map +0 -1
  35. package/dist/cjs/template.d.cts +0 -33
  36. package/dist/cjs/transform/default-transform-plugin.cjs +0 -95
  37. package/dist/cjs/transform/default-transform-plugin.cjs.map +0 -1
  38. package/dist/cjs/transform/default-transform-plugin.d.cts +0 -2
  39. package/dist/cjs/transform/transform.cjs +0 -374
  40. package/dist/cjs/transform/transform.cjs.map +0 -1
  41. package/dist/cjs/transform/transform.d.cts +0 -4
  42. package/dist/cjs/transform/types.d.cts +0 -43
  43. package/dist/cjs/transform/utils.cjs +0 -36
  44. package/dist/cjs/transform/utils.cjs.map +0 -1
  45. package/dist/cjs/transform/utils.d.cts +0 -2
  46. package/dist/cjs/types.d.cts +0 -47
  47. package/dist/cjs/utils.cjs +0 -438
  48. package/dist/cjs/utils.cjs.map +0 -1
  49. package/dist/cjs/utils.d.cts +0 -133
  50. package/dist/esm/config.d.ts +0 -155
  51. package/dist/esm/config.js +0 -140
  52. package/dist/esm/config.js.map +0 -1
  53. package/dist/esm/filesystem/physical/getRouteNodes.d.ts +0 -15
  54. package/dist/esm/filesystem/physical/getRouteNodes.js +0 -187
  55. package/dist/esm/filesystem/physical/getRouteNodes.js.map +0 -1
  56. package/dist/esm/filesystem/physical/rootPathId.d.ts +0 -1
  57. package/dist/esm/filesystem/physical/rootPathId.js +0 -5
  58. package/dist/esm/filesystem/physical/rootPathId.js.map +0 -1
  59. package/dist/esm/filesystem/virtual/config.d.ts +0 -3
  60. package/dist/esm/filesystem/virtual/config.js +0 -37
  61. package/dist/esm/filesystem/virtual/config.js.map +0 -1
  62. package/dist/esm/filesystem/virtual/getRouteNodes.d.ts +0 -8
  63. package/dist/esm/filesystem/virtual/getRouteNodes.js +0 -192
  64. package/dist/esm/filesystem/virtual/getRouteNodes.js.map +0 -1
  65. package/dist/esm/filesystem/virtual/loadConfigFile.d.ts +0 -1
  66. package/dist/esm/filesystem/virtual/loadConfigFile.js +0 -11
  67. package/dist/esm/filesystem/virtual/loadConfigFile.js.map +0 -1
  68. package/dist/esm/generator.d.ts +0 -76
  69. package/dist/esm/generator.js +0 -953
  70. package/dist/esm/generator.js.map +0 -1
  71. package/dist/esm/index.d.ts +0 -12
  72. package/dist/esm/index.js +0 -35
  73. package/dist/esm/index.js.map +0 -1
  74. package/dist/esm/logger.d.ts +0 -10
  75. package/dist/esm/logger.js +0 -37
  76. package/dist/esm/logger.js.map +0 -1
  77. package/dist/esm/plugin/default-generator-plugin.d.ts +0 -2
  78. package/dist/esm/plugin/default-generator-plugin.js +0 -101
  79. package/dist/esm/plugin/default-generator-plugin.js.map +0 -1
  80. package/dist/esm/plugin/types.d.ts +0 -46
  81. package/dist/esm/template.d.ts +0 -33
  82. package/dist/esm/template.js +0 -110
  83. package/dist/esm/template.js.map +0 -1
  84. package/dist/esm/transform/default-transform-plugin.d.ts +0 -2
  85. package/dist/esm/transform/default-transform-plugin.js +0 -95
  86. package/dist/esm/transform/default-transform-plugin.js.map +0 -1
  87. package/dist/esm/transform/transform.d.ts +0 -4
  88. package/dist/esm/transform/transform.js +0 -374
  89. package/dist/esm/transform/transform.js.map +0 -1
  90. package/dist/esm/transform/types.d.ts +0 -43
  91. package/dist/esm/transform/utils.d.ts +0 -2
  92. package/dist/esm/transform/utils.js +0 -36
  93. package/dist/esm/transform/utils.js.map +0 -1
  94. package/dist/esm/types.d.ts +0 -47
  95. package/dist/esm/utils.d.ts +0 -133
  96. package/dist/esm/utils.js +0 -420
  97. package/dist/esm/utils.js.map +0 -1
@@ -1,95 +0,0 @@
1
- import { types } from "recast";
2
- import { ensureStringArgument } from "./utils.js";
3
- const b = types.builders;
4
- const defaultTransformPlugin = {
5
- name: "default-transform",
6
- exportName: "Route",
7
- imports: (ctx) => {
8
- const imports = {};
9
- const targetModule = `@tanstack/${ctx.target}-router`;
10
- if (ctx.verboseFileRoutes === false) {
11
- imports.banned = [
12
- {
13
- source: targetModule,
14
- specifiers: [
15
- { imported: "createLazyFileRoute" },
16
- { imported: "createFileRoute" }
17
- ]
18
- }
19
- ];
20
- } else {
21
- if (ctx.lazy) {
22
- imports.required = [
23
- {
24
- source: targetModule,
25
- specifiers: [{ imported: "createLazyFileRoute" }]
26
- }
27
- ];
28
- imports.banned = [
29
- {
30
- source: targetModule,
31
- specifiers: [{ imported: "createFileRoute" }]
32
- }
33
- ];
34
- } else {
35
- imports.required = [
36
- {
37
- source: targetModule,
38
- specifiers: [{ imported: "createFileRoute" }]
39
- }
40
- ];
41
- imports.banned = [
42
- {
43
- source: targetModule,
44
- specifiers: [{ imported: "createLazyFileRoute" }]
45
- }
46
- ];
47
- }
48
- }
49
- return imports;
50
- },
51
- onExportFound: ({ decl, ctx }) => {
52
- var _a;
53
- let appliedChanges = false;
54
- if (((_a = decl.init) == null ? void 0 : _a.type) === "CallExpression") {
55
- const callExpression = decl.init;
56
- let identifier;
57
- if (callExpression.callee.type === "Identifier") {
58
- identifier = callExpression.callee;
59
- if (ctx.verboseFileRoutes) {
60
- callExpression.callee = b.callExpression(identifier, [
61
- b.stringLiteral(ctx.routeId)
62
- ]);
63
- appliedChanges = true;
64
- }
65
- } else if (callExpression.callee.type === "CallExpression" && callExpression.callee.callee.type === "Identifier") {
66
- identifier = callExpression.callee.callee;
67
- if (!ctx.verboseFileRoutes) {
68
- callExpression.callee = identifier;
69
- appliedChanges = true;
70
- } else {
71
- appliedChanges = ensureStringArgument(
72
- callExpression.callee,
73
- ctx.routeId,
74
- ctx.preferredQuote
75
- );
76
- }
77
- }
78
- if (identifier === void 0) {
79
- throw new Error(`expected identifier to be present`);
80
- }
81
- if (identifier.name === "createFileRoute" && ctx.lazy) {
82
- identifier.name = "createLazyFileRoute";
83
- appliedChanges = true;
84
- } else if (identifier.name === "createLazyFileRoute" && !ctx.lazy) {
85
- identifier.name = "createFileRoute";
86
- appliedChanges = true;
87
- }
88
- }
89
- return appliedChanges;
90
- }
91
- };
92
- export {
93
- defaultTransformPlugin
94
- };
95
- //# sourceMappingURL=default-transform-plugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default-transform-plugin.js","sources":["../../../src/transform/default-transform-plugin.ts"],"sourcesContent":["import { types } from 'recast'\nimport { ensureStringArgument } from './utils'\nimport type { TransformImportsConfig, TransformPlugin } from './types'\n\nconst b = types.builders\n\nexport const defaultTransformPlugin: TransformPlugin = {\n name: 'default-transform',\n exportName: 'Route',\n imports: (ctx) => {\n const imports: TransformImportsConfig = {}\n const targetModule = `@tanstack/${ctx.target}-router`\n if (ctx.verboseFileRoutes === false) {\n imports.banned = [\n {\n source: targetModule,\n specifiers: [\n { imported: 'createLazyFileRoute' },\n { imported: 'createFileRoute' },\n ],\n },\n ]\n } else {\n if (ctx.lazy) {\n imports.required = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createLazyFileRoute' }],\n },\n ]\n imports.banned = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createFileRoute' }],\n },\n ]\n } else {\n imports.required = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createFileRoute' }],\n },\n ]\n imports.banned = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createLazyFileRoute' }],\n },\n ]\n }\n }\n return imports\n },\n onExportFound: ({ decl, ctx }) => {\n let appliedChanges = false\n if (decl.init?.type === 'CallExpression') {\n const callExpression = decl.init\n let identifier: types.namedTypes.Identifier | undefined\n // `const Route = createFileRoute({ ... })`\n if (callExpression.callee.type === 'Identifier') {\n identifier = callExpression.callee\n if (ctx.verboseFileRoutes) {\n // we need to add the string literal via another CallExpression\n callExpression.callee = b.callExpression(identifier, [\n b.stringLiteral(ctx.routeId),\n ])\n appliedChanges = true\n }\n }\n // `const Route = createFileRoute('/path')({ ... })`\n else if (\n callExpression.callee.type === 'CallExpression' &&\n callExpression.callee.callee.type === 'Identifier'\n ) {\n identifier = callExpression.callee.callee\n if (!ctx.verboseFileRoutes) {\n // we need to remove the route id\n callExpression.callee = identifier\n appliedChanges = true\n } else {\n // check if the route id is correct\n appliedChanges = ensureStringArgument(\n callExpression.callee,\n ctx.routeId,\n ctx.preferredQuote,\n )\n }\n }\n if (identifier === undefined) {\n throw new Error(`expected identifier to be present`)\n }\n if (identifier.name === 'createFileRoute' && ctx.lazy) {\n identifier.name = 'createLazyFileRoute'\n appliedChanges = true\n } else if (identifier.name === 'createLazyFileRoute' && !ctx.lazy) {\n identifier.name = 'createFileRoute'\n appliedChanges = true\n }\n }\n\n return appliedChanges\n },\n}\n"],"names":[],"mappings":";;AAIA,MAAM,IAAI,MAAM;AAET,MAAM,yBAA0C;AAAA,EACrD,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,SAAS,CAAC,QAAQ;AAChB,UAAM,UAAkC,CAAC;AACnC,UAAA,eAAe,aAAa,IAAI,MAAM;AACxC,QAAA,IAAI,sBAAsB,OAAO;AACnC,cAAQ,SAAS;AAAA,QACf;AAAA,UACE,QAAQ;AAAA,UACR,YAAY;AAAA,YACV,EAAE,UAAU,sBAAsB;AAAA,YAClC,EAAE,UAAU,kBAAkB;AAAA,UAAA;AAAA,QAChC;AAAA,MAEJ;AAAA,IAAA,OACK;AACL,UAAI,IAAI,MAAM;AACZ,gBAAQ,WAAW;AAAA,UACjB;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,sBAAuB,CAAA;AAAA,UAAA;AAAA,QAEpD;AACA,gBAAQ,SAAS;AAAA,UACf;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,kBAAmB,CAAA;AAAA,UAAA;AAAA,QAEhD;AAAA,MAAA,OACK;AACL,gBAAQ,WAAW;AAAA,UACjB;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,kBAAmB,CAAA;AAAA,UAAA;AAAA,QAEhD;AACA,gBAAQ,SAAS;AAAA,UACf;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,sBAAuB,CAAA;AAAA,UAAA;AAAA,QAEpD;AAAA,MAAA;AAAA,IACF;AAEK,WAAA;AAAA,EACT;AAAA,EACA,eAAe,CAAC,EAAE,MAAM,UAAU;;AAChC,QAAI,iBAAiB;AACjB,UAAA,UAAK,SAAL,mBAAW,UAAS,kBAAkB;AACxC,YAAM,iBAAiB,KAAK;AACxB,UAAA;AAEA,UAAA,eAAe,OAAO,SAAS,cAAc;AAC/C,qBAAa,eAAe;AAC5B,YAAI,IAAI,mBAAmB;AAEV,yBAAA,SAAS,EAAE,eAAe,YAAY;AAAA,YACnD,EAAE,cAAc,IAAI,OAAO;AAAA,UAAA,CAC5B;AACgB,2BAAA;AAAA,QAAA;AAAA,MACnB,WAIA,eAAe,OAAO,SAAS,oBAC/B,eAAe,OAAO,OAAO,SAAS,cACtC;AACA,qBAAa,eAAe,OAAO;AAC/B,YAAA,CAAC,IAAI,mBAAmB;AAE1B,yBAAe,SAAS;AACP,2BAAA;AAAA,QAAA,OACZ;AAEY,2BAAA;AAAA,YACf,eAAe;AAAA,YACf,IAAI;AAAA,YACJ,IAAI;AAAA,UACN;AAAA,QAAA;AAAA,MACF;AAEF,UAAI,eAAe,QAAW;AACtB,cAAA,IAAI,MAAM,mCAAmC;AAAA,MAAA;AAErD,UAAI,WAAW,SAAS,qBAAqB,IAAI,MAAM;AACrD,mBAAW,OAAO;AACD,yBAAA;AAAA,MAAA,WACR,WAAW,SAAS,yBAAyB,CAAC,IAAI,MAAM;AACjE,mBAAW,OAAO;AACD,yBAAA;AAAA,MAAA;AAAA,IACnB;AAGK,WAAA;AAAA,EAAA;AAEX;"}
@@ -1,4 +0,0 @@
1
- import { types } from 'recast';
2
- import { TransformOptions, TransformResult } from './types.js';
3
- export declare function transform({ ctx, source, plugins, }: TransformOptions): Promise<TransformResult>;
4
- export declare function detectPreferredQuoteStyle(ast: types.ASTNode): "'" | '"';
@@ -1,374 +0,0 @@
1
- import { parseAst } from "@tanstack/router-utils";
2
- import { parse, types, print, visit } from "recast";
3
- import { SourceMapConsumer } from "source-map";
4
- import { mergeImportDeclarations } from "../utils.js";
5
- const b = types.builders;
6
- async function transform({
7
- ctx,
8
- source,
9
- plugins
10
- }) {
11
- var _a, _b, _c, _d;
12
- let appliedChanges = false;
13
- let ast;
14
- const foundExports = [];
15
- try {
16
- ast = parse(source, {
17
- sourceFileName: "output.ts",
18
- parser: {
19
- parse(code) {
20
- return parseAst({
21
- code,
22
- // we need to instruct babel to produce tokens,
23
- // otherwise recast will try to generate the tokens via its own parser and will fail
24
- tokens: true
25
- });
26
- }
27
- }
28
- });
29
- } catch (e) {
30
- console.error("Error parsing code", ctx.routeId, source, e);
31
- return {
32
- result: "error",
33
- error: e
34
- };
35
- }
36
- const preferredQuote = detectPreferredQuoteStyle(ast);
37
- const registeredExports = /* @__PURE__ */ new Map();
38
- for (const plugin of plugins ?? []) {
39
- const exportName = plugin.exportName;
40
- if (registeredExports.has(exportName)) {
41
- throw new Error(
42
- `Export ${exportName} is already registered by plugin ${(_a = registeredExports.get(exportName)) == null ? void 0 : _a.name}`
43
- );
44
- }
45
- registeredExports.set(exportName, plugin);
46
- }
47
- function onExportFound(decl, exportName, plugin) {
48
- const pluginAppliedChanges = plugin.onExportFound({
49
- decl,
50
- ctx: { ...ctx, preferredQuote }
51
- });
52
- if (pluginAppliedChanges) {
53
- appliedChanges = true;
54
- }
55
- registeredExports.delete(exportName);
56
- foundExports.push(exportName);
57
- }
58
- const program = ast.program;
59
- for (const n of program.body) {
60
- if (registeredExports.size > 0 && n.type === "ExportNamedDeclaration") {
61
- if (((_b = n.declaration) == null ? void 0 : _b.type) === "VariableDeclaration") {
62
- const decl = n.declaration.declarations[0];
63
- if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
64
- const plugin = registeredExports.get(decl.id.name);
65
- if (plugin) {
66
- onExportFound(decl, decl.id.name, plugin);
67
- }
68
- }
69
- } else if (n.declaration === null && n.specifiers) {
70
- for (const spec of n.specifiers) {
71
- if (typeof spec.exported.name === "string") {
72
- const plugin = registeredExports.get(spec.exported.name);
73
- if (plugin) {
74
- const variableName = ((_c = spec.local) == null ? void 0 : _c.name) || spec.exported.name;
75
- for (const decl of program.body) {
76
- if (decl.type === "VariableDeclaration" && decl.declarations[0]) {
77
- const variable = decl.declarations[0];
78
- if (variable.type === "VariableDeclarator" && variable.id.type === "Identifier" && variable.id.name === variableName) {
79
- onExportFound(variable, spec.exported.name, plugin);
80
- break;
81
- }
82
- }
83
- }
84
- }
85
- }
86
- }
87
- }
88
- }
89
- }
90
- const imports = {
91
- required: [],
92
- banned: []
93
- };
94
- for (const plugin of plugins ?? []) {
95
- const exportName = plugin.exportName;
96
- if (foundExports.includes(exportName)) {
97
- const pluginImports = plugin.imports(ctx);
98
- if (pluginImports.required) {
99
- imports.required.push(...pluginImports.required);
100
- }
101
- if (pluginImports.banned) {
102
- imports.banned.push(...pluginImports.banned);
103
- }
104
- }
105
- }
106
- imports.required = mergeImportDeclarations(imports.required);
107
- imports.banned = mergeImportDeclarations(imports.banned);
108
- const importStatementCandidates = [];
109
- const importDeclarationsToRemove = [];
110
- for (const n of program.body) {
111
- const findImport = (opts) => (i) => {
112
- if (i.source === opts.source) {
113
- const importKind = i.importKind || "value";
114
- const expectedImportKind = opts.importKind || "value";
115
- return expectedImportKind === importKind;
116
- }
117
- return false;
118
- };
119
- if (n.type === "ImportDeclaration" && typeof n.source.value === "string") {
120
- const filterImport = findImport({
121
- source: n.source.value,
122
- importKind: n.importKind
123
- });
124
- let requiredImports = imports.required.filter(filterImport)[0];
125
- const bannedImports = imports.banned.filter(filterImport)[0];
126
- if (!requiredImports && !bannedImports) {
127
- continue;
128
- }
129
- const importSpecifiersToRemove = [];
130
- if (n.specifiers) {
131
- for (const spec of n.specifiers) {
132
- if (!requiredImports && !bannedImports) {
133
- break;
134
- }
135
- if (spec.type === "ImportSpecifier" && typeof spec.imported.name === "string") {
136
- if (requiredImports) {
137
- const requiredImportIndex = requiredImports.specifiers.findIndex(
138
- (imp) => imp.imported === spec.imported.name
139
- );
140
- if (requiredImportIndex !== -1) {
141
- requiredImports.specifiers.splice(requiredImportIndex, 1);
142
- if (requiredImports.specifiers.length === 0) {
143
- imports.required = imports.required.splice(
144
- imports.required.indexOf(requiredImports),
145
- 1
146
- );
147
- requiredImports = void 0;
148
- }
149
- } else {
150
- importStatementCandidates.push(n);
151
- }
152
- }
153
- if (bannedImports) {
154
- const bannedImportIndex = bannedImports.specifiers.findIndex(
155
- (imp) => imp.imported === spec.imported.name
156
- );
157
- if (bannedImportIndex !== -1) {
158
- importSpecifiersToRemove.push(spec);
159
- }
160
- }
161
- }
162
- }
163
- if (importSpecifiersToRemove.length > 0) {
164
- appliedChanges = true;
165
- n.specifiers = n.specifiers.filter(
166
- (spec) => !importSpecifiersToRemove.includes(spec)
167
- );
168
- if (n.specifiers.length === 0) {
169
- importDeclarationsToRemove.push(n);
170
- }
171
- }
172
- }
173
- }
174
- }
175
- imports.required.forEach((requiredImport) => {
176
- if (requiredImport.specifiers.length > 0) {
177
- appliedChanges = true;
178
- if (importStatementCandidates.length > 0) {
179
- const importStatement2 = importStatementCandidates.find(
180
- (importStatement3) => {
181
- if (importStatement3.source.value === requiredImport.source) {
182
- const importKind = importStatement3.importKind || "value";
183
- const requiredImportKind = requiredImport.importKind || "value";
184
- return importKind === requiredImportKind;
185
- }
186
- return false;
187
- }
188
- );
189
- if (importStatement2) {
190
- if (importStatement2.specifiers === void 0) {
191
- importStatement2.specifiers = [];
192
- }
193
- const importSpecifiersToAdd = requiredImport.specifiers.map(
194
- (spec) => b.importSpecifier(
195
- b.identifier(spec.imported),
196
- b.identifier(spec.imported)
197
- )
198
- );
199
- importStatement2.specifiers = [
200
- ...importStatement2.specifiers,
201
- ...importSpecifiersToAdd
202
- ];
203
- return;
204
- }
205
- }
206
- const importStatement = b.importDeclaration(
207
- requiredImport.specifiers.map(
208
- (spec) => b.importSpecifier(
209
- b.identifier(spec.imported),
210
- spec.local ? b.identifier(spec.local) : null
211
- )
212
- ),
213
- b.stringLiteral(requiredImport.source)
214
- );
215
- program.body.unshift(importStatement);
216
- }
217
- });
218
- if (importDeclarationsToRemove.length > 0) {
219
- appliedChanges = true;
220
- for (const importDeclaration of importDeclarationsToRemove) {
221
- if (((_d = importDeclaration.specifiers) == null ? void 0 : _d.length) === 0) {
222
- const index = program.body.indexOf(importDeclaration);
223
- if (index !== -1) {
224
- program.body.splice(index, 1);
225
- }
226
- }
227
- }
228
- }
229
- if (!appliedChanges) {
230
- return {
231
- exports: foundExports,
232
- result: "not-modified"
233
- };
234
- }
235
- const printResult = print(ast, {
236
- reuseWhitespace: true,
237
- sourceMapName: "output.map"
238
- });
239
- let transformedCode = printResult.code;
240
- if (printResult.map) {
241
- const fixedOutput = await fixTransformedOutputText({
242
- originalCode: source,
243
- transformedCode,
244
- sourceMap: printResult.map,
245
- preferredQuote
246
- });
247
- transformedCode = fixedOutput;
248
- }
249
- return {
250
- result: "modified",
251
- exports: foundExports,
252
- output: transformedCode
253
- };
254
- }
255
- async function fixTransformedOutputText({
256
- originalCode,
257
- transformedCode,
258
- sourceMap,
259
- preferredQuote
260
- }) {
261
- const originalLines = originalCode.split("\n");
262
- const transformedLines = transformedCode.split("\n");
263
- const defaultUsesSemicolons = detectSemicolonUsage(originalCode);
264
- const consumer = await new SourceMapConsumer(sourceMap);
265
- const fixedLines = transformedLines.map((line, i) => {
266
- const transformedLineNum = i + 1;
267
- let origLineText = void 0;
268
- for (let col = 0; col < line.length; col++) {
269
- const mapped = consumer.originalPositionFor({
270
- line: transformedLineNum,
271
- column: col
272
- });
273
- if (mapped.line != null && mapped.line > 0) {
274
- origLineText = originalLines[mapped.line - 1];
275
- break;
276
- }
277
- }
278
- if (origLineText !== void 0) {
279
- if (origLineText === line) {
280
- return origLineText;
281
- }
282
- return fixLine(line, {
283
- originalLine: origLineText,
284
- useOriginalSemicolon: true,
285
- useOriginalQuotes: true,
286
- fallbackQuote: preferredQuote
287
- });
288
- } else {
289
- return fixLine(line, {
290
- originalLine: null,
291
- useOriginalSemicolon: false,
292
- useOriginalQuotes: false,
293
- fallbackQuote: preferredQuote,
294
- fallbackSemicolon: defaultUsesSemicolons
295
- });
296
- }
297
- });
298
- return fixedLines.join("\n");
299
- }
300
- function fixLine(line, {
301
- originalLine,
302
- useOriginalSemicolon,
303
- useOriginalQuotes,
304
- fallbackQuote,
305
- fallbackSemicolon = true
306
- }) {
307
- let result = line;
308
- if (useOriginalQuotes && originalLine) {
309
- result = fixQuotes(result, originalLine, fallbackQuote);
310
- } else if (!useOriginalQuotes && fallbackQuote) {
311
- result = fixQuotesToPreferred(result, fallbackQuote);
312
- }
313
- if (useOriginalSemicolon && originalLine) {
314
- const hadSemicolon = originalLine.trimEnd().endsWith(";");
315
- const hasSemicolon = result.trimEnd().endsWith(";");
316
- if (hadSemicolon && !hasSemicolon) result += ";";
317
- if (!hadSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
318
- } else if (!useOriginalSemicolon) {
319
- const hasSemicolon = result.trimEnd().endsWith(";");
320
- if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
321
- if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ";";
322
- }
323
- return result;
324
- }
325
- function fixQuotes(line, originalLine, fallbackQuote) {
326
- let originalQuote = detectQuoteFromLine(originalLine);
327
- if (!originalQuote) {
328
- originalQuote = fallbackQuote;
329
- }
330
- return fixQuotesToPreferred(line, originalQuote);
331
- }
332
- function fixQuotesToPreferred(line, quote) {
333
- return line.replace(
334
- /(['"`])([^'"`\\]*(?:\\.[^'"`\\]*)*)\1/g,
335
- (_, q, content) => {
336
- const escaped = content.replaceAll(quote, `\\${quote}`);
337
- return `${quote}${escaped}${quote}`;
338
- }
339
- );
340
- }
341
- function detectQuoteFromLine(line) {
342
- const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
343
- return match ? match[1] : null;
344
- }
345
- function detectSemicolonUsage(code) {
346
- const lines = code.split("\n").map((l) => l.trim());
347
- const total = lines.length;
348
- const withSemis = lines.filter((l) => l.endsWith(";")).length;
349
- return withSemis > total / 2;
350
- }
351
- function detectPreferredQuoteStyle(ast) {
352
- let single = 0;
353
- let double = 0;
354
- visit(ast, {
355
- visitStringLiteral(path) {
356
- var _a;
357
- if (path.parent.node.type !== "JSXAttribute") {
358
- const raw = (_a = path.node.extra) == null ? void 0 : _a.raw;
359
- if (raw == null ? void 0 : raw.startsWith("'")) single++;
360
- else if (raw == null ? void 0 : raw.startsWith('"')) double++;
361
- }
362
- return false;
363
- }
364
- });
365
- if (single >= double) {
366
- return "'";
367
- }
368
- return '"';
369
- }
370
- export {
371
- detectPreferredQuoteStyle,
372
- transform
373
- };
374
- //# sourceMappingURL=transform.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transform.js","sources":["../../../src/transform/transform.ts"],"sourcesContent":["import { parseAst } from '@tanstack/router-utils'\nimport { parse, print, types, visit } from 'recast'\nimport { SourceMapConsumer } from 'source-map'\nimport { mergeImportDeclarations } from '../utils'\nimport type { ImportDeclaration } from '../types'\nimport type { RawSourceMap } from 'source-map'\nimport type {\n TransformOptions,\n TransformPlugin,\n TransformResult,\n} from './types'\n\nconst b = types.builders\n\nexport async function transform({\n ctx,\n source,\n plugins,\n}: TransformOptions): Promise<TransformResult> {\n let appliedChanges = false as boolean\n let ast: types.namedTypes.File\n const foundExports: Array<string> = []\n try {\n ast = parse(source, {\n sourceFileName: 'output.ts',\n parser: {\n parse(code: string) {\n return parseAst({\n code,\n // we need to instruct babel to produce tokens,\n // otherwise recast will try to generate the tokens via its own parser and will fail\n tokens: true,\n })\n },\n },\n })\n } catch (e) {\n console.error('Error parsing code', ctx.routeId, source, e)\n return {\n result: 'error',\n error: e,\n }\n }\n\n const preferredQuote = detectPreferredQuoteStyle(ast)\n\n const registeredExports = new Map</* export name */ string, TransformPlugin>()\n\n for (const plugin of plugins ?? []) {\n const exportName = plugin.exportName\n if (registeredExports.has(exportName)) {\n throw new Error(\n `Export ${exportName} is already registered by plugin ${registeredExports.get(exportName)?.name}`,\n )\n }\n registeredExports.set(exportName, plugin)\n }\n\n function onExportFound(\n decl: types.namedTypes.VariableDeclarator,\n exportName: string,\n plugin: TransformPlugin,\n ) {\n const pluginAppliedChanges = plugin.onExportFound({\n decl,\n ctx: { ...ctx, preferredQuote },\n })\n if (pluginAppliedChanges) {\n appliedChanges = true\n }\n\n // export is handled, remove it from the registered exports\n registeredExports.delete(exportName)\n // store the export so we can later return it once the file is transformed\n foundExports.push(exportName)\n }\n\n const program: types.namedTypes.Program = ast.program\n // first pass: find registered exports\n for (const n of program.body) {\n if (registeredExports.size > 0 && n.type === 'ExportNamedDeclaration') {\n // direct export of a variable declaration, e.g. `export const Route = createFileRoute('/path')`\n if (n.declaration?.type === 'VariableDeclaration') {\n const decl = n.declaration.declarations[0]\n if (\n decl &&\n decl.type === 'VariableDeclarator' &&\n decl.id.type === 'Identifier'\n ) {\n const plugin = registeredExports.get(decl.id.name)\n if (plugin) {\n onExportFound(decl, decl.id.name, plugin)\n }\n }\n }\n // this is an export without a declaration, e.g. `export { Route }`\n else if (n.declaration === null && n.specifiers) {\n for (const spec of n.specifiers) {\n if (typeof spec.exported.name === 'string') {\n const plugin = registeredExports.get(spec.exported.name)\n if (plugin) {\n const variableName = spec.local?.name || spec.exported.name\n // find the matching variable declaration by iterating over the top-level declarations\n for (const decl of program.body) {\n if (\n decl.type === 'VariableDeclaration' &&\n decl.declarations[0]\n ) {\n const variable = decl.declarations[0]\n if (\n variable.type === 'VariableDeclarator' &&\n variable.id.type === 'Identifier' &&\n variable.id.name === variableName\n ) {\n onExportFound(variable, spec.exported.name, plugin)\n break\n }\n }\n }\n }\n }\n }\n }\n }\n }\n\n const imports: {\n required: Array<ImportDeclaration>\n banned: Array<ImportDeclaration>\n } = {\n required: [],\n banned: [],\n }\n\n for (const plugin of plugins ?? []) {\n const exportName = plugin.exportName\n if (foundExports.includes(exportName)) {\n const pluginImports = plugin.imports(ctx)\n if (pluginImports.required) {\n imports.required.push(...pluginImports.required)\n }\n if (pluginImports.banned) {\n imports.banned.push(...pluginImports.banned)\n }\n }\n }\n\n imports.required = mergeImportDeclarations(imports.required)\n imports.banned = mergeImportDeclarations(imports.banned)\n\n const importStatementCandidates: Array<types.namedTypes.ImportDeclaration> =\n []\n const importDeclarationsToRemove: Array<types.namedTypes.ImportDeclaration> =\n []\n\n // second pass: apply import rules, but only if a matching export for the plugin was found\n for (const n of program.body) {\n const findImport =\n (opts: { source: string; importKind?: 'type' | 'value' | 'typeof' }) =>\n (i: ImportDeclaration) => {\n if (i.source === opts.source) {\n const importKind = i.importKind || 'value'\n const expectedImportKind = opts.importKind || 'value'\n return expectedImportKind === importKind\n }\n return false\n }\n if (n.type === 'ImportDeclaration' && typeof n.source.value === 'string') {\n const filterImport = findImport({\n source: n.source.value,\n importKind: n.importKind,\n })\n let requiredImports = imports.required.filter(filterImport)[0]\n\n const bannedImports = imports.banned.filter(filterImport)[0]\n if (!requiredImports && !bannedImports) {\n continue\n }\n const importSpecifiersToRemove: types.namedTypes.ImportDeclaration['specifiers'] =\n []\n if (n.specifiers) {\n for (const spec of n.specifiers) {\n if (!requiredImports && !bannedImports) {\n break\n }\n if (\n spec.type === 'ImportSpecifier' &&\n typeof spec.imported.name === 'string'\n ) {\n if (requiredImports) {\n const requiredImportIndex = requiredImports.specifiers.findIndex(\n (imp) => imp.imported === spec.imported.name,\n )\n if (requiredImportIndex !== -1) {\n // import is already present, remove it from requiredImports\n requiredImports.specifiers.splice(requiredImportIndex, 1)\n if (requiredImports.specifiers.length === 0) {\n imports.required = imports.required.splice(\n imports.required.indexOf(requiredImports),\n 1,\n )\n requiredImports = undefined\n }\n } else {\n // add the import statement to the candidates\n importStatementCandidates.push(n)\n }\n }\n if (bannedImports) {\n const bannedImportIndex = bannedImports.specifiers.findIndex(\n (imp) => imp.imported === spec.imported.name,\n )\n if (bannedImportIndex !== -1) {\n importSpecifiersToRemove.push(spec)\n }\n }\n }\n }\n if (importSpecifiersToRemove.length > 0) {\n appliedChanges = true\n n.specifiers = n.specifiers.filter(\n (spec) => !importSpecifiersToRemove.includes(spec),\n )\n\n // mark the import statement as to be deleted if it is now empty\n if (n.specifiers.length === 0) {\n importDeclarationsToRemove.push(n)\n }\n }\n }\n }\n }\n imports.required.forEach((requiredImport) => {\n if (requiredImport.specifiers.length > 0) {\n appliedChanges = true\n if (importStatementCandidates.length > 0) {\n // find the first import statement that matches both the module and the import kind\n const importStatement = importStatementCandidates.find(\n (importStatement) => {\n if (importStatement.source.value === requiredImport.source) {\n const importKind = importStatement.importKind || 'value'\n const requiredImportKind = requiredImport.importKind || 'value'\n return importKind === requiredImportKind\n }\n return false\n },\n )\n if (importStatement) {\n if (importStatement.specifiers === undefined) {\n importStatement.specifiers = []\n }\n const importSpecifiersToAdd = requiredImport.specifiers.map((spec) =>\n b.importSpecifier(\n b.identifier(spec.imported),\n b.identifier(spec.imported),\n ),\n )\n importStatement.specifiers = [\n ...importStatement.specifiers,\n ...importSpecifiersToAdd,\n ]\n return\n }\n }\n const importStatement = b.importDeclaration(\n requiredImport.specifiers.map((spec) =>\n b.importSpecifier(\n b.identifier(spec.imported),\n spec.local ? b.identifier(spec.local) : null,\n ),\n ),\n b.stringLiteral(requiredImport.source),\n )\n program.body.unshift(importStatement)\n }\n })\n if (importDeclarationsToRemove.length > 0) {\n appliedChanges = true\n for (const importDeclaration of importDeclarationsToRemove) {\n // check if the import declaration is still empty\n if (importDeclaration.specifiers?.length === 0) {\n const index = program.body.indexOf(importDeclaration)\n if (index !== -1) {\n program.body.splice(index, 1)\n }\n }\n }\n }\n\n if (!appliedChanges) {\n return {\n exports: foundExports,\n result: 'not-modified',\n }\n }\n\n const printResult = print(ast, {\n reuseWhitespace: true,\n sourceMapName: 'output.map',\n })\n let transformedCode = printResult.code\n if (printResult.map) {\n const fixedOutput = await fixTransformedOutputText({\n originalCode: source,\n transformedCode,\n sourceMap: printResult.map as RawSourceMap,\n preferredQuote,\n })\n transformedCode = fixedOutput\n }\n return {\n result: 'modified',\n exports: foundExports,\n output: transformedCode,\n }\n}\n\nasync function fixTransformedOutputText({\n originalCode,\n transformedCode,\n sourceMap,\n preferredQuote,\n}: {\n originalCode: string\n transformedCode: string\n sourceMap: RawSourceMap\n preferredQuote: '\"' | \"'\"\n}) {\n const originalLines = originalCode.split('\\n')\n const transformedLines = transformedCode.split('\\n')\n\n const defaultUsesSemicolons = detectSemicolonUsage(originalCode)\n\n const consumer = await new SourceMapConsumer(sourceMap)\n\n const fixedLines = transformedLines.map((line, i) => {\n const transformedLineNum = i + 1\n\n let origLineText: string | undefined = undefined\n\n for (let col = 0; col < line.length; col++) {\n const mapped = consumer.originalPositionFor({\n line: transformedLineNum,\n column: col,\n })\n if (mapped.line != null && mapped.line > 0) {\n origLineText = originalLines[mapped.line - 1]\n break\n }\n }\n\n if (origLineText !== undefined) {\n if (origLineText === line) {\n return origLineText\n }\n return fixLine(line, {\n originalLine: origLineText,\n useOriginalSemicolon: true,\n useOriginalQuotes: true,\n fallbackQuote: preferredQuote,\n })\n } else {\n return fixLine(line, {\n originalLine: null,\n useOriginalSemicolon: false,\n useOriginalQuotes: false,\n fallbackQuote: preferredQuote,\n fallbackSemicolon: defaultUsesSemicolons,\n })\n }\n })\n\n return fixedLines.join('\\n')\n}\n\nfunction fixLine(\n line: string,\n {\n originalLine,\n useOriginalSemicolon,\n useOriginalQuotes,\n fallbackQuote,\n fallbackSemicolon = true,\n }: {\n originalLine: string | null\n useOriginalSemicolon: boolean\n useOriginalQuotes: boolean\n fallbackQuote: string\n fallbackSemicolon?: boolean\n },\n) {\n let result = line\n\n if (useOriginalQuotes && originalLine) {\n result = fixQuotes(result, originalLine, fallbackQuote)\n } else if (!useOriginalQuotes && fallbackQuote) {\n result = fixQuotesToPreferred(result, fallbackQuote)\n }\n\n if (useOriginalSemicolon && originalLine) {\n const hadSemicolon = originalLine.trimEnd().endsWith(';')\n const hasSemicolon = result.trimEnd().endsWith(';')\n if (hadSemicolon && !hasSemicolon) result += ';'\n if (!hadSemicolon && hasSemicolon) result = result.replace(/;\\s*$/, '')\n } else if (!useOriginalSemicolon) {\n const hasSemicolon = result.trimEnd().endsWith(';')\n if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\\s*$/, '')\n if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ';'\n }\n\n return result\n}\n\nfunction fixQuotes(line: string, originalLine: string, fallbackQuote: string) {\n let originalQuote = detectQuoteFromLine(originalLine)\n if (!originalQuote) {\n originalQuote = fallbackQuote\n }\n return fixQuotesToPreferred(line, originalQuote)\n}\n\nfunction fixQuotesToPreferred(line: string, quote: string) {\n // Replace existing quotes with preferred quote\n return line.replace(\n /(['\"`])([^'\"`\\\\]*(?:\\\\.[^'\"`\\\\]*)*)\\1/g,\n (_, q, content) => {\n const escaped = content.replaceAll(quote, `\\\\${quote}`)\n return `${quote}${escaped}${quote}`\n },\n )\n}\n\nfunction detectQuoteFromLine(line: string) {\n const match = line.match(/(['\"`])(?:\\\\.|[^\\\\])*?\\1/)\n return match ? match[1] : null\n}\n\nfunction detectSemicolonUsage(code: string) {\n const lines = code.split('\\n').map((l) => l.trim())\n const total = lines.length\n const withSemis = lines.filter((l) => l.endsWith(';')).length\n return withSemis > total / 2\n}\n\nexport function detectPreferredQuoteStyle(ast: types.ASTNode): \"'\" | '\"' {\n let single = 0\n let double = 0\n\n visit(ast, {\n visitStringLiteral(path) {\n if (path.parent.node.type !== 'JSXAttribute') {\n const raw = path.node.extra?.raw\n if (raw?.startsWith(\"'\")) single++\n else if (raw?.startsWith('\"')) double++\n }\n return false\n },\n })\n\n if (single >= double) {\n return \"'\"\n }\n return '\"'\n}\n"],"names":["importStatement"],"mappings":";;;;AAYA,MAAM,IAAI,MAAM;AAEhB,eAAsB,UAAU;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,GAA+C;;AAC7C,MAAI,iBAAiB;AACjB,MAAA;AACJ,QAAM,eAA8B,CAAC;AACjC,MAAA;A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AAAA,EACT,CACD;AAED,MAAI,UAAU,QAAQ;AACb,WAAA;AAAA,EAAA;AAEF,SAAA;AACT;"}
@@ -1,43 +0,0 @@
1
- import { ImportDeclaration } from '../types.js';
2
- import { types } from 'recast';
3
- import { Config } from '../config.js';
4
- export interface TransformOptions {
5
- source: string;
6
- ctx: TransformContext;
7
- plugins?: Array<TransformPlugin>;
8
- }
9
- export type TransformResult = {
10
- result: 'not-modified';
11
- exports: Array<string>;
12
- } | {
13
- result: 'modified';
14
- output: string;
15
- exports: Array<string>;
16
- } | {
17
- result: 'error';
18
- error?: any;
19
- };
20
- export interface TransformImportsConfig {
21
- banned?: Array<ImportDeclaration>;
22
- required?: Array<ImportDeclaration>;
23
- }
24
- export interface TransformPlugin {
25
- name: string;
26
- exportName: string;
27
- imports: (ctx: TransformContext) => TransformImportsConfig;
28
- /**
29
- * Called after the export is found in the AST.
30
- * @returns true if the plugin modified the AST, false otherwise
31
- */
32
- onExportFound: (opts: {
33
- decl: types.namedTypes.VariableDeclarator;
34
- ctx: TransformContext;
35
- }) => boolean;
36
- }
37
- export interface TransformContext {
38
- target: Config['target'];
39
- routeId: string;
40
- lazy: boolean;
41
- verboseFileRoutes: boolean;
42
- preferredQuote?: '"' | "'";
43
- }
@@ -1,2 +0,0 @@
1
- import { types } from 'recast';
2
- export declare function ensureStringArgument(callExpression: types.namedTypes.CallExpression, value: string, preferredQuote?: "'" | '"'): boolean;
@@ -1,36 +0,0 @@
1
- import { types } from "recast";
2
- const b = types.builders;
3
- function ensureStringArgument(callExpression, value, preferredQuote) {
4
- const argument = callExpression.arguments[0];
5
- if (!argument) {
6
- let stringLiteral;
7
- if (!preferredQuote) {
8
- stringLiteral = b.stringLiteral.from({ value });
9
- } else {
10
- stringLiteral = b.stringLiteral.from({
11
- value,
12
- extra: {
13
- rawValue: value,
14
- raw: `${preferredQuote}${value}${preferredQuote}`
15
- }
16
- });
17
- }
18
- callExpression.arguments.push(stringLiteral);
19
- return true;
20
- } else if (argument.type === "StringLiteral") {
21
- if (argument.value !== value) {
22
- argument.value = value;
23
- return true;
24
- }
25
- } else if (argument.type === "TemplateLiteral") {
26
- if (argument.quasis.length === 1 && argument.quasis[0] && argument.quasis[0].value.raw !== value) {
27
- argument.quasis[0].value.raw = value;
28
- return true;
29
- }
30
- }
31
- return false;
32
- }
33
- export {
34
- ensureStringArgument
35
- };
36
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sources":["../../../src/transform/utils.ts"],"sourcesContent":["import { types } from 'recast'\n\nconst b = types.builders\n\nexport function ensureStringArgument(\n callExpression: types.namedTypes.CallExpression,\n value: string,\n preferredQuote?: \"'\" | '\"',\n) {\n const argument = callExpression.arguments[0]\n if (!argument) {\n let stringLiteral: types.namedTypes.StringLiteral\n if (!preferredQuote) {\n stringLiteral = b.stringLiteral.from({ value })\n } else {\n stringLiteral = b.stringLiteral.from({\n value,\n extra: {\n rawValue: value,\n raw: `${preferredQuote}${value}${preferredQuote}`,\n },\n })\n }\n callExpression.arguments.push(stringLiteral)\n return true\n } else if (argument.type === 'StringLiteral') {\n if (argument.value !== value) {\n argument.value = value\n return true\n }\n } else if (argument.type === 'TemplateLiteral') {\n if (\n argument.quasis.length === 1 &&\n argument.quasis[0] &&\n argument.quasis[0].value.raw !== value\n ) {\n argument.quasis[0].value.raw = value\n return true\n }\n }\n return false\n}\n"],"names":[],"mappings":";AAEA,MAAM,IAAI,MAAM;AAEA,SAAA,qBACd,gBACA,OACA,gBACA;AACM,QAAA,WAAW,eAAe,UAAU,CAAC;AAC3C,MAAI,CAAC,UAAU;AACT,QAAA;AACJ,QAAI,CAAC,gBAAgB;AACnB,sBAAgB,EAAE,cAAc,KAAK,EAAE,OAAO;AAAA,IAAA,OACzC;AACW,sBAAA,EAAE,cAAc,KAAK;AAAA,QACnC;AAAA,QACA,OAAO;AAAA,UACL,UAAU;AAAA,UACV,KAAK,GAAG,cAAc,GAAG,KAAK,GAAG,cAAc;AAAA,QAAA;AAAA,MACjD,CACD;AAAA,IAAA;AAEY,mBAAA,UAAU,KAAK,aAAa;AACpC,WAAA;AAAA,EAAA,WACE,SAAS,SAAS,iBAAiB;AACxC,QAAA,SAAS,UAAU,OAAO;AAC5B,eAAS,QAAQ;AACV,aAAA;AAAA,IAAA;AAAA,EACT,WACS,SAAS,SAAS,mBAAmB;AAC9C,QACE,SAAS,OAAO,WAAW,KAC3B,SAAS,OAAO,CAAC,KACjB,SAAS,OAAO,CAAC,EAAE,MAAM,QAAQ,OACjC;AACA,eAAS,OAAO,CAAC,EAAE,MAAM,MAAM;AACxB,aAAA;AAAA,IAAA;AAAA,EACT;AAEK,SAAA;AACT;"}
@@ -1,47 +0,0 @@
1
- export type RouteNode = {
2
- filePath: string;
3
- fullPath: string;
4
- variableName: string;
5
- _fsRouteType: FsRouteType;
6
- routePath?: string;
7
- cleanedPath?: string;
8
- path?: string;
9
- isNonPath?: boolean;
10
- isVirtualParentRequired?: boolean;
11
- isVirtualParentRoute?: boolean;
12
- isVirtual?: boolean;
13
- children?: Array<RouteNode>;
14
- parent?: RouteNode;
15
- exports?: Array<string>;
16
- };
17
- export interface GetRouteNodesResult {
18
- rootRouteNode?: RouteNode;
19
- routeNodes: Array<RouteNode>;
20
- physicalDirectories: Array<string>;
21
- }
22
- export type FsRouteType = '__root' | 'static' | 'layout' | 'pathless_layout' | 'lazy' | 'loader' | 'component' | 'pendingComponent' | 'errorComponent';
23
- export type RouteSubNode = {
24
- component?: RouteNode;
25
- errorComponent?: RouteNode;
26
- pendingComponent?: RouteNode;
27
- loader?: RouteNode;
28
- lazy?: RouteNode;
29
- };
30
- export type ImportSpecifier = {
31
- imported: string;
32
- local?: string;
33
- };
34
- export type ImportDeclaration = {
35
- source: string;
36
- specifiers: Array<ImportSpecifier>;
37
- importKind?: 'type' | 'value';
38
- };
39
- export type HandleNodeAccumulator = {
40
- routeTree: Array<RouteNode>;
41
- routePiecesByPath: Record<string, RouteSubNode>;
42
- routeNodes: Array<RouteNode>;
43
- };
44
- export type GetRoutesByFileMapResultValue = {
45
- routePath: string;
46
- };
47
- export type GetRoutesByFileMapResult = Map<string, GetRoutesByFileMapResultValue>;