@tanstack/router-plugin 1.39.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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/dist/cjs/ast.cjs +51 -0
  4. package/dist/cjs/ast.cjs.map +1 -0
  5. package/dist/cjs/ast.d.cts +22 -0
  6. package/dist/cjs/code-splitter.cjs +157 -0
  7. package/dist/cjs/code-splitter.cjs.map +1 -0
  8. package/dist/cjs/code-splitter.d.cts +22 -0
  9. package/dist/cjs/compilers.cjs +327 -0
  10. package/dist/cjs/compilers.cjs.map +1 -0
  11. package/dist/cjs/compilers.d.cts +18 -0
  12. package/dist/cjs/config.cjs +16 -0
  13. package/dist/cjs/config.cjs.map +1 -0
  14. package/dist/cjs/config.d.cts +79 -0
  15. package/dist/cjs/constants.cjs +7 -0
  16. package/dist/cjs/constants.cjs.map +1 -0
  17. package/dist/cjs/constants.d.cts +2 -0
  18. package/dist/cjs/eliminateUnreferencedIdentifiers.cjs +149 -0
  19. package/dist/cjs/eliminateUnreferencedIdentifiers.cjs.map +1 -0
  20. package/dist/cjs/eliminateUnreferencedIdentifiers.d.cts +9 -0
  21. package/dist/cjs/index.cjs +7 -0
  22. package/dist/cjs/index.cjs.map +1 -0
  23. package/dist/cjs/index.d.cts +2 -0
  24. package/dist/cjs/router-generator.cjs +67 -0
  25. package/dist/cjs/router-generator.cjs.map +1 -0
  26. package/dist/cjs/router-generator.d.cts +18 -0
  27. package/dist/cjs/vite.cjs +16 -0
  28. package/dist/cjs/vite.cjs.map +1 -0
  29. package/dist/cjs/vite.d.cts +41 -0
  30. package/dist/esm/ast.d.ts +22 -0
  31. package/dist/esm/ast.js +34 -0
  32. package/dist/esm/ast.js.map +1 -0
  33. package/dist/esm/code-splitter.d.ts +22 -0
  34. package/dist/esm/code-splitter.js +157 -0
  35. package/dist/esm/code-splitter.js.map +1 -0
  36. package/dist/esm/compilers.d.ts +18 -0
  37. package/dist/esm/compilers.js +309 -0
  38. package/dist/esm/compilers.js.map +1 -0
  39. package/dist/esm/config.d.ts +79 -0
  40. package/dist/esm/config.js +16 -0
  41. package/dist/esm/config.js.map +1 -0
  42. package/dist/esm/constants.d.ts +2 -0
  43. package/dist/esm/constants.js +7 -0
  44. package/dist/esm/constants.js.map +1 -0
  45. package/dist/esm/eliminateUnreferencedIdentifiers.d.ts +9 -0
  46. package/dist/esm/eliminateUnreferencedIdentifiers.js +132 -0
  47. package/dist/esm/eliminateUnreferencedIdentifiers.js.map +1 -0
  48. package/dist/esm/index.d.ts +2 -0
  49. package/dist/esm/index.js +7 -0
  50. package/dist/esm/index.js.map +1 -0
  51. package/dist/esm/router-generator.d.ts +18 -0
  52. package/dist/esm/router-generator.js +67 -0
  53. package/dist/esm/router-generator.js.map +1 -0
  54. package/dist/esm/vite.d.ts +41 -0
  55. package/dist/esm/vite.js +16 -0
  56. package/dist/esm/vite.js.map +1 -0
  57. package/package.json +88 -0
  58. package/src/ast.ts +49 -0
  59. package/src/code-splitter.ts +162 -0
  60. package/src/compilers.ts +414 -0
  61. package/src/config.ts +25 -0
  62. package/src/constants.ts +2 -0
  63. package/src/eliminateUnreferencedIdentifiers.ts +211 -0
  64. package/src/index.ts +2 -0
  65. package/src/router-generator.ts +92 -0
  66. package/src/vite.ts +19 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Tanner Linsley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ <img src="https://static.scarf.sh/a.png?x-pxid=d988eb79-b0fc-4a2b-8514-6a1ab932d188" />
2
+
3
+ # TanStack Router Plugin
4
+
5
+ See https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const babel = require("@babel/core");
4
+ function _interopNamespaceDefault(e) {
5
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
6
+ if (e) {
7
+ for (const k in e) {
8
+ if (k !== "default") {
9
+ const d = Object.getOwnPropertyDescriptor(e, k);
10
+ Object.defineProperty(n, k, d.get ? d : {
11
+ enumerable: true,
12
+ get: () => e[k]
13
+ });
14
+ }
15
+ }
16
+ }
17
+ n.default = e;
18
+ return Object.freeze(n);
19
+ }
20
+ const babel__namespace = /* @__PURE__ */ _interopNamespaceDefault(babel);
21
+ function compileAst(makeOpts) {
22
+ return async (opts) => {
23
+ const res = babel__namespace.transformSync(opts.code, {
24
+ plugins: [
25
+ ["@babel/plugin-syntax-jsx", {}],
26
+ [
27
+ "@babel/plugin-syntax-typescript",
28
+ {
29
+ isTSX: true
30
+ }
31
+ ],
32
+ ...opts.getBabelConfig().plugins
33
+ ],
34
+ root: makeOpts.root,
35
+ filename: opts.filename,
36
+ sourceMaps: true
37
+ });
38
+ if (res == null ? void 0 : res.code) {
39
+ return {
40
+ code: res.code,
41
+ map: res.map
42
+ };
43
+ }
44
+ return {
45
+ code: opts.code,
46
+ map: null
47
+ };
48
+ };
49
+ }
50
+ exports.compileAst = compileAst;
51
+ //# sourceMappingURL=ast.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast.cjs","sources":["../../src/ast.ts"],"sourcesContent":["import * as babel from '@babel/core'\n\nexport type CompileAstFn = (compileOpts: {\n code: string\n filename: string\n getBabelConfig: () => { plugins: Array<any> }\n}) => Promise<{\n code: string\n map: any\n}>\n\nexport function compileAst(makeOpts: { root: string }) {\n return async (opts: {\n code: string\n filename: string\n getBabelConfig: () => { plugins: Array<any> }\n }): Promise<{\n code: string\n map: any\n }> => {\n const res = babel.transformSync(opts.code, {\n plugins: [\n ['@babel/plugin-syntax-jsx', {}],\n [\n '@babel/plugin-syntax-typescript',\n {\n isTSX: true,\n },\n ],\n ...opts.getBabelConfig().plugins,\n ],\n root: makeOpts.root,\n filename: opts.filename,\n sourceMaps: true,\n })\n\n if (res?.code) {\n return {\n code: res.code,\n map: res.map,\n }\n }\n\n return {\n code: opts.code,\n map: null,\n }\n }\n}\n"],"names":["babel"],"mappings":";;;;;;;;;;;;;;;;;;;;AAWO,SAAS,WAAW,UAA4B;AACrD,SAAO,OAAO,SAOR;AACJ,UAAM,MAAMA,iBAAM,cAAc,KAAK,MAAM;AAAA,MACzC,SAAS;AAAA,QACP,CAAC,4BAA4B,CAAA,CAAE;AAAA,QAC/B;AAAA,UACE;AAAA,UACA;AAAA,YACE,OAAO;AAAA,UACT;AAAA,QACF;AAAA,QACA,GAAG,KAAK,eAAA,EAAiB;AAAA,MAC3B;AAAA,MACA,MAAM,SAAS;AAAA,MACf,UAAU,KAAK;AAAA,MACf,YAAY;AAAA,IAAA,CACb;AAED,QAAI,2BAAK,MAAM;AACN,aAAA;AAAA,QACL,MAAM,IAAI;AAAA,QACV,KAAK,IAAI;AAAA,MAAA;AAAA,IAEb;AAEO,WAAA;AAAA,MACL,MAAM,KAAK;AAAA,MACX,KAAK;AAAA,IAAA;AAAA,EACP;AAEJ;;"}
@@ -0,0 +1,22 @@
1
+ export type CompileAstFn = (compileOpts: {
2
+ code: string;
3
+ filename: string;
4
+ getBabelConfig: () => {
5
+ plugins: Array<any>;
6
+ };
7
+ }) => Promise<{
8
+ code: string;
9
+ map: any;
10
+ }>;
11
+ export declare function compileAst(makeOpts: {
12
+ root: string;
13
+ }): (opts: {
14
+ code: string;
15
+ filename: string;
16
+ getBabelConfig: () => {
17
+ plugins: Array<any>;
18
+ };
19
+ }) => Promise<{
20
+ code: string;
21
+ map: any;
22
+ }>;
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const node_path = require("node:path");
4
+ const node_url = require("node:url");
5
+ const unplugin = require("unplugin");
6
+ const ast = require("./ast.cjs");
7
+ const compilers = require("./compilers.cjs");
8
+ const config = require("./config.cjs");
9
+ const constants = require("./constants.cjs");
10
+ function capitalizeFirst(str) {
11
+ return str.charAt(0).toUpperCase() + str.slice(1);
12
+ }
13
+ function fileIsInRoutesDirectory(filePath, routesDirectory) {
14
+ const routesDirectoryPath = node_path.isAbsolute(routesDirectory) ? routesDirectory : node_path.join(process.cwd(), routesDirectory);
15
+ return filePath.startsWith(routesDirectoryPath);
16
+ }
17
+ const bannedBeforeExternalPlugins = [
18
+ {
19
+ identifier: "@react-refresh",
20
+ pkg: "@vitejs/plugin-react",
21
+ usage: "viteReact()"
22
+ }
23
+ ];
24
+ class FoundPluginInBeforeCode extends Error {
25
+ constructor(externalPlugin, framework) {
26
+ super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again:
27
+ e.g.
28
+ plugins: [
29
+ TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}
30
+ ${externalPlugin.usage},
31
+ ]
32
+ `);
33
+ }
34
+ }
35
+ const unpluginFactory = (options = {}, { framework }) => {
36
+ const debug = Boolean(process.env.TSR_VITE_DEBUG);
37
+ let ROOT = process.cwd();
38
+ let userConfig = options;
39
+ const handleSplittingFile = async (code, id) => {
40
+ const compiledAst = ast.compileAst({
41
+ root: ROOT
42
+ });
43
+ if (debug)
44
+ console.info("Splitting route: ", id);
45
+ const compiled = await compilers.splitFile({
46
+ code,
47
+ compileAst: compiledAst,
48
+ filename: id
49
+ });
50
+ if (debug)
51
+ console.info("");
52
+ if (debug)
53
+ console.info("Split Output");
54
+ if (debug)
55
+ console.info("");
56
+ if (debug)
57
+ console.info(compiled.code);
58
+ if (debug)
59
+ console.info("");
60
+ if (debug)
61
+ console.info("");
62
+ if (debug)
63
+ console.info("");
64
+ if (debug)
65
+ console.info("");
66
+ if (debug)
67
+ console.info("");
68
+ if (debug)
69
+ console.info("");
70
+ if (debug)
71
+ console.info("");
72
+ if (debug)
73
+ console.info("");
74
+ return compiled;
75
+ };
76
+ const handleCompilingFile = async (code, id) => {
77
+ const compiledAst = ast.compileAst({
78
+ root: ROOT
79
+ });
80
+ if (debug)
81
+ console.info("Handling createRoute: ", id);
82
+ const compiled = await compilers.compileFile({
83
+ code,
84
+ compileAst: compiledAst,
85
+ filename: id
86
+ });
87
+ if (debug)
88
+ console.info("");
89
+ if (debug)
90
+ console.info("Compiled Output");
91
+ if (debug)
92
+ console.info("");
93
+ if (debug)
94
+ console.info(compiled.code);
95
+ if (debug)
96
+ console.info("");
97
+ if (debug)
98
+ console.info("");
99
+ if (debug)
100
+ console.info("");
101
+ if (debug)
102
+ console.info("");
103
+ if (debug)
104
+ console.info("");
105
+ if (debug)
106
+ console.info("");
107
+ if (debug)
108
+ console.info("");
109
+ if (debug)
110
+ console.info("");
111
+ if (debug)
112
+ console.info("");
113
+ if (debug)
114
+ console.info("");
115
+ return compiled;
116
+ };
117
+ return {
118
+ name: "router-code-splitter-plugin",
119
+ enforce: "pre",
120
+ resolveId(source) {
121
+ var _a;
122
+ if (!((_a = userConfig.experimental) == null ? void 0 : _a.enableCodeSplitting)) {
123
+ return null;
124
+ }
125
+ if (source.startsWith(constants.splitPrefix + ":")) {
126
+ return source.replace(constants.splitPrefix + ":", "");
127
+ }
128
+ return null;
129
+ },
130
+ async transform(code, id) {
131
+ const url = node_url.pathToFileURL(id);
132
+ url.searchParams.delete("v");
133
+ id = node_url.fileURLToPath(url).replace(/\\/g, "/");
134
+ if (id.includes(constants.splitPrefix)) {
135
+ return await handleSplittingFile(code, id);
136
+ } else if (fileIsInRoutesDirectory(id, userConfig.routesDirectory) && (code.includes("createRoute(") || code.includes("createFileRoute("))) {
137
+ for (const externalPlugin of bannedBeforeExternalPlugins) {
138
+ if (code.includes(externalPlugin.identifier)) {
139
+ throw new FoundPluginInBeforeCode(externalPlugin, framework);
140
+ }
141
+ }
142
+ return await handleCompilingFile(code, id);
143
+ }
144
+ return null;
145
+ },
146
+ vite: {
147
+ async configResolved(config$1) {
148
+ ROOT = config$1.root;
149
+ userConfig = await config.getConfig(options, ROOT);
150
+ }
151
+ }
152
+ };
153
+ };
154
+ const unpluginRouterCodeSplitter = /* @__PURE__ */ unplugin.createUnplugin(unpluginFactory);
155
+ exports.unpluginFactory = unpluginFactory;
156
+ exports.unpluginRouterCodeSplitter = unpluginRouterCodeSplitter;
157
+ //# sourceMappingURL=code-splitter.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code-splitter.cjs","sources":["../../src/code-splitter.ts"],"sourcesContent":["import { isAbsolute, join } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { createUnplugin } from 'unplugin'\n\nimport { compileAst } from './ast'\nimport { compileFile, splitFile } from './compilers'\nimport { getConfig } from './config'\nimport { splitPrefix } from './constants'\n\nimport type { PluginOptions } from './config'\nimport type { UnpluginFactory } from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nfunction fileIsInRoutesDirectory(filePath: string, routesDirectory: string) {\n const routesDirectoryPath = isAbsolute(routesDirectory)\n ? routesDirectory\n : join(process.cwd(), routesDirectory)\n\n return filePath.startsWith(routesDirectoryPath)\n}\n\nconst bannedBeforeExternalPlugins = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(\n externalPlugin: (typeof bannedBeforeExternalPlugins)[number],\n framework: string,\n ) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nexport const unpluginFactory: UnpluginFactory<Partial<PluginOptions>> = (\n options = {},\n { framework },\n) => {\n const debug = Boolean(process.env.TSR_VITE_DEBUG)\n\n let ROOT: string = process.cwd()\n let userConfig = options as PluginOptions\n\n const handleSplittingFile = async (code: string, id: string) => {\n const compiledAst = compileAst({\n root: ROOT,\n })\n\n if (debug) console.info('Splitting route: ', id)\n\n const compiled = await splitFile({\n code,\n compileAst: compiledAst,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Split Output')\n if (debug) console.info('')\n if (debug) console.info(compiled.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiled\n }\n\n const handleCompilingFile = async (code: string, id: string) => {\n const compiledAst = compileAst({\n root: ROOT,\n })\n\n if (debug) console.info('Handling createRoute: ', id)\n\n const compiled = await compileFile({\n code,\n compileAst: compiledAst,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Compiled Output')\n if (debug) console.info('')\n if (debug) console.info(compiled.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiled\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n resolveId(source) {\n if (!userConfig.experimental?.enableCodeSplitting) {\n return null\n }\n\n if (source.startsWith(splitPrefix + ':')) {\n return source.replace(splitPrefix + ':', '')\n }\n return null\n },\n async transform(code, id) {\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(splitPrefix)) {\n return await handleSplittingFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return await handleCompilingFile(code, id)\n }\n\n return null\n },\n vite: {\n async configResolved(config) {\n ROOT = config.root\n userConfig = await getConfig(options, ROOT)\n },\n },\n }\n}\n\nexport const unpluginRouterCodeSplitter =\n /* #__PURE__ */ createUnplugin(unpluginFactory)\n"],"names":["isAbsolute","join","compileAst","splitFile","compileFile","splitPrefix","pathToFileURL","fileURLToPath","config","getConfig"],"mappings":";;;;;;;;;AAYA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AAEA,SAAS,wBAAwB,UAAkB,iBAAyB;AACpE,QAAA,sBAAsBA,qBAAW,eAAe,IAClD,kBACAC,UAAK,KAAA,QAAQ,OAAO,eAAe;AAEhC,SAAA,SAAS,WAAW,mBAAmB;AAChD;AAEA,MAAM,8BAA8B;AAAA,EAClC;AAAA,IACE,YAAY;AAAA,IACZ,KAAK;AAAA,IACL,OAAO;AAAA,EACT;AACF;AAEA,MAAM,gCAAgC,MAA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Ac,KAAK,KAAK,SAAS,kBAAkB,IAClE;AACA,mBAAW,kBAAkB,6BAA6B;AACxD,cAAI,KAAK,SAAS,eAAe,UAAU,GAAG;AACtC,kBAAA,IAAI,wBAAwB,gBAAgB,SAAS;AAAA,UAC7D;AAAA,QACF;AAEO,eAAA,MAAM,oBAAoB,MAAM,EAAE;AAAA,MAC3C;AAEO,aAAA;AAAA,IACT;AAAA,IACA,MAAM;AAAA,MACJ,MAAM,eAAeG,UAAQ;AAC3B,eAAOA,SAAO;AACD,qBAAA,MAAMC,OAAAA,UAAU,SAAS,IAAI;AAAA,MAC5C;AAAA,IACF;AAAA,EAAA;AAEJ;AAEa,MAAA,qEACoB,eAAe;;;"}
@@ -0,0 +1,22 @@
1
+ import { PluginOptions } from './config.cjs';
2
+ import { UnpluginFactory } from 'unplugin';
3
+
4
+ export declare const unpluginFactory: UnpluginFactory<Partial<PluginOptions>>;
5
+ export declare const unpluginRouterCodeSplitter: import('unplugin').UnpluginInstance<Partial<{
6
+ routeFileIgnorePrefix: string;
7
+ routesDirectory: string;
8
+ generatedRouteTree: string;
9
+ quoteStyle: "single" | "double";
10
+ semicolons: boolean;
11
+ disableTypes: boolean;
12
+ addExtensions: boolean;
13
+ disableLogging: boolean;
14
+ routeTreeFileHeader: string[];
15
+ routeTreeFileFooter: string[];
16
+ enableRouteGeneration?: boolean | undefined;
17
+ experimental?: {
18
+ enableCodeSplitting?: boolean | undefined;
19
+ } | undefined;
20
+ routeFilePrefix?: string | undefined;
21
+ routeFileIgnorePattern?: string | undefined;
22
+ }>, boolean>;
@@ -0,0 +1,327 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const t = require("@babel/types");
4
+ const template = require("@babel/template");
5
+ const constants = require("./constants.cjs");
6
+ const eliminateUnreferencedIdentifiers = require("./eliminateUnreferencedIdentifiers.cjs");
7
+ function _interopNamespaceDefault(e) {
8
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
9
+ if (e) {
10
+ for (const k in e) {
11
+ if (k !== "default") {
12
+ const d = Object.getOwnPropertyDescriptor(e, k);
13
+ Object.defineProperty(n, k, d.get ? d : {
14
+ enumerable: true,
15
+ get: () => e[k]
16
+ });
17
+ }
18
+ }
19
+ }
20
+ n.default = e;
21
+ return Object.freeze(n);
22
+ }
23
+ const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
24
+ const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
25
+ async function compileFile(opts) {
26
+ return await opts.compileAst({
27
+ code: opts.code,
28
+ filename: opts.filename,
29
+ getBabelConfig: () => ({
30
+ plugins: [
31
+ [
32
+ {
33
+ visitor: {
34
+ Program: {
35
+ enter(programPath, state) {
36
+ const splitUrl = `${constants.splitPrefix}:${opts.filename}?${constants.splitPrefix}`;
37
+ programPath.traverse(
38
+ {
39
+ CallExpression: (path) => {
40
+ if (path.node.callee.type === "Identifier") {
41
+ if (path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute") {
42
+ if (path.parentPath.node.type === "CallExpression") {
43
+ const options = resolveIdentifier(
44
+ path,
45
+ path.parentPath.node.arguments[0]
46
+ );
47
+ let found = false;
48
+ const hasImportedOrDefinedIdentifier = (name) => {
49
+ return programPath.scope.hasBinding(name);
50
+ };
51
+ if (t__namespace.isObjectExpression(options)) {
52
+ options.properties.forEach((prop) => {
53
+ if (t__namespace.isObjectProperty(prop)) {
54
+ if (t__namespace.isIdentifier(prop.key)) {
55
+ if (prop.key.name === "component") {
56
+ const value = prop.value;
57
+ if (t__namespace.isIdentifier(value)) {
58
+ removeIdentifierLiteral(path, value);
59
+ }
60
+ if (!hasImportedOrDefinedIdentifier(
61
+ "lazyRouteComponent"
62
+ )) {
63
+ programPath.unshiftContainer("body", [
64
+ template__namespace.smart(
65
+ `import { lazyRouteComponent } from '@tanstack/react-router'`
66
+ )()
67
+ ]);
68
+ }
69
+ if (!hasImportedOrDefinedIdentifier(
70
+ "$$splitComponentImporter"
71
+ )) {
72
+ programPath.unshiftContainer("body", [
73
+ template__namespace.smart(
74
+ `const $$splitComponentImporter = () => import('${splitUrl}')`
75
+ )()
76
+ ]);
77
+ }
78
+ prop.value = template__namespace.expression(
79
+ `lazyRouteComponent($$splitComponentImporter, 'component')`
80
+ )();
81
+ programPath.pushContainer("body", [
82
+ template__namespace.smart(
83
+ `function DummyComponent() { return null }`
84
+ )()
85
+ ]);
86
+ found = true;
87
+ } else if (prop.key.name === "loader") {
88
+ const value = prop.value;
89
+ if (t__namespace.isIdentifier(value)) {
90
+ removeIdentifierLiteral(path, value);
91
+ }
92
+ if (!hasImportedOrDefinedIdentifier(
93
+ "lazyFn"
94
+ )) {
95
+ programPath.unshiftContainer("body", [
96
+ template__namespace.smart(
97
+ `import { lazyFn } from '@tanstack/react-router'`
98
+ )()
99
+ ]);
100
+ }
101
+ if (!hasImportedOrDefinedIdentifier(
102
+ "$$splitLoaderImporter"
103
+ )) {
104
+ programPath.unshiftContainer("body", [
105
+ template__namespace.smart(
106
+ `const $$splitLoaderImporter = () => import('${splitUrl}')`
107
+ )()
108
+ ]);
109
+ }
110
+ prop.value = template__namespace.expression(
111
+ `lazyFn($$splitLoaderImporter, 'loader')`
112
+ )();
113
+ found = true;
114
+ }
115
+ }
116
+ }
117
+ programPath.scope.crawl();
118
+ });
119
+ }
120
+ if (found) {
121
+ programPath.pushContainer("body", [
122
+ template__namespace.smart(
123
+ `function TSR_Dummy_Component() {}`
124
+ )()
125
+ ]);
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
131
+ },
132
+ state
133
+ );
134
+ eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
135
+ }
136
+ }
137
+ }
138
+ },
139
+ {
140
+ root: process.cwd(),
141
+ minify: process.env.NODE_ENV === "production"
142
+ }
143
+ ]
144
+ ].filter(Boolean)
145
+ })
146
+ });
147
+ }
148
+ function resolveIdentifier(path, node) {
149
+ if (t__namespace.isIdentifier(node)) {
150
+ const binding = path.scope.getBinding(node.name);
151
+ if (binding) {
152
+ const declarator = binding.path.node;
153
+ if (t__namespace.isObjectExpression(declarator.init)) {
154
+ return declarator.init;
155
+ } else if (t__namespace.isFunctionDeclaration(declarator.init)) {
156
+ return declarator.init;
157
+ }
158
+ }
159
+ return void 0;
160
+ }
161
+ return node;
162
+ }
163
+ function removeIdentifierLiteral(path, node) {
164
+ if (t__namespace.isIdentifier(node)) {
165
+ const binding = path.scope.getBinding(node.name);
166
+ if (binding) {
167
+ binding.path.remove();
168
+ }
169
+ }
170
+ }
171
+ const splitNodeTypes = ["component", "loader"];
172
+ async function splitFile(opts) {
173
+ return await opts.compileAst({
174
+ code: opts.code,
175
+ filename: opts.filename,
176
+ getBabelConfig: () => ({
177
+ plugins: [
178
+ [
179
+ {
180
+ visitor: {
181
+ Program: {
182
+ enter(programPath, state) {
183
+ const splitNodesByType = {
184
+ component: void 0,
185
+ loader: void 0
186
+ };
187
+ programPath.traverse(
188
+ {
189
+ CallExpression: (path) => {
190
+ if (path.node.callee.type === "Identifier") {
191
+ if (path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute") {
192
+ if (path.parentPath.node.type === "CallExpression") {
193
+ const options = resolveIdentifier(
194
+ path,
195
+ path.parentPath.node.arguments[0]
196
+ );
197
+ if (t__namespace.isObjectExpression(options)) {
198
+ options.properties.forEach((prop) => {
199
+ if (t__namespace.isObjectProperty(prop)) {
200
+ splitNodeTypes.forEach((type) => {
201
+ if (t__namespace.isIdentifier(prop.key)) {
202
+ if (prop.key.name === type) {
203
+ splitNodesByType[type] = prop.value;
204
+ }
205
+ }
206
+ });
207
+ }
208
+ });
209
+ options.properties = [];
210
+ }
211
+ }
212
+ }
213
+ }
214
+ }
215
+ },
216
+ state
217
+ );
218
+ splitNodeTypes.forEach((splitType) => {
219
+ let splitNode = splitNodesByType[splitType];
220
+ if (!splitNode) {
221
+ return;
222
+ }
223
+ while (t__namespace.isIdentifier(splitNode)) {
224
+ const binding = programPath.scope.getBinding(
225
+ splitNode.name
226
+ );
227
+ splitNode = binding == null ? void 0 : binding.path.node;
228
+ }
229
+ if (splitNode) {
230
+ if (t__namespace.isFunctionDeclaration(splitNode)) {
231
+ programPath.pushContainer(
232
+ "body",
233
+ t__namespace.variableDeclaration("const", [
234
+ t__namespace.variableDeclarator(
235
+ t__namespace.identifier(splitType),
236
+ t__namespace.functionExpression(
237
+ splitNode.id || null,
238
+ // Anonymize the function expression
239
+ splitNode.params,
240
+ splitNode.body,
241
+ splitNode.generator,
242
+ splitNode.async
243
+ )
244
+ )
245
+ ])
246
+ );
247
+ } else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
248
+ programPath.pushContainer(
249
+ "body",
250
+ t__namespace.variableDeclaration("const", [
251
+ t__namespace.variableDeclarator(
252
+ t__namespace.identifier(splitType),
253
+ splitNode
254
+ )
255
+ ])
256
+ );
257
+ } else if (t__namespace.isImportSpecifier(splitNode)) {
258
+ programPath.pushContainer(
259
+ "body",
260
+ t__namespace.variableDeclaration("const", [
261
+ t__namespace.variableDeclarator(
262
+ t__namespace.identifier(splitType),
263
+ splitNode.local
264
+ )
265
+ ])
266
+ );
267
+ } else {
268
+ console.info(splitNode);
269
+ throw new Error(
270
+ `Unexpected splitNode type ☝️: ${splitNode.type}`
271
+ );
272
+ }
273
+ }
274
+ programPath.node.body = programPath.node.body.filter(
275
+ (node) => {
276
+ return node !== splitNode;
277
+ }
278
+ );
279
+ programPath.pushContainer("body", [
280
+ t__namespace.exportNamedDeclaration(null, [
281
+ t__namespace.exportSpecifier(
282
+ t__namespace.identifier(splitType),
283
+ t__namespace.identifier(splitType)
284
+ )
285
+ ])
286
+ ]);
287
+ });
288
+ programPath.traverse({
289
+ ExportNamedDeclaration(path) {
290
+ if (path.node.declaration) {
291
+ if (t__namespace.isVariableDeclaration(path.node.declaration)) {
292
+ path.replaceWith(
293
+ t__namespace.importDeclaration(
294
+ path.node.declaration.declarations.map(
295
+ (decl) => t__namespace.importSpecifier(
296
+ t__namespace.identifier(decl.id.name),
297
+ t__namespace.identifier(decl.id.name)
298
+ )
299
+ ),
300
+ t__namespace.stringLiteral(
301
+ opts.filename.split(
302
+ `?${constants.splitPrefix}`
303
+ )[0]
304
+ )
305
+ )
306
+ );
307
+ }
308
+ }
309
+ }
310
+ });
311
+ eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
312
+ }
313
+ }
314
+ }
315
+ },
316
+ {
317
+ root: process.cwd(),
318
+ minify: process.env.NODE_ENV === "production"
319
+ }
320
+ ]
321
+ ].filter(Boolean)
322
+ })
323
+ });
324
+ }
325
+ exports.compileFile = compileFile;
326
+ exports.splitFile = splitFile;
327
+ //# sourceMappingURL=compilers.cjs.map