@tanstack/router-generator 1.132.0-alpha.9 → 1.132.2
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 +6 -2
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/config.d.cts +12 -9
- package/dist/cjs/generator.cjs +264 -316
- package/dist/cjs/generator.cjs.map +1 -1
- package/dist/cjs/generator.d.cts +20 -7
- package/dist/cjs/index.cjs +0 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +4 -4
- package/dist/cjs/plugin/types.d.cts +10 -38
- package/dist/cjs/transform/transform.cjs +108 -40
- package/dist/cjs/transform/transform.cjs.map +1 -1
- package/dist/cjs/transform/transform.d.cts +1 -1
- package/dist/cjs/transform/types.d.cts +4 -18
- package/dist/cjs/types.d.cts +1 -1
- package/dist/cjs/utils.cjs +55 -39
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +5 -5
- package/dist/esm/config.d.ts +12 -9
- package/dist/esm/config.js +6 -2
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/generator.d.ts +20 -7
- package/dist/esm/generator.js +266 -318
- package/dist/esm/generator.js.map +1 -1
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +1 -2
- package/dist/esm/plugin/types.d.ts +10 -38
- package/dist/esm/transform/transform.d.ts +1 -1
- package/dist/esm/transform/transform.js +106 -38
- package/dist/esm/transform/transform.js.map +1 -1
- package/dist/esm/transform/types.d.ts +4 -18
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/utils.d.ts +5 -5
- package/dist/esm/utils.js +55 -39
- package/dist/esm/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/config.ts +7 -1
- package/src/generator.ts +306 -366
- package/src/index.ts +2 -7
- package/src/plugin/types.ts +11 -44
- package/src/transform/transform.ts +118 -53
- package/src/transform/types.ts +5 -18
- package/src/types.ts +1 -1
- package/src/utils.ts +85 -70
- package/dist/cjs/plugin/default-generator-plugin.cjs +0 -94
- package/dist/cjs/plugin/default-generator-plugin.cjs.map +0 -1
- package/dist/cjs/plugin/default-generator-plugin.d.cts +0 -2
- package/dist/cjs/transform/default-transform-plugin.cjs +0 -97
- package/dist/cjs/transform/default-transform-plugin.cjs.map +0 -1
- package/dist/cjs/transform/default-transform-plugin.d.cts +0 -2
- package/dist/esm/plugin/default-generator-plugin.d.ts +0 -2
- package/dist/esm/plugin/default-generator-plugin.js +0 -94
- package/dist/esm/plugin/default-generator-plugin.js.map +0 -1
- package/dist/esm/transform/default-transform-plugin.d.ts +0 -2
- package/dist/esm/transform/default-transform-plugin.js +0 -97
- package/dist/esm/transform/default-transform-plugin.js.map +0 -1
- package/src/plugin/default-generator-plugin.ts +0 -109
- package/src/transform/default-transform-plugin.ts +0 -106
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { parseAst } from "@tanstack/router-utils";
|
|
2
|
-
import { parse, types, print
|
|
2
|
+
import { parse, visit, types, print } from "recast";
|
|
3
3
|
import { SourceMapConsumer } from "source-map";
|
|
4
4
|
import { mergeImportDeclarations } from "../utils.js";
|
|
5
|
+
import { ensureStringArgument } from "./utils.js";
|
|
5
6
|
const b = types.builders;
|
|
6
7
|
async function transform({
|
|
7
8
|
ctx,
|
|
8
9
|
source,
|
|
9
|
-
|
|
10
|
+
node
|
|
10
11
|
}) {
|
|
11
12
|
let appliedChanges = false;
|
|
12
13
|
let ast;
|
|
13
|
-
const foundExports = [];
|
|
14
14
|
try {
|
|
15
15
|
ast = parse(source, {
|
|
16
16
|
sourceFileName: "output.ts",
|
|
@@ -33,49 +33,83 @@ async function transform({
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
const preferredQuote = detectPreferredQuoteStyle(ast);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
let routeExportHandled = false;
|
|
37
|
+
function onExportFound(decl) {
|
|
38
|
+
if (decl.init?.type === "CallExpression") {
|
|
39
|
+
const callExpression = decl.init;
|
|
40
|
+
const firstArgument = callExpression.arguments[0];
|
|
41
|
+
if (firstArgument) {
|
|
42
|
+
if (firstArgument.type === "ObjectExpression") {
|
|
43
|
+
const staticProperties = firstArgument.properties.flatMap((p) => {
|
|
44
|
+
if (p.type === "ObjectProperty" && p.key.type === "Identifier") {
|
|
45
|
+
return p.key.name;
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
});
|
|
49
|
+
node.createFileRouteProps = new Set(staticProperties);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
let identifier;
|
|
53
|
+
if (callExpression.callee.type === "Identifier") {
|
|
54
|
+
identifier = callExpression.callee;
|
|
55
|
+
if (ctx.verboseFileRoutes) {
|
|
56
|
+
callExpression.callee = b.callExpression(identifier, [
|
|
57
|
+
b.stringLiteral(ctx.routeId)
|
|
58
|
+
]);
|
|
59
|
+
appliedChanges = true;
|
|
60
|
+
}
|
|
61
|
+
} else if (callExpression.callee.type === "CallExpression" && callExpression.callee.callee.type === "Identifier") {
|
|
62
|
+
identifier = callExpression.callee.callee;
|
|
63
|
+
if (!ctx.verboseFileRoutes) {
|
|
64
|
+
callExpression.callee = identifier;
|
|
65
|
+
appliedChanges = true;
|
|
66
|
+
} else {
|
|
67
|
+
appliedChanges = ensureStringArgument(
|
|
68
|
+
callExpression.callee,
|
|
69
|
+
ctx.routeId,
|
|
70
|
+
ctx.preferredQuote
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (identifier === void 0) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`expected identifier to be present in ${ctx.routeId} for export "Route"`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (identifier.name === "createFileRoute" && ctx.lazy) {
|
|
80
|
+
identifier.name = "createLazyFileRoute";
|
|
81
|
+
appliedChanges = true;
|
|
82
|
+
} else if (identifier.name === "createLazyFileRoute" && !ctx.lazy) {
|
|
83
|
+
identifier.name = "createFileRoute";
|
|
84
|
+
appliedChanges = true;
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
40
87
|
throw new Error(
|
|
41
|
-
`
|
|
88
|
+
`expected "Route" export to be initialized by a CallExpression`
|
|
42
89
|
);
|
|
43
90
|
}
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
function onExportFound(decl, exportName, plugin) {
|
|
47
|
-
const pluginAppliedChanges = plugin.onExportFound({
|
|
48
|
-
decl,
|
|
49
|
-
ctx: { ...ctx, preferredQuote }
|
|
50
|
-
});
|
|
51
|
-
if (pluginAppliedChanges) {
|
|
52
|
-
appliedChanges = true;
|
|
53
|
-
}
|
|
54
|
-
registeredExports.delete(exportName);
|
|
55
|
-
foundExports.push(exportName);
|
|
91
|
+
routeExportHandled = true;
|
|
56
92
|
}
|
|
57
93
|
const program = ast.program;
|
|
58
94
|
for (const n of program.body) {
|
|
59
|
-
if (
|
|
95
|
+
if (n.type === "ExportNamedDeclaration") {
|
|
60
96
|
if (n.declaration?.type === "VariableDeclaration") {
|
|
61
97
|
const decl = n.declaration.declarations[0];
|
|
62
98
|
if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
onExportFound(decl, decl.id.name, plugin);
|
|
99
|
+
if (decl.id.name === "Route") {
|
|
100
|
+
onExportFound(decl);
|
|
66
101
|
}
|
|
67
102
|
}
|
|
68
103
|
} else if (n.declaration === null && n.specifiers) {
|
|
69
104
|
for (const spec of n.specifiers) {
|
|
70
105
|
if (typeof spec.exported.name === "string") {
|
|
71
|
-
|
|
72
|
-
if (plugin) {
|
|
106
|
+
if (spec.exported.name === "Route") {
|
|
73
107
|
const variableName = spec.local?.name || spec.exported.name;
|
|
74
108
|
for (const decl of program.body) {
|
|
75
109
|
if (decl.type === "VariableDeclaration" && decl.declarations[0]) {
|
|
76
110
|
const variable = decl.declarations[0];
|
|
77
111
|
if (variable.type === "VariableDeclarator" && variable.id.type === "Identifier" && variable.id.name === variableName) {
|
|
78
|
-
onExportFound(variable
|
|
112
|
+
onExportFound(variable);
|
|
79
113
|
break;
|
|
80
114
|
}
|
|
81
115
|
}
|
|
@@ -85,21 +119,57 @@ async function transform({
|
|
|
85
119
|
}
|
|
86
120
|
}
|
|
87
121
|
}
|
|
122
|
+
if (routeExportHandled) {
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (!routeExportHandled) {
|
|
127
|
+
return {
|
|
128
|
+
result: "no-route-export"
|
|
129
|
+
};
|
|
88
130
|
}
|
|
89
131
|
const imports = {
|
|
90
132
|
required: [],
|
|
91
133
|
banned: []
|
|
92
134
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
135
|
+
const targetModule = `@tanstack/${ctx.target}-router`;
|
|
136
|
+
if (ctx.verboseFileRoutes === false) {
|
|
137
|
+
imports.banned = [
|
|
138
|
+
{
|
|
139
|
+
source: targetModule,
|
|
140
|
+
specifiers: [
|
|
141
|
+
{ imported: "createLazyFileRoute" },
|
|
142
|
+
{ imported: "createFileRoute" }
|
|
143
|
+
]
|
|
102
144
|
}
|
|
145
|
+
];
|
|
146
|
+
} else {
|
|
147
|
+
if (ctx.lazy) {
|
|
148
|
+
imports.required = [
|
|
149
|
+
{
|
|
150
|
+
source: targetModule,
|
|
151
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
152
|
+
}
|
|
153
|
+
];
|
|
154
|
+
imports.banned = [
|
|
155
|
+
{
|
|
156
|
+
source: targetModule,
|
|
157
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
158
|
+
}
|
|
159
|
+
];
|
|
160
|
+
} else {
|
|
161
|
+
imports.required = [
|
|
162
|
+
{
|
|
163
|
+
source: targetModule,
|
|
164
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
165
|
+
}
|
|
166
|
+
];
|
|
167
|
+
imports.banned = [
|
|
168
|
+
{
|
|
169
|
+
source: targetModule,
|
|
170
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
171
|
+
}
|
|
172
|
+
];
|
|
103
173
|
}
|
|
104
174
|
}
|
|
105
175
|
imports.required = mergeImportDeclarations(imports.required);
|
|
@@ -227,7 +297,6 @@ async function transform({
|
|
|
227
297
|
}
|
|
228
298
|
if (!appliedChanges) {
|
|
229
299
|
return {
|
|
230
|
-
exports: foundExports,
|
|
231
300
|
result: "not-modified"
|
|
232
301
|
};
|
|
233
302
|
}
|
|
@@ -247,7 +316,6 @@ async function transform({
|
|
|
247
316
|
}
|
|
248
317
|
return {
|
|
249
318
|
result: "modified",
|
|
250
|
-
exports: foundExports,
|
|
251
319
|
output: transformedCode
|
|
252
320
|
};
|
|
253
321
|
}
|
|
@@ -1 +1 @@
|
|
|
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;AAC7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,GAAG,EAAG;AAAA,MACjC;AACA,aAAO;AAAA,IACT;AAAA,EAAA,CACD;AAED,MAAI,UAAU,QAAQ;AACpB,WAAO;AAAA,EACT;AACA,SAAO;AACT;"}
|
|
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 { ensureStringArgument } from './utils'\nimport type { ImportDeclaration } from '../types'\nimport type { RawSourceMap } from 'source-map'\nimport type { TransformOptions, TransformResult } from './types'\n\nconst b = types.builders\n\nexport async function transform({\n ctx,\n source,\n node,\n}: TransformOptions): Promise<TransformResult> {\n let appliedChanges = false as boolean\n let ast: types.namedTypes.File\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 let routeExportHandled = false as boolean\n function onExportFound(decl: types.namedTypes.VariableDeclarator) {\n if (decl.init?.type === 'CallExpression') {\n const callExpression = decl.init\n const firstArgument = callExpression.arguments[0]\n if (firstArgument) {\n if (firstArgument.type === 'ObjectExpression') {\n const staticProperties = firstArgument.properties.flatMap((p) => {\n if (p.type === 'ObjectProperty' && p.key.type === 'Identifier') {\n return p.key.name\n }\n return []\n })\n node.createFileRouteProps = new Set(staticProperties)\n }\n }\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(\n `expected identifier to be present in ${ctx.routeId} for export \"Route\"`,\n )\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 } else {\n throw new Error(\n `expected \"Route\" export to be initialized by a CallExpression`,\n )\n }\n routeExportHandled = true\n }\n\n const program: types.namedTypes.Program = ast.program\n // first pass: find Route export\n for (const n of program.body) {\n if (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 if (decl.id.name === 'Route') {\n onExportFound(decl)\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 if (spec.exported.name === 'Route') {\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)\n break\n }\n }\n }\n }\n }\n }\n }\n }\n if (routeExportHandled) {\n break\n }\n }\n\n if (!routeExportHandled) {\n return {\n result: 'no-route-export',\n }\n }\n\n const imports: {\n required: Array<ImportDeclaration>\n banned: Array<ImportDeclaration>\n } = {\n required: [],\n banned: [],\n }\n\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\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 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 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":";;;;;AASA,MAAM,IAAI,MAAM;AAEhB,eAAsB,UAAU;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,GAA+C;AAC7C,MAAI,iBAAiB;AACrB,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,QAAQ;AAAA,MAClB,gBAAgB;AAAA,MAChB,QAAQ;AAAA,QACN,MAAM,MAAc;AAClB,iBAAO,SAAS;AAAA,YACd;AAAA;AAAA;AAAA,YAGA,QAAQ;AAAA,UAAA,CACT;AAAA,QACH;AAAA,MAAA;AAAA,IACF,CACD;AAAA,EACH,SAAS,GAAG;AACV,YAAQ,MAAM,sBAAsB,IAAI,SAAS,QAAQ,CAAC;AAC1D,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,OAAO;AAAA,IAAA;AAAA,EAEX;AAEA,QAAM,iBAAiB,0BAA0B,GAAG;AAEpD,MAAI,qBAAqB;AACzB,WAAS,cAAc,MAA2C;AAChE,QAAI,KAAK,MAAM,SAAS,kBAAkB;AACxC,YAAM,iBAAiB,KAAK;AAC5B,YAAM,gBAAgB,eAAe,UAAU,CAAC;AAChD,UAAI,eAAe;AACjB,YAAI,cAAc,SAAS,oBAAoB;AAC7C,gBAAM,mBAAmB,cAAc,WAAW,QAAQ,CAAC,MAAM;AAC/D,gBAAI,EAAE,SAAS,oBAAoB,EAAE,IAAI,SAAS,cAAc;AAC9D,qBAAO,EAAE,IAAI;AAAA,YACf;AACA,mBAAO,CAAA;AAAA,UACT,CAAC;AACD,e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AAM,QAAQ,MAAM;AACpB,QAAM,YAAY,MAAM,OAAO,CAAC,MAAM,EAAE,SAAS,GAAG,CAAC,EAAE;AACvD,SAAO,YAAY,QAAQ;AAC7B;AAEO,SAAS,0BAA0B,KAA+B;AACvE,MAAI,SAAS;AACb,MAAI,SAAS;AAEb,QAAM,KAAK;AAAA,IACT,mBAAmB,MAAM;AACvB,UAAI,KAAK,OAAO,KAAK,SAAS,gBAAgB;AAC5C,cAAM,MAAM,KAAK,KAAK,OAAO;AAC7B,YAAI,KAAK,WAAW,GAAG,EAAG;AAAA,iBACjB,KAAK,WAAW,GAAG,EAAG;AAAA,MACjC;AACA,aAAO;AAAA,IACT;AAAA,EAAA,CACD;AAED,MAAI,UAAU,QAAQ;AACpB,WAAO;AAAA,EACT;AACA,SAAO;AACT;"}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { ImportDeclaration } from '../types.js';
|
|
2
|
-
import { types } from 'recast';
|
|
1
|
+
import { ImportDeclaration, RouteNode } from '../types.js';
|
|
3
2
|
import { Config } from '../config.js';
|
|
4
3
|
export interface TransformOptions {
|
|
5
4
|
source: string;
|
|
6
5
|
ctx: TransformContext;
|
|
7
|
-
|
|
6
|
+
node: RouteNode;
|
|
8
7
|
}
|
|
9
8
|
export type TransformResult = {
|
|
9
|
+
result: 'no-route-export';
|
|
10
|
+
} | {
|
|
10
11
|
result: 'not-modified';
|
|
11
|
-
exports: Array<string>;
|
|
12
12
|
} | {
|
|
13
13
|
result: 'modified';
|
|
14
14
|
output: string;
|
|
15
|
-
exports: Array<string>;
|
|
16
15
|
} | {
|
|
17
16
|
result: 'error';
|
|
18
17
|
error?: any;
|
|
@@ -21,19 +20,6 @@ export interface TransformImportsConfig {
|
|
|
21
20
|
banned?: Array<ImportDeclaration>;
|
|
22
21
|
required?: Array<ImportDeclaration>;
|
|
23
22
|
}
|
|
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
23
|
export interface TransformContext {
|
|
38
24
|
target: Config['target'];
|
|
39
25
|
routeId: string;
|
package/dist/esm/types.d.ts
CHANGED
package/dist/esm/utils.d.ts
CHANGED
|
@@ -81,7 +81,7 @@ export declare function hasParentRoute(routes: Array<RouteNode>, node: RouteNode
|
|
|
81
81
|
/**
|
|
82
82
|
* Gets the final variable name for a route
|
|
83
83
|
*/
|
|
84
|
-
export declare const getResolvedRouteNodeVariableName: (routeNode: RouteNode
|
|
84
|
+
export declare const getResolvedRouteNodeVariableName: (routeNode: RouteNode) => string;
|
|
85
85
|
/**
|
|
86
86
|
* Checks if a given RouteNode is valid for augmenting it with typing based on conditions.
|
|
87
87
|
* Also asserts that the RouteNode is defined.
|
|
@@ -119,15 +119,15 @@ export declare const inferTo: (routeNode: RouteNode) => string;
|
|
|
119
119
|
*/
|
|
120
120
|
export declare const dedupeBranchesAndIndexRoutes: (routes: Array<RouteNode>) => Array<RouteNode>;
|
|
121
121
|
export declare function checkRouteFullPathUniqueness(_routes: Array<RouteNode>, config: Config): void;
|
|
122
|
-
export declare function buildRouteTreeConfig(nodes: Array<RouteNode>,
|
|
122
|
+
export declare function buildRouteTreeConfig(nodes: Array<RouteNode>, disableTypes: boolean, depth?: number): Array<string>;
|
|
123
123
|
export declare function buildImportString(importDeclaration: ImportDeclaration): string;
|
|
124
124
|
export declare function lowerCaseFirstChar(value: string): string;
|
|
125
125
|
export declare function mergeImportDeclarations(imports: Array<ImportDeclaration>): Array<ImportDeclaration>;
|
|
126
|
-
export declare
|
|
127
|
-
export declare const findParent: (node: RouteNode | undefined, exportName: string) => string;
|
|
126
|
+
export declare const findParent: (node: RouteNode | undefined) => string;
|
|
128
127
|
export declare function buildFileRoutesByPathInterface(opts: {
|
|
129
128
|
routeNodes: Array<RouteNode>;
|
|
130
129
|
module: string;
|
|
131
130
|
interfaceName: string;
|
|
132
|
-
exportName: string;
|
|
133
131
|
}): string;
|
|
132
|
+
export declare function getImportPath(node: RouteNode, config: Config, generatedRouteTreePath: string): string;
|
|
133
|
+
export declare function getImportForRouteNode(node: RouteNode, config: Config, generatedRouteTreePath: string, root: string): ImportDeclaration;
|
package/dist/esm/utils.js
CHANGED
|
@@ -176,8 +176,8 @@ function hasParentRoute(routes, node, routePathToCheck) {
|
|
|
176
176
|
const parentRoutePath = segments.join("/");
|
|
177
177
|
return hasParentRoute(routes, node, parentRoutePath);
|
|
178
178
|
}
|
|
179
|
-
const getResolvedRouteNodeVariableName = (routeNode
|
|
180
|
-
return routeNode.children?.length ? `${routeNode.variableName}
|
|
179
|
+
const getResolvedRouteNodeVariableName = (routeNode) => {
|
|
180
|
+
return routeNode.children?.length ? `${routeNode.variableName}RouteWithChildren` : `${routeNode.variableName}Route`;
|
|
181
181
|
};
|
|
182
182
|
function isRouteNodeValidForAugmentation(routeNode) {
|
|
183
183
|
if (!routeNode || routeNode.isVirtual) {
|
|
@@ -253,8 +253,8 @@ Conflicting files:
|
|
|
253
253
|
throw new Error(errorMessage);
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
-
function buildRouteTreeConfig(nodes,
|
|
257
|
-
const children = nodes.
|
|
256
|
+
function buildRouteTreeConfig(nodes, disableTypes, depth = 1) {
|
|
257
|
+
const children = nodes.map((node) => {
|
|
258
258
|
if (node._fsRouteType === "__root") {
|
|
259
259
|
return;
|
|
260
260
|
}
|
|
@@ -265,21 +265,20 @@ function buildRouteTreeConfig(nodes, exportName, disableTypes, depth = 1) {
|
|
|
265
265
|
if (node.children?.length) {
|
|
266
266
|
const childConfigs = buildRouteTreeConfig(
|
|
267
267
|
node.children,
|
|
268
|
-
exportName,
|
|
269
268
|
disableTypes,
|
|
270
269
|
depth + 1
|
|
271
270
|
);
|
|
272
|
-
const childrenDeclaration = disableTypes ? "" : `interface ${route}
|
|
273
|
-
${node.children.
|
|
274
|
-
(child) => `${child.variableName}
|
|
271
|
+
const childrenDeclaration = disableTypes ? "" : `interface ${route}RouteChildren {
|
|
272
|
+
${node.children.map(
|
|
273
|
+
(child) => `${child.variableName}Route: typeof ${getResolvedRouteNodeVariableName(child)}`
|
|
275
274
|
).join(",")}
|
|
276
275
|
}`;
|
|
277
|
-
const children2 = `const ${route}${
|
|
278
|
-
${node.children.
|
|
279
|
-
(child) => `${child.variableName}
|
|
276
|
+
const children2 = `const ${route}RouteChildren${disableTypes ? "" : `: ${route}RouteChildren`} = {
|
|
277
|
+
${node.children.map(
|
|
278
|
+
(child) => `${child.variableName}Route: ${getResolvedRouteNodeVariableName(child)}`
|
|
280
279
|
).join(",")}
|
|
281
280
|
}`;
|
|
282
|
-
const routeWithChildren = `const ${route}
|
|
281
|
+
const routeWithChildren = `const ${route}RouteWithChildren = ${route}Route._addFileChildren(${route}RouteChildren)`;
|
|
283
282
|
return [
|
|
284
283
|
childConfigs.join("\n"),
|
|
285
284
|
childrenDeclaration,
|
|
@@ -295,12 +294,6 @@ function buildImportString(importDeclaration) {
|
|
|
295
294
|
const { source, specifiers, importKind } = importDeclaration;
|
|
296
295
|
return specifiers.length ? `import ${importKind === "type" ? "type " : ""}{ ${specifiers.map((s) => s.local ? `${s.imported} as ${s.local}` : s.imported).join(", ")} } from '${source}'` : "";
|
|
297
296
|
}
|
|
298
|
-
function lowerCaseFirstChar(value) {
|
|
299
|
-
if (!value[0]) {
|
|
300
|
-
return value;
|
|
301
|
-
}
|
|
302
|
-
return value[0].toLowerCase() + value.slice(1);
|
|
303
|
-
}
|
|
304
297
|
function mergeImportDeclarations(imports) {
|
|
305
298
|
const merged = {};
|
|
306
299
|
for (const imp of imports) {
|
|
@@ -318,36 +311,26 @@ function mergeImportDeclarations(imports) {
|
|
|
318
311
|
}
|
|
319
312
|
return Object.values(merged);
|
|
320
313
|
}
|
|
321
|
-
|
|
322
|
-
return node.children?.some((child) => hasChildWithExport(child)) ?? false;
|
|
323
|
-
}
|
|
324
|
-
const findParent = (node, exportName) => {
|
|
314
|
+
const findParent = (node) => {
|
|
325
315
|
if (!node) {
|
|
326
|
-
return `
|
|
316
|
+
return `rootRouteImport`;
|
|
327
317
|
}
|
|
328
318
|
if (node.parent) {
|
|
329
|
-
if (node.
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
return `${node.parent.variableName}${exportName}`;
|
|
334
|
-
}
|
|
319
|
+
if (node.isVirtualParentRequired) {
|
|
320
|
+
return `${node.parent.variableName}Route`;
|
|
321
|
+
} else {
|
|
322
|
+
return `${node.parent.variableName}Route`;
|
|
335
323
|
}
|
|
336
324
|
}
|
|
337
|
-
return findParent(node.parent
|
|
325
|
+
return findParent(node.parent);
|
|
338
326
|
};
|
|
339
327
|
function buildFileRoutesByPathInterface(opts) {
|
|
340
328
|
return `declare module '${opts.module}' {
|
|
341
329
|
interface ${opts.interfaceName} {
|
|
342
330
|
${opts.routeNodes.map((routeNode) => {
|
|
343
331
|
const filePathId = routeNode.routePath;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
preloaderRoute = `typeof ${routeNode.variableName}${opts.exportName}Import`;
|
|
347
|
-
} else {
|
|
348
|
-
preloaderRoute = "unknown";
|
|
349
|
-
}
|
|
350
|
-
const parent = findParent(routeNode, opts.exportName);
|
|
332
|
+
const preloaderRoute = `typeof ${routeNode.variableName}RouteImport`;
|
|
333
|
+
const parent = findParent(routeNode);
|
|
351
334
|
return `'${filePathId}': {
|
|
352
335
|
id: '${filePathId}'
|
|
353
336
|
path: '${inferPath(routeNode)}'
|
|
@@ -359,6 +342,39 @@ function buildFileRoutesByPathInterface(opts) {
|
|
|
359
342
|
}
|
|
360
343
|
}`;
|
|
361
344
|
}
|
|
345
|
+
function getImportPath(node, config, generatedRouteTreePath) {
|
|
346
|
+
return replaceBackslash(
|
|
347
|
+
removeExt(
|
|
348
|
+
path.relative(
|
|
349
|
+
path.dirname(generatedRouteTreePath),
|
|
350
|
+
path.resolve(config.routesDirectory, node.filePath)
|
|
351
|
+
),
|
|
352
|
+
config.addExtensions
|
|
353
|
+
)
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
function getImportForRouteNode(node, config, generatedRouteTreePath, root) {
|
|
357
|
+
let source = "";
|
|
358
|
+
if (config.importRoutesUsingAbsolutePaths) {
|
|
359
|
+
source = replaceBackslash(
|
|
360
|
+
removeExt(
|
|
361
|
+
path.resolve(root, config.routesDirectory, node.filePath),
|
|
362
|
+
config.addExtensions
|
|
363
|
+
)
|
|
364
|
+
);
|
|
365
|
+
} else {
|
|
366
|
+
source = `./${getImportPath(node, config, generatedRouteTreePath)}`;
|
|
367
|
+
}
|
|
368
|
+
return {
|
|
369
|
+
source,
|
|
370
|
+
specifiers: [
|
|
371
|
+
{
|
|
372
|
+
imported: "Route",
|
|
373
|
+
local: `${node.variableName}RouteImport`
|
|
374
|
+
}
|
|
375
|
+
]
|
|
376
|
+
};
|
|
377
|
+
}
|
|
362
378
|
export {
|
|
363
379
|
buildFileRoutesByPathInterface,
|
|
364
380
|
buildImportString,
|
|
@@ -375,14 +391,14 @@ export {
|
|
|
375
391
|
determineNodePath,
|
|
376
392
|
findParent,
|
|
377
393
|
format,
|
|
394
|
+
getImportForRouteNode,
|
|
395
|
+
getImportPath,
|
|
378
396
|
getResolvedRouteNodeVariableName,
|
|
379
|
-
hasChildWithExport,
|
|
380
397
|
hasParentRoute,
|
|
381
398
|
inferFullPath,
|
|
382
399
|
inferPath,
|
|
383
400
|
inferTo,
|
|
384
401
|
isRouteNodeValidForAugmentation,
|
|
385
|
-
lowerCaseFirstChar,
|
|
386
402
|
mergeImportDeclarations,
|
|
387
403
|
multiSortBy,
|
|
388
404
|
removeExt,
|