@tanstack/router-generator 1.121.0-alpha.3 → 1.121.0
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 +23 -5
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/config.d.cts +7 -3
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs +5 -3
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs +1 -1
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/generator.cjs +828 -665
- package/dist/cjs/generator.cjs.map +1 -1
- package/dist/cjs/generator.d.cts +78 -1
- package/dist/cjs/index.cjs +5 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -3
- package/dist/cjs/logger.cjs +37 -0
- package/dist/cjs/logger.cjs.map +1 -0
- package/dist/cjs/logger.d.cts +10 -0
- package/dist/cjs/plugin/default-generator-plugin.cjs +88 -0
- package/dist/cjs/plugin/default-generator-plugin.cjs.map +1 -0
- package/dist/cjs/plugin/default-generator-plugin.d.cts +2 -0
- package/dist/cjs/plugin/types.d.cts +46 -0
- package/dist/cjs/template.cjs +10 -10
- package/dist/cjs/template.cjs.map +1 -1
- package/dist/cjs/template.d.cts +2 -2
- package/dist/cjs/transform/default-transform-plugin.cjs +95 -0
- package/dist/cjs/transform/default-transform-plugin.cjs.map +1 -0
- package/dist/cjs/transform/default-transform-plugin.d.cts +2 -0
- package/dist/cjs/transform/transform.cjs +351 -0
- package/dist/cjs/transform/transform.cjs.map +1 -0
- package/dist/cjs/transform/transform.d.cts +4 -0
- package/dist/cjs/transform/types.d.cts +43 -0
- package/dist/cjs/transform/utils.cjs +36 -0
- package/dist/cjs/transform/utils.cjs.map +1 -0
- package/dist/cjs/transform/utils.d.cts +2 -0
- package/dist/cjs/types.d.cts +22 -0
- package/dist/cjs/utils.cjs +237 -40
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +76 -9
- package/dist/esm/config.d.ts +7 -3
- package/dist/esm/config.js +21 -3
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/filesystem/physical/getRouteNodes.js +3 -1
- package/dist/esm/filesystem/physical/getRouteNodes.js.map +1 -1
- package/dist/esm/filesystem/virtual/getRouteNodes.js +1 -1
- package/dist/esm/filesystem/virtual/getRouteNodes.js.map +1 -1
- package/dist/esm/generator.d.ts +78 -1
- package/dist/esm/generator.js +817 -653
- package/dist/esm/generator.js.map +1 -1
- package/dist/esm/index.d.ts +7 -3
- package/dist/esm/index.js +7 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/logger.d.ts +10 -0
- package/dist/esm/logger.js +37 -0
- package/dist/esm/logger.js.map +1 -0
- package/dist/esm/plugin/default-generator-plugin.d.ts +2 -0
- package/dist/esm/plugin/default-generator-plugin.js +88 -0
- package/dist/esm/plugin/default-generator-plugin.js.map +1 -0
- package/dist/esm/plugin/types.d.ts +46 -0
- package/dist/esm/template.d.ts +2 -2
- package/dist/esm/template.js +10 -10
- package/dist/esm/template.js.map +1 -1
- package/dist/esm/transform/default-transform-plugin.d.ts +2 -0
- package/dist/esm/transform/default-transform-plugin.js +95 -0
- package/dist/esm/transform/default-transform-plugin.js.map +1 -0
- package/dist/esm/transform/transform.d.ts +4 -0
- package/dist/esm/transform/transform.js +351 -0
- package/dist/esm/transform/transform.js.map +1 -0
- package/dist/esm/transform/types.d.ts +43 -0
- package/dist/esm/transform/utils.d.ts +2 -0
- package/dist/esm/transform/utils.js +36 -0
- package/dist/esm/transform/utils.js.map +1 -0
- package/dist/esm/types.d.ts +22 -0
- package/dist/esm/utils.d.ts +76 -9
- package/dist/esm/utils.js +237 -40
- package/dist/esm/utils.js.map +1 -1
- package/package.json +9 -10
- package/src/config.ts +23 -2
- package/src/filesystem/physical/getRouteNodes.ts +2 -1
- package/src/filesystem/virtual/getRouteNodes.ts +1 -1
- package/src/generator.ts +1108 -934
- package/src/index.ts +25 -3
- package/src/logger.ts +43 -0
- package/src/plugin/default-generator-plugin.ts +96 -0
- package/src/plugin/types.ts +51 -0
- package/src/template.ts +33 -12
- package/src/transform/default-transform-plugin.ts +103 -0
- package/src/transform/transform.ts +430 -0
- package/src/transform/types.ts +50 -0
- package/src/transform/utils.ts +42 -0
- package/src/types.ts +25 -0
- package/src/utils.ts +351 -36
package/dist/esm/template.js
CHANGED
|
@@ -6,9 +6,9 @@ function fillTemplate(config, template, values) {
|
|
|
6
6
|
);
|
|
7
7
|
return format(replaced, config);
|
|
8
8
|
}
|
|
9
|
-
function getTargetTemplate(
|
|
9
|
+
function getTargetTemplate(config) {
|
|
10
|
+
const target = config.target;
|
|
10
11
|
switch (target) {
|
|
11
|
-
// TODO: Remove this disabled eslint rule when more target types are added.
|
|
12
12
|
case "react":
|
|
13
13
|
return {
|
|
14
14
|
fullPkg: "@tanstack/react-router",
|
|
@@ -35,8 +35,8 @@ function getTargetTemplate(target) {
|
|
|
35
35
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
36
36
|
].join(""),
|
|
37
37
|
imports: {
|
|
38
|
-
tsrImports: () => "",
|
|
39
|
-
tsrExportStart: () => `export const Route = createFileRoute(`,
|
|
38
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createFileRoute } from '@tanstack/react-router';",
|
|
39
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createFileRoute(" : `export const Route = createFileRoute('${routePath}')(`,
|
|
40
40
|
tsrExportEnd: () => ");"
|
|
41
41
|
}
|
|
42
42
|
},
|
|
@@ -48,8 +48,8 @@ function getTargetTemplate(target) {
|
|
|
48
48
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
49
49
|
].join(""),
|
|
50
50
|
imports: {
|
|
51
|
-
tsrImports: () => "import { createLazyFileRoute } from '@tanstack/react-router';",
|
|
52
|
-
tsrExportStart: (routePath) => `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
51
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createLazyFileRoute } from '@tanstack/react-router';",
|
|
52
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createLazyFileRoute(" : `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
53
53
|
tsrExportEnd: () => ");"
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -80,8 +80,8 @@ function getTargetTemplate(target) {
|
|
|
80
80
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
81
81
|
].join(""),
|
|
82
82
|
imports: {
|
|
83
|
-
tsrImports: () => "",
|
|
84
|
-
tsrExportStart: () => `export const Route = createFileRoute(`,
|
|
83
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createFileRoute } from '@tanstack/solid-router';",
|
|
84
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createFileRoute(" : `export const Route = createFileRoute('${routePath}')(`,
|
|
85
85
|
tsrExportEnd: () => ");"
|
|
86
86
|
}
|
|
87
87
|
},
|
|
@@ -93,8 +93,8 @@ function getTargetTemplate(target) {
|
|
|
93
93
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
94
94
|
].join(""),
|
|
95
95
|
imports: {
|
|
96
|
-
tsrImports: () => "import { createLazyFileRoute } from '@tanstack/solid-router';",
|
|
97
|
-
tsrExportStart: (routePath) => `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
96
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createLazyFileRoute } from '@tanstack/solid-router';",
|
|
97
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createLazyFileRoute(" : `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
98
98
|
tsrExportEnd: () => ");"
|
|
99
99
|
}
|
|
100
100
|
}
|
package/dist/esm/template.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.js","sources":["../../src/template.ts"],"sourcesContent":["import { format } from './utils'\nimport type { Config } from './config'\n\ntype TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'\n\nexport function fillTemplate(\n config: Config,\n template: string,\n values: Record<TemplateTag, string>,\n) {\n const replaced = template.replace(\n /%%(\\w+)%%/g,\n (_, key) => values[key as TemplateTag] || '',\n )\n return format(replaced, config)\n}\n\
|
|
1
|
+
{"version":3,"file":"template.js","sources":["../../src/template.ts"],"sourcesContent":["import { format } from './utils'\nimport type { Config } from './config'\n\ntype TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'\n\nexport function fillTemplate(\n config: Config,\n template: string,\n values: Record<TemplateTag, string>,\n) {\n const replaced = template.replace(\n /%%(\\w+)%%/g,\n (_, key) => values[key as TemplateTag] || '',\n )\n return format(replaced, config)\n}\n\nexport type TargetTemplate = {\n fullPkg: string\n subPkg: string\n rootRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: () => string\n tsrExportEnd: () => string\n }\n }\n route: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n lazyRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n}\n\nexport function getTargetTemplate(config: Config): TargetTemplate {\n const target = config.target\n switch (target) {\n case 'react':\n return {\n fullPkg: '@tanstack/react-router',\n subPkg: 'react-router',\n rootRoute: {\n template: () =>\n [\n 'import * as React from \"react\"\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RootComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RootComponent() { return (<React.Fragment><div>Hello \"%%tsrPath%%\"!</div><Outlet /></React.Fragment>) };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { Outlet, createRootRoute } from '@tanstack/react-router';\",\n tsrExportStart: () => 'export const Route = createRootRoute(',\n tsrExportEnd: () => ');',\n },\n },\n route: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createFileRoute('\n : `export const Route = createFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n lazyRoute: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createLazyFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createLazyFileRoute('\n : `export const Route = createLazyFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n }\n case 'solid':\n return {\n fullPkg: '@tanstack/solid-router',\n subPkg: 'solid-router',\n rootRoute: {\n template: () =>\n [\n 'import * as Solid from \"solid-js\"\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RootComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RootComponent() { return (<><div>Hello \"%%tsrPath%%\"!</div><Outlet /></>) };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { Outlet, createRootRoute } from '@tanstack/solid-router';\",\n tsrExportStart: () => 'export const Route = createRootRoute(',\n tsrExportEnd: () => ');',\n },\n },\n route: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createFileRoute } from '@tanstack/solid-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createFileRoute('\n : `export const Route = createFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n lazyRoute: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createLazyFileRoute } from '@tanstack/solid-router';\",\n\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createLazyFileRoute('\n : `export const Route = createLazyFileRoute('${routePath}')(`,\n\n tsrExportEnd: () => ');',\n },\n },\n }\n default:\n throw new Error(`router-generator: Unknown target type: ${target}`)\n }\n}\n"],"names":[],"mappings":";AAKgB,SAAA,aACd,QACA,UACA,QACA;AACA,QAAM,WAAW,SAAS;AAAA,IACxB;AAAA,IACA,CAAC,GAAG,QAAQ,OAAO,GAAkB,KAAK;AAAA,EAC5C;AACO,SAAA,OAAO,UAAU,MAAM;AAChC;AA+BO,SAAS,kBAAkB,QAAgC;AAChE,QAAM,SAAS,OAAO;AACtB,UAAQ,QAAQ;AAAA,IACd,KAAK;AACI,aAAA;AAAA,QACL,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,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AAAA,YACxD,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV,OAAO,sBAAsB,QACzB,KACA;AAAA,YAEN,gBAAgB,CAAC,cACf,OAAO,sBAAsB,QACzB,8CACA,6CAA6C,SAAS;AAAA,YAE5D,cAAc,MAAM;AAAA,UAAA;AAAA,QACtB;AAAA,MAEJ;AAAA,IACF;AACE,YAAM,IAAI,MAAM,0CAA0C,MAAM,EAAE;AAAA,EAAA;AAExE;"}
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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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;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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): "'" | '"';
|
|
@@ -0,0 +1,351 @@
|
|
|
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;
|
|
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
|
+
const program = ast.program;
|
|
48
|
+
for (const n of program.body) {
|
|
49
|
+
if (registeredExports.size > 0 && n.type === "ExportNamedDeclaration" && ((_b = n.declaration) == null ? void 0 : _b.type) === "VariableDeclaration") {
|
|
50
|
+
const decl = n.declaration.declarations[0];
|
|
51
|
+
if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
|
|
52
|
+
const plugin = registeredExports.get(decl.id.name);
|
|
53
|
+
if (plugin) {
|
|
54
|
+
const pluginAppliedChanges = plugin.onExportFound({
|
|
55
|
+
decl,
|
|
56
|
+
ctx: { ...ctx, preferredQuote }
|
|
57
|
+
});
|
|
58
|
+
if (pluginAppliedChanges) {
|
|
59
|
+
appliedChanges = true;
|
|
60
|
+
}
|
|
61
|
+
registeredExports.delete(decl.id.name);
|
|
62
|
+
foundExports.push(decl.id.name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const imports = {
|
|
68
|
+
required: [],
|
|
69
|
+
banned: []
|
|
70
|
+
};
|
|
71
|
+
for (const plugin of plugins ?? []) {
|
|
72
|
+
const exportName = plugin.exportName;
|
|
73
|
+
if (foundExports.includes(exportName)) {
|
|
74
|
+
const pluginImports = plugin.imports(ctx);
|
|
75
|
+
if (pluginImports.required) {
|
|
76
|
+
imports.required.push(...pluginImports.required);
|
|
77
|
+
}
|
|
78
|
+
if (pluginImports.banned) {
|
|
79
|
+
imports.banned.push(...pluginImports.banned);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
imports.required = mergeImportDeclarations(imports.required);
|
|
84
|
+
imports.banned = mergeImportDeclarations(imports.banned);
|
|
85
|
+
const importStatementCandidates = [];
|
|
86
|
+
const importDeclarationsToRemove = [];
|
|
87
|
+
for (const n of program.body) {
|
|
88
|
+
const findImport = (opts) => (i) => {
|
|
89
|
+
if (i.source === opts.source) {
|
|
90
|
+
const importKind = i.importKind || "value";
|
|
91
|
+
const expectedImportKind = opts.importKind || "value";
|
|
92
|
+
return expectedImportKind === importKind;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
};
|
|
96
|
+
if (n.type === "ImportDeclaration" && typeof n.source.value === "string") {
|
|
97
|
+
const filterImport = findImport({
|
|
98
|
+
source: n.source.value,
|
|
99
|
+
importKind: n.importKind
|
|
100
|
+
});
|
|
101
|
+
let requiredImports = imports.required.filter(filterImport)[0];
|
|
102
|
+
const bannedImports = imports.banned.filter(filterImport)[0];
|
|
103
|
+
if (!requiredImports && !bannedImports) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const importSpecifiersToRemove = [];
|
|
107
|
+
if (n.specifiers) {
|
|
108
|
+
for (const spec of n.specifiers) {
|
|
109
|
+
if (!requiredImports && !bannedImports) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
if (spec.type === "ImportSpecifier" && typeof spec.imported.name === "string") {
|
|
113
|
+
if (requiredImports) {
|
|
114
|
+
const requiredImportIndex = requiredImports.specifiers.findIndex(
|
|
115
|
+
(imp) => imp.imported === spec.imported.name
|
|
116
|
+
);
|
|
117
|
+
if (requiredImportIndex !== -1) {
|
|
118
|
+
requiredImports.specifiers.splice(requiredImportIndex, 1);
|
|
119
|
+
if (requiredImports.specifiers.length === 0) {
|
|
120
|
+
imports.required = imports.required.splice(
|
|
121
|
+
imports.required.indexOf(requiredImports),
|
|
122
|
+
1
|
|
123
|
+
);
|
|
124
|
+
requiredImports = void 0;
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
importStatementCandidates.push(n);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (bannedImports) {
|
|
131
|
+
const bannedImportIndex = bannedImports.specifiers.findIndex(
|
|
132
|
+
(imp) => imp.imported === spec.imported.name
|
|
133
|
+
);
|
|
134
|
+
if (bannedImportIndex !== -1) {
|
|
135
|
+
importSpecifiersToRemove.push(spec);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (importSpecifiersToRemove.length > 0) {
|
|
141
|
+
appliedChanges = true;
|
|
142
|
+
n.specifiers = n.specifiers.filter(
|
|
143
|
+
(spec) => !importSpecifiersToRemove.includes(spec)
|
|
144
|
+
);
|
|
145
|
+
if (n.specifiers.length === 0) {
|
|
146
|
+
importDeclarationsToRemove.push(n);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
imports.required.forEach((requiredImport) => {
|
|
153
|
+
if (requiredImport.specifiers.length > 0) {
|
|
154
|
+
appliedChanges = true;
|
|
155
|
+
if (importStatementCandidates.length > 0) {
|
|
156
|
+
const importStatement2 = importStatementCandidates.find(
|
|
157
|
+
(importStatement3) => {
|
|
158
|
+
if (importStatement3.source.value === requiredImport.source) {
|
|
159
|
+
const importKind = importStatement3.importKind || "value";
|
|
160
|
+
const requiredImportKind = requiredImport.importKind || "value";
|
|
161
|
+
return importKind === requiredImportKind;
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
if (importStatement2) {
|
|
167
|
+
if (importStatement2.specifiers === void 0) {
|
|
168
|
+
importStatement2.specifiers = [];
|
|
169
|
+
}
|
|
170
|
+
const importSpecifiersToAdd = requiredImport.specifiers.map(
|
|
171
|
+
(spec) => b.importSpecifier(
|
|
172
|
+
b.identifier(spec.imported),
|
|
173
|
+
b.identifier(spec.imported)
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
importStatement2.specifiers = [
|
|
177
|
+
...importStatement2.specifiers,
|
|
178
|
+
...importSpecifiersToAdd
|
|
179
|
+
];
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const importStatement = b.importDeclaration(
|
|
184
|
+
requiredImport.specifiers.map(
|
|
185
|
+
(spec) => b.importSpecifier(
|
|
186
|
+
b.identifier(spec.imported),
|
|
187
|
+
spec.local ? b.identifier(spec.local) : null
|
|
188
|
+
)
|
|
189
|
+
),
|
|
190
|
+
b.stringLiteral(requiredImport.source)
|
|
191
|
+
);
|
|
192
|
+
program.body.unshift(importStatement);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
if (importDeclarationsToRemove.length > 0) {
|
|
196
|
+
appliedChanges = true;
|
|
197
|
+
for (const importDeclaration of importDeclarationsToRemove) {
|
|
198
|
+
if (((_c = importDeclaration.specifiers) == null ? void 0 : _c.length) === 0) {
|
|
199
|
+
const index = program.body.indexOf(importDeclaration);
|
|
200
|
+
if (index !== -1) {
|
|
201
|
+
program.body.splice(index, 1);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (!appliedChanges) {
|
|
207
|
+
return {
|
|
208
|
+
exports: foundExports,
|
|
209
|
+
result: "not-modified"
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
const printResult = print(ast, {
|
|
213
|
+
reuseWhitespace: true,
|
|
214
|
+
sourceMapName: "output.map"
|
|
215
|
+
});
|
|
216
|
+
let transformedCode = printResult.code;
|
|
217
|
+
if (printResult.map) {
|
|
218
|
+
const fixedOutput = await fixTransformedOutputText({
|
|
219
|
+
originalCode: source,
|
|
220
|
+
transformedCode,
|
|
221
|
+
sourceMap: printResult.map,
|
|
222
|
+
preferredQuote
|
|
223
|
+
});
|
|
224
|
+
transformedCode = fixedOutput;
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
result: "modified",
|
|
228
|
+
exports: foundExports,
|
|
229
|
+
output: transformedCode
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
async function fixTransformedOutputText({
|
|
233
|
+
originalCode,
|
|
234
|
+
transformedCode,
|
|
235
|
+
sourceMap,
|
|
236
|
+
preferredQuote
|
|
237
|
+
}) {
|
|
238
|
+
const originalLines = originalCode.split("\n");
|
|
239
|
+
const transformedLines = transformedCode.split("\n");
|
|
240
|
+
const defaultUsesSemicolons = detectSemicolonUsage(originalCode);
|
|
241
|
+
const consumer = await new SourceMapConsumer(sourceMap);
|
|
242
|
+
const fixedLines = transformedLines.map((line, i) => {
|
|
243
|
+
const transformedLineNum = i + 1;
|
|
244
|
+
let origLineText = void 0;
|
|
245
|
+
for (let col = 0; col < line.length; col++) {
|
|
246
|
+
const mapped = consumer.originalPositionFor({
|
|
247
|
+
line: transformedLineNum,
|
|
248
|
+
column: col
|
|
249
|
+
});
|
|
250
|
+
if (mapped.line != null && mapped.line > 0) {
|
|
251
|
+
origLineText = originalLines[mapped.line - 1];
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (origLineText !== void 0) {
|
|
256
|
+
if (origLineText === line) {
|
|
257
|
+
return origLineText;
|
|
258
|
+
}
|
|
259
|
+
return fixLine(line, {
|
|
260
|
+
originalLine: origLineText,
|
|
261
|
+
useOriginalSemicolon: true,
|
|
262
|
+
useOriginalQuotes: true,
|
|
263
|
+
fallbackQuote: preferredQuote
|
|
264
|
+
});
|
|
265
|
+
} else {
|
|
266
|
+
return fixLine(line, {
|
|
267
|
+
originalLine: null,
|
|
268
|
+
useOriginalSemicolon: false,
|
|
269
|
+
useOriginalQuotes: false,
|
|
270
|
+
fallbackQuote: preferredQuote,
|
|
271
|
+
fallbackSemicolon: defaultUsesSemicolons
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return fixedLines.join("\n");
|
|
276
|
+
}
|
|
277
|
+
function fixLine(line, {
|
|
278
|
+
originalLine,
|
|
279
|
+
useOriginalSemicolon,
|
|
280
|
+
useOriginalQuotes,
|
|
281
|
+
fallbackQuote,
|
|
282
|
+
fallbackSemicolon = true
|
|
283
|
+
}) {
|
|
284
|
+
let result = line;
|
|
285
|
+
if (useOriginalQuotes && originalLine) {
|
|
286
|
+
result = fixQuotes(result, originalLine, fallbackQuote);
|
|
287
|
+
} else if (!useOriginalQuotes && fallbackQuote) {
|
|
288
|
+
result = fixQuotesToPreferred(result, fallbackQuote);
|
|
289
|
+
}
|
|
290
|
+
if (useOriginalSemicolon && originalLine) {
|
|
291
|
+
const hadSemicolon = originalLine.trimEnd().endsWith(";");
|
|
292
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
293
|
+
if (hadSemicolon && !hasSemicolon) result += ";";
|
|
294
|
+
if (!hadSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
295
|
+
} else if (!useOriginalSemicolon) {
|
|
296
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
297
|
+
if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
298
|
+
if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ";";
|
|
299
|
+
}
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
function fixQuotes(line, originalLine, fallbackQuote) {
|
|
303
|
+
let originalQuote = detectQuoteFromLine(originalLine);
|
|
304
|
+
if (!originalQuote) {
|
|
305
|
+
originalQuote = fallbackQuote;
|
|
306
|
+
}
|
|
307
|
+
return fixQuotesToPreferred(line, originalQuote);
|
|
308
|
+
}
|
|
309
|
+
function fixQuotesToPreferred(line, quote) {
|
|
310
|
+
return line.replace(
|
|
311
|
+
/(['"`])([^'"`\\]*(?:\\.[^'"`\\]*)*)\1/g,
|
|
312
|
+
(_, q, content) => {
|
|
313
|
+
const escaped = content.replaceAll(quote, `\\${quote}`);
|
|
314
|
+
return `${quote}${escaped}${quote}`;
|
|
315
|
+
}
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
function detectQuoteFromLine(line) {
|
|
319
|
+
const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
|
|
320
|
+
return match ? match[1] : null;
|
|
321
|
+
}
|
|
322
|
+
function detectSemicolonUsage(code) {
|
|
323
|
+
const lines = code.split("\n").map((l) => l.trim());
|
|
324
|
+
const total = lines.length;
|
|
325
|
+
const withSemis = lines.filter((l) => l.endsWith(";")).length;
|
|
326
|
+
return withSemis > total / 2;
|
|
327
|
+
}
|
|
328
|
+
function detectPreferredQuoteStyle(ast) {
|
|
329
|
+
let single = 0;
|
|
330
|
+
let double = 0;
|
|
331
|
+
visit(ast, {
|
|
332
|
+
visitStringLiteral(path) {
|
|
333
|
+
var _a;
|
|
334
|
+
if (path.parent.node.type !== "JSXAttribute") {
|
|
335
|
+
const raw = (_a = path.node.extra) == null ? void 0 : _a.raw;
|
|
336
|
+
if (raw == null ? void 0 : raw.startsWith("'")) single++;
|
|
337
|
+
else if (raw == null ? void 0 : raw.startsWith('"')) double++;
|
|
338
|
+
}
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
if (single >= double) {
|
|
343
|
+
return "'";
|
|
344
|
+
}
|
|
345
|
+
return '"';
|
|
346
|
+
}
|
|
347
|
+
export {
|
|
348
|
+
detectPreferredQuoteStyle,
|
|
349
|
+
transform
|
|
350
|
+
};
|
|
351
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +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 const program: types.namedTypes.Program = ast.program\n // first pass: find registered exports\n for (const n of program.body) {\n if (\n registeredExports.size > 0 &&\n n.type === 'ExportNamedDeclaration' &&\n n.declaration?.type === 'VariableDeclaration'\n ) {\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 const pluginAppliedChanges = plugin.onExportFound({\n decl,\n ctx: { ...ctx, preferredQuote },\n })\n\n if (pluginAppliedChanges) {\n appliedChanges = true\n }\n\n // export is handled, remove it from the registered exports\n registeredExports.delete(decl.id.name)\n // store the export so we can later return it once the file is transformed\n foundExports.push(decl.id.name)\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;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,QAAA;AAAA,MACH;AAAA,IACF,CACD;AAAA,WACM,GAAG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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;AACtC,cAAA,OAAM,UAAK,KAAK,UAAV,mBAAiB;AACzB,YAAA,2BAAK,WAAW,KAAM;AAAA,iBACjB,2BAAK,WAAW,KAAM;AAAA,MAAA;AAE1B,aAAA;AAAA,IAAA;AAAA,EACT,CACD;AAED,MAAI,UAAU,QAAQ;AACb,WAAA;AAAA,EAAA;AAEF,SAAA;AACT;"}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
}
|