@tanstack/router-plugin 1.20.3-alpha.1

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 (122) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/cjs/core/code-splitter/compilers.cjs +755 -0
  4. package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -0
  5. package/dist/cjs/core/code-splitter/compilers.d.cts +22 -0
  6. package/dist/cjs/core/code-splitter/framework-options.cjs +34 -0
  7. package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -0
  8. package/dist/cjs/core/code-splitter/framework-options.d.cts +10 -0
  9. package/dist/cjs/core/code-splitter/path-ids.cjs +37 -0
  10. package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -0
  11. package/dist/cjs/core/code-splitter/path-ids.d.cts +2 -0
  12. package/dist/cjs/core/config.cjs +46 -0
  13. package/dist/cjs/core/config.cjs.map +1 -0
  14. package/dist/cjs/core/config.d.cts +160 -0
  15. package/dist/cjs/core/constants.cjs +19 -0
  16. package/dist/cjs/core/constants.cjs.map +1 -0
  17. package/dist/cjs/core/constants.d.cts +5 -0
  18. package/dist/cjs/core/route-autoimport-plugin.cjs +98 -0
  19. package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -0
  20. package/dist/cjs/core/route-autoimport-plugin.d.cts +6 -0
  21. package/dist/cjs/core/route-hmr-statement.cjs +33 -0
  22. package/dist/cjs/core/route-hmr-statement.cjs.map +1 -0
  23. package/dist/cjs/core/route-hmr-statement.d.cts +1 -0
  24. package/dist/cjs/core/router-code-splitter-plugin.cjs +173 -0
  25. package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -0
  26. package/dist/cjs/core/router-code-splitter-plugin.d.cts +3 -0
  27. package/dist/cjs/core/router-composed-plugin.cjs +27 -0
  28. package/dist/cjs/core/router-composed-plugin.cjs.map +1 -0
  29. package/dist/cjs/core/router-composed-plugin.d.cts +3 -0
  30. package/dist/cjs/core/router-generator-plugin.cjs +145 -0
  31. package/dist/cjs/core/router-generator-plugin.cjs.map +1 -0
  32. package/dist/cjs/core/router-generator-plugin.d.cts +3 -0
  33. package/dist/cjs/core/router-hmr-plugin.cjs +51 -0
  34. package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -0
  35. package/dist/cjs/core/router-hmr-plugin.d.cts +8 -0
  36. package/dist/cjs/core/utils.cjs +12 -0
  37. package/dist/cjs/core/utils.cjs.map +1 -0
  38. package/dist/cjs/core/utils.d.cts +2 -0
  39. package/dist/cjs/esbuild.cjs +20 -0
  40. package/dist/cjs/esbuild.cjs.map +1 -0
  41. package/dist/cjs/esbuild.d.cts +127 -0
  42. package/dist/cjs/index.cjs +9 -0
  43. package/dist/cjs/index.cjs.map +1 -0
  44. package/dist/cjs/index.d.cts +4 -0
  45. package/dist/cjs/rspack.cjs +22 -0
  46. package/dist/cjs/rspack.cjs.map +1 -0
  47. package/dist/cjs/rspack.d.cts +139 -0
  48. package/dist/cjs/vite.cjs +22 -0
  49. package/dist/cjs/vite.cjs.map +1 -0
  50. package/dist/cjs/vite.d.cts +159 -0
  51. package/dist/cjs/webpack.cjs +22 -0
  52. package/dist/cjs/webpack.cjs.map +1 -0
  53. package/dist/cjs/webpack.d.cts +127 -0
  54. package/dist/esm/core/code-splitter/compilers.d.ts +22 -0
  55. package/dist/esm/core/code-splitter/compilers.js +737 -0
  56. package/dist/esm/core/code-splitter/compilers.js.map +1 -0
  57. package/dist/esm/core/code-splitter/framework-options.d.ts +10 -0
  58. package/dist/esm/core/code-splitter/framework-options.js +34 -0
  59. package/dist/esm/core/code-splitter/framework-options.js.map +1 -0
  60. package/dist/esm/core/code-splitter/path-ids.d.ts +2 -0
  61. package/dist/esm/core/code-splitter/path-ids.js +37 -0
  62. package/dist/esm/core/code-splitter/path-ids.js.map +1 -0
  63. package/dist/esm/core/config.d.ts +160 -0
  64. package/dist/esm/core/config.js +46 -0
  65. package/dist/esm/core/config.js.map +1 -0
  66. package/dist/esm/core/constants.d.ts +5 -0
  67. package/dist/esm/core/constants.js +19 -0
  68. package/dist/esm/core/constants.js.map +1 -0
  69. package/dist/esm/core/route-autoimport-plugin.d.ts +6 -0
  70. package/dist/esm/core/route-autoimport-plugin.js +81 -0
  71. package/dist/esm/core/route-autoimport-plugin.js.map +1 -0
  72. package/dist/esm/core/route-hmr-statement.d.ts +1 -0
  73. package/dist/esm/core/route-hmr-statement.js +16 -0
  74. package/dist/esm/core/route-hmr-statement.js.map +1 -0
  75. package/dist/esm/core/router-code-splitter-plugin.d.ts +3 -0
  76. package/dist/esm/core/router-code-splitter-plugin.js +173 -0
  77. package/dist/esm/core/router-code-splitter-plugin.js.map +1 -0
  78. package/dist/esm/core/router-composed-plugin.d.ts +3 -0
  79. package/dist/esm/core/router-composed-plugin.js +27 -0
  80. package/dist/esm/core/router-composed-plugin.js.map +1 -0
  81. package/dist/esm/core/router-generator-plugin.d.ts +3 -0
  82. package/dist/esm/core/router-generator-plugin.js +123 -0
  83. package/dist/esm/core/router-generator-plugin.js.map +1 -0
  84. package/dist/esm/core/router-hmr-plugin.d.ts +8 -0
  85. package/dist/esm/core/router-hmr-plugin.js +51 -0
  86. package/dist/esm/core/router-hmr-plugin.js.map +1 -0
  87. package/dist/esm/core/utils.d.ts +2 -0
  88. package/dist/esm/core/utils.js +12 -0
  89. package/dist/esm/core/utils.js.map +1 -0
  90. package/dist/esm/esbuild.d.ts +127 -0
  91. package/dist/esm/esbuild.js +20 -0
  92. package/dist/esm/esbuild.js.map +1 -0
  93. package/dist/esm/index.d.ts +4 -0
  94. package/dist/esm/index.js +9 -0
  95. package/dist/esm/index.js.map +1 -0
  96. package/dist/esm/rspack.d.ts +139 -0
  97. package/dist/esm/rspack.js +22 -0
  98. package/dist/esm/rspack.js.map +1 -0
  99. package/dist/esm/vite.d.ts +159 -0
  100. package/dist/esm/vite.js +22 -0
  101. package/dist/esm/vite.js.map +1 -0
  102. package/dist/esm/webpack.d.ts +127 -0
  103. package/dist/esm/webpack.js +22 -0
  104. package/dist/esm/webpack.js.map +1 -0
  105. package/package.json +133 -0
  106. package/src/core/code-splitter/compilers.ts +1005 -0
  107. package/src/core/code-splitter/framework-options.ts +41 -0
  108. package/src/core/code-splitter/path-ids.ts +39 -0
  109. package/src/core/config.ts +80 -0
  110. package/src/core/constants.ts +17 -0
  111. package/src/core/route-autoimport-plugin.ts +102 -0
  112. package/src/core/route-hmr-statement.ts +13 -0
  113. package/src/core/router-code-splitter-plugin.ts +253 -0
  114. package/src/core/router-composed-plugin.ts +32 -0
  115. package/src/core/router-generator-plugin.ts +172 -0
  116. package/src/core/router-hmr-plugin.ts +65 -0
  117. package/src/core/utils.ts +18 -0
  118. package/src/esbuild.ts +56 -0
  119. package/src/index.ts +4 -0
  120. package/src/rspack.ts +67 -0
  121. package/src/vite.ts +57 -0
  122. package/src/webpack.ts +55 -0
@@ -0,0 +1,755 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const t = require("@babel/types");
4
+ const babel = require("@babel/core");
5
+ const template = require("@babel/template");
6
+ const babelDeadCodeElimination = require("babel-dead-code-elimination");
7
+ const routerUtils = require("@tanstack/router-utils");
8
+ const constants = require("../constants.cjs");
9
+ const routeHmrStatement = require("../route-hmr-statement.cjs");
10
+ const pathIds = require("./path-ids.cjs");
11
+ const frameworkOptions = require("./framework-options.cjs");
12
+ function _interopNamespaceDefault(e) {
13
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
14
+ if (e) {
15
+ for (const k in e) {
16
+ if (k !== "default") {
17
+ const d = Object.getOwnPropertyDescriptor(e, k);
18
+ Object.defineProperty(n, k, d.get ? d : {
19
+ enumerable: true,
20
+ get: () => e[k]
21
+ });
22
+ }
23
+ }
24
+ }
25
+ n.default = e;
26
+ return Object.freeze(n);
27
+ }
28
+ const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
29
+ const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
30
+ process.env.TSR_VITE_DEBUG;
31
+ const SPLIT_NODES_CONFIG = /* @__PURE__ */ new Map([
32
+ [
33
+ "loader",
34
+ {
35
+ routeIdent: "loader",
36
+ localImporterIdent: "$$splitLoaderImporter",
37
+ // const $$splitLoaderImporter = () => import('...')
38
+ splitStrategy: "lazyFn",
39
+ localExporterIdent: "SplitLoader",
40
+ // const SplitLoader = ...
41
+ exporterIdent: "loader"
42
+ // export { SplitLoader as loader }
43
+ }
44
+ ],
45
+ [
46
+ "component",
47
+ {
48
+ routeIdent: "component",
49
+ localImporterIdent: "$$splitComponentImporter",
50
+ // const $$splitComponentImporter = () => import('...')
51
+ splitStrategy: "lazyRouteComponent",
52
+ localExporterIdent: "SplitComponent",
53
+ // const SplitComponent = ...
54
+ exporterIdent: "component"
55
+ // export { SplitComponent as component }
56
+ }
57
+ ],
58
+ [
59
+ "pendingComponent",
60
+ {
61
+ routeIdent: "pendingComponent",
62
+ localImporterIdent: "$$splitPendingComponentImporter",
63
+ // const $$splitPendingComponentImporter = () => import('...')
64
+ splitStrategy: "lazyRouteComponent",
65
+ localExporterIdent: "SplitPendingComponent",
66
+ // const SplitPendingComponent = ...
67
+ exporterIdent: "pendingComponent"
68
+ // export { SplitPendingComponent as pendingComponent }
69
+ }
70
+ ],
71
+ [
72
+ "errorComponent",
73
+ {
74
+ routeIdent: "errorComponent",
75
+ localImporterIdent: "$$splitErrorComponentImporter",
76
+ // const $$splitErrorComponentImporter = () => import('...')
77
+ splitStrategy: "lazyRouteComponent",
78
+ localExporterIdent: "SplitErrorComponent",
79
+ // const SplitErrorComponent = ...
80
+ exporterIdent: "errorComponent"
81
+ // export { SplitErrorComponent as errorComponent }
82
+ }
83
+ ],
84
+ [
85
+ "notFoundComponent",
86
+ {
87
+ routeIdent: "notFoundComponent",
88
+ localImporterIdent: "$$splitNotFoundComponentImporter",
89
+ // const $$splitNotFoundComponentImporter = () => import('...')
90
+ splitStrategy: "lazyRouteComponent",
91
+ localExporterIdent: "SplitNotFoundComponent",
92
+ // const SplitNotFoundComponent = ...
93
+ exporterIdent: "notFoundComponent"
94
+ // export { SplitNotFoundComponent as notFoundComponent }
95
+ }
96
+ ]
97
+ ]);
98
+ const KNOWN_SPLIT_ROUTE_IDENTS = [...SPLIT_NODES_CONFIG.keys()];
99
+ function addSplitSearchParamToFilename(filename, grouping) {
100
+ const [bareFilename] = filename.split("?");
101
+ const params = new URLSearchParams();
102
+ params.append(constants.tsrSplit, pathIds.createIdentifier(grouping));
103
+ return `${bareFilename}?${params.toString()}`;
104
+ }
105
+ function removeSplitSearchParamFromFilename(filename) {
106
+ const [bareFilename] = filename.split("?");
107
+ return bareFilename;
108
+ }
109
+ function compileCodeSplitReferenceRoute(opts) {
110
+ const ast = routerUtils.parseAst(opts);
111
+ const refIdents = babelDeadCodeElimination.findReferencedIdentifiers(ast);
112
+ function findIndexForSplitNode(str) {
113
+ return opts.codeSplitGroupings.findIndex(
114
+ (group) => group.includes(str)
115
+ );
116
+ }
117
+ const frameworkOptions$1 = frameworkOptions.getFrameworkOptions(opts.targetFramework);
118
+ const PACKAGE = frameworkOptions$1.package;
119
+ const LAZY_ROUTE_COMPONENT_IDENT = frameworkOptions$1.idents.lazyRouteComponent;
120
+ const LAZY_FN_IDENT = frameworkOptions$1.idents.lazyFn;
121
+ babel.traverse(ast, {
122
+ Program: {
123
+ enter(programPath, programState) {
124
+ const state = programState;
125
+ const removableImportPaths = /* @__PURE__ */ new Set([]);
126
+ programPath.traverse(
127
+ {
128
+ CallExpression: (path) => {
129
+ if (!t__namespace.isIdentifier(path.node.callee)) {
130
+ return;
131
+ }
132
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
133
+ return;
134
+ }
135
+ function babelHandleReference(routeOptions) {
136
+ const hasImportedOrDefinedIdentifier = (name) => {
137
+ return programPath.scope.hasBinding(name);
138
+ };
139
+ if (t__namespace.isObjectExpression(routeOptions)) {
140
+ routeOptions.properties.forEach((prop) => {
141
+ if (t__namespace.isObjectProperty(prop)) {
142
+ if (t__namespace.isIdentifier(prop.key)) {
143
+ const codeSplitGroupingByKey = findIndexForSplitNode(
144
+ prop.key.name
145
+ );
146
+ if (codeSplitGroupingByKey === -1) {
147
+ return;
148
+ }
149
+ const codeSplitGroup = [
150
+ ...new Set(
151
+ opts.codeSplitGroupings[codeSplitGroupingByKey]
152
+ )
153
+ ];
154
+ const key = prop.key.name;
155
+ const isNodeConfigAvailable = SPLIT_NODES_CONFIG.has(
156
+ key
157
+ );
158
+ if (!isNodeConfigAvailable) {
159
+ return;
160
+ }
161
+ const splitNodeMeta = SPLIT_NODES_CONFIG.get(
162
+ key
163
+ );
164
+ const splitUrl = addSplitSearchParamToFilename(
165
+ opts.filename,
166
+ codeSplitGroup
167
+ );
168
+ if (splitNodeMeta.splitStrategy === "lazyRouteComponent") {
169
+ const value = prop.value;
170
+ let shouldSplit = true;
171
+ if (t__namespace.isIdentifier(value)) {
172
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(
173
+ programPath,
174
+ value.name
175
+ ).path;
176
+ if (existingImportPath) {
177
+ removableImportPaths.add(existingImportPath);
178
+ }
179
+ const isExported = hasExport(ast, value);
180
+ shouldSplit = !isExported;
181
+ if (shouldSplit) {
182
+ removeIdentifierLiteral(path, value);
183
+ }
184
+ }
185
+ if (!shouldSplit) {
186
+ return;
187
+ }
188
+ if (!hasImportedOrDefinedIdentifier(
189
+ LAZY_ROUTE_COMPONENT_IDENT
190
+ )) {
191
+ programPath.unshiftContainer("body", [
192
+ template__namespace.statement(
193
+ `import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`
194
+ )()
195
+ ]);
196
+ }
197
+ if (!hasImportedOrDefinedIdentifier(
198
+ splitNodeMeta.localImporterIdent
199
+ )) {
200
+ programPath.unshiftContainer("body", [
201
+ template__namespace.statement(
202
+ `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
203
+ )()
204
+ ]);
205
+ }
206
+ if (key === "component") {
207
+ prop.value = template__namespace.expression(
208
+ `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}', () => Route.ssr)`
209
+ )();
210
+ } else {
211
+ prop.value = template__namespace.expression(
212
+ `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
213
+ )();
214
+ }
215
+ if (opts.runtimeEnv !== "prod") {
216
+ programPath.pushContainer("body", routeHmrStatement.routeHmrStatement);
217
+ }
218
+ }
219
+ if (splitNodeMeta.splitStrategy === "lazyFn") {
220
+ const value = prop.value;
221
+ let shouldSplit = true;
222
+ if (t__namespace.isIdentifier(value)) {
223
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(
224
+ programPath,
225
+ value.name
226
+ ).path;
227
+ if (existingImportPath) {
228
+ removableImportPaths.add(existingImportPath);
229
+ }
230
+ const isExported = hasExport(ast, value);
231
+ shouldSplit = !isExported;
232
+ if (shouldSplit) {
233
+ removeIdentifierLiteral(path, value);
234
+ }
235
+ }
236
+ if (!shouldSplit) {
237
+ return;
238
+ }
239
+ if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
240
+ programPath.unshiftContainer(
241
+ "body",
242
+ template__namespace.smart(
243
+ `import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`
244
+ )()
245
+ );
246
+ }
247
+ if (!hasImportedOrDefinedIdentifier(
248
+ splitNodeMeta.localImporterIdent
249
+ )) {
250
+ programPath.unshiftContainer("body", [
251
+ template__namespace.statement(
252
+ `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
253
+ )()
254
+ ]);
255
+ }
256
+ prop.value = template__namespace.expression(
257
+ `${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
258
+ )();
259
+ }
260
+ }
261
+ }
262
+ programPath.scope.crawl();
263
+ });
264
+ }
265
+ }
266
+ if (t__namespace.isCallExpression(path.parentPath.node)) {
267
+ const options = resolveIdentifier(
268
+ path,
269
+ path.parentPath.node.arguments[0]
270
+ );
271
+ babelHandleReference(options);
272
+ } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
273
+ const caller = resolveIdentifier(
274
+ path,
275
+ path.parentPath.node.init
276
+ );
277
+ if (t__namespace.isCallExpression(caller)) {
278
+ const options = resolveIdentifier(path, caller.arguments[0]);
279
+ babelHandleReference(options);
280
+ }
281
+ }
282
+ }
283
+ },
284
+ state
285
+ );
286
+ if (removableImportPaths.size > 0) {
287
+ programPath.traverse({
288
+ ImportDeclaration(path) {
289
+ if (path.node.specifiers.length > 0) return;
290
+ if (removableImportPaths.has(path.node.source.value)) {
291
+ path.remove();
292
+ }
293
+ }
294
+ });
295
+ }
296
+ }
297
+ }
298
+ });
299
+ babelDeadCodeElimination.deadCodeElimination(ast, refIdents);
300
+ return routerUtils.generateFromAst(ast, {
301
+ sourceMaps: true,
302
+ sourceFileName: opts.filename,
303
+ filename: opts.filename
304
+ });
305
+ }
306
+ function compileCodeSplitVirtualRoute(opts) {
307
+ const ast = routerUtils.parseAst(opts);
308
+ const refIdents = babelDeadCodeElimination.findReferencedIdentifiers(ast);
309
+ const intendedSplitNodes = new Set(opts.splitTargets);
310
+ const knownExportedIdents = /* @__PURE__ */ new Set();
311
+ babel.traverse(ast, {
312
+ Program: {
313
+ enter(programPath, programState) {
314
+ const state = programState;
315
+ const trackedNodesToSplitByType = {
316
+ component: void 0,
317
+ loader: void 0,
318
+ pendingComponent: void 0,
319
+ errorComponent: void 0,
320
+ notFoundComponent: void 0
321
+ };
322
+ programPath.traverse(
323
+ {
324
+ CallExpression: (path) => {
325
+ if (!t__namespace.isIdentifier(path.node.callee)) {
326
+ return;
327
+ }
328
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
329
+ return;
330
+ }
331
+ function babelHandleVirtual(options) {
332
+ if (t__namespace.isObjectExpression(options)) {
333
+ options.properties.forEach((prop) => {
334
+ if (t__namespace.isObjectProperty(prop)) {
335
+ KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
336
+ if (!t__namespace.isIdentifier(prop.key) || prop.key.name !== splitType) {
337
+ return;
338
+ }
339
+ const value = prop.value;
340
+ let isExported = false;
341
+ if (t__namespace.isIdentifier(value)) {
342
+ isExported = hasExport(ast, value);
343
+ if (isExported) {
344
+ knownExportedIdents.add(value.name);
345
+ }
346
+ }
347
+ if (isExported && t__namespace.isIdentifier(value)) {
348
+ removeExports(ast, value);
349
+ } else {
350
+ const meta = SPLIT_NODES_CONFIG.get(splitType);
351
+ trackedNodesToSplitByType[splitType] = {
352
+ node: prop.value,
353
+ meta
354
+ };
355
+ }
356
+ });
357
+ }
358
+ });
359
+ options.properties = [];
360
+ }
361
+ }
362
+ if (t__namespace.isCallExpression(path.parentPath.node)) {
363
+ const options = resolveIdentifier(
364
+ path,
365
+ path.parentPath.node.arguments[0]
366
+ );
367
+ babelHandleVirtual(options);
368
+ } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
369
+ const caller = resolveIdentifier(
370
+ path,
371
+ path.parentPath.node.init
372
+ );
373
+ if (t__namespace.isCallExpression(caller)) {
374
+ const options = resolveIdentifier(path, caller.arguments[0]);
375
+ babelHandleVirtual(options);
376
+ }
377
+ }
378
+ }
379
+ },
380
+ state
381
+ );
382
+ intendedSplitNodes.forEach((SPLIT_TYPE) => {
383
+ const splitKey = trackedNodesToSplitByType[SPLIT_TYPE];
384
+ if (!splitKey) {
385
+ return;
386
+ }
387
+ let splitNode = splitKey.node;
388
+ const splitMeta = splitKey.meta;
389
+ while (t__namespace.isIdentifier(splitNode)) {
390
+ const binding = programPath.scope.getBinding(splitNode.name);
391
+ splitNode = binding == null ? void 0 : binding.path.node;
392
+ }
393
+ if (splitNode) {
394
+ if (t__namespace.isFunctionDeclaration(splitNode)) {
395
+ programPath.pushContainer(
396
+ "body",
397
+ t__namespace.variableDeclaration("const", [
398
+ t__namespace.variableDeclarator(
399
+ t__namespace.identifier(splitMeta.localExporterIdent),
400
+ t__namespace.functionExpression(
401
+ splitNode.id || null,
402
+ // Anonymize the function expression
403
+ splitNode.params,
404
+ splitNode.body,
405
+ splitNode.generator,
406
+ splitNode.async
407
+ )
408
+ )
409
+ ])
410
+ );
411
+ } else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
412
+ programPath.pushContainer(
413
+ "body",
414
+ t__namespace.variableDeclaration("const", [
415
+ t__namespace.variableDeclarator(
416
+ t__namespace.identifier(splitMeta.localExporterIdent),
417
+ splitNode
418
+ )
419
+ ])
420
+ );
421
+ } else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
422
+ programPath.pushContainer(
423
+ "body",
424
+ t__namespace.variableDeclaration("const", [
425
+ t__namespace.variableDeclarator(
426
+ t__namespace.identifier(splitMeta.localExporterIdent),
427
+ splitNode.local
428
+ )
429
+ ])
430
+ );
431
+ } else if (t__namespace.isVariableDeclarator(splitNode)) {
432
+ programPath.pushContainer(
433
+ "body",
434
+ t__namespace.variableDeclaration("const", [
435
+ t__namespace.variableDeclarator(
436
+ t__namespace.identifier(splitMeta.localExporterIdent),
437
+ splitNode.init
438
+ )
439
+ ])
440
+ );
441
+ } else if (t__namespace.isCallExpression(splitNode)) {
442
+ const outputSplitNodeCode = routerUtils.generateFromAst(splitNode).code;
443
+ const splitNodeAst = babel.parse(outputSplitNodeCode);
444
+ if (!splitNodeAst) {
445
+ throw new Error(
446
+ `Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`
447
+ );
448
+ }
449
+ const statement = splitNodeAst.program.body[0];
450
+ if (!statement) {
451
+ throw new Error(
452
+ `Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}" as no statement was found in the program body`
453
+ );
454
+ }
455
+ if (t__namespace.isExpressionStatement(statement)) {
456
+ const expression = statement.expression;
457
+ programPath.pushContainer(
458
+ "body",
459
+ t__namespace.variableDeclaration("const", [
460
+ t__namespace.variableDeclarator(
461
+ t__namespace.identifier(splitMeta.localExporterIdent),
462
+ expression
463
+ )
464
+ ])
465
+ );
466
+ } else {
467
+ throw new Error(
468
+ `Unexpected expression type encounter for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`
469
+ );
470
+ }
471
+ } else if (t__namespace.isConditionalExpression(splitNode)) {
472
+ programPath.pushContainer(
473
+ "body",
474
+ t__namespace.variableDeclaration("const", [
475
+ t__namespace.variableDeclarator(
476
+ t__namespace.identifier(splitMeta.localExporterIdent),
477
+ splitNode
478
+ )
479
+ ])
480
+ );
481
+ } else if (t__namespace.isTSAsExpression(splitNode)) {
482
+ splitNode = splitNode.expression;
483
+ programPath.pushContainer(
484
+ "body",
485
+ t__namespace.variableDeclaration("const", [
486
+ t__namespace.variableDeclarator(
487
+ t__namespace.identifier(splitMeta.localExporterIdent),
488
+ splitNode
489
+ )
490
+ ])
491
+ );
492
+ } else {
493
+ console.info("Unexpected splitNode type:", splitNode);
494
+ throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
495
+ }
496
+ }
497
+ programPath.node.body = programPath.node.body.filter((node) => {
498
+ return node !== splitNode;
499
+ });
500
+ programPath.pushContainer("body", [
501
+ t__namespace.exportNamedDeclaration(null, [
502
+ t__namespace.exportSpecifier(
503
+ t__namespace.identifier(splitMeta.localExporterIdent),
504
+ // local variable name
505
+ t__namespace.identifier(splitMeta.exporterIdent)
506
+ // as what name it should be exported as
507
+ )
508
+ ])
509
+ ]);
510
+ });
511
+ programPath.traverse({
512
+ ExportNamedDeclaration(path) {
513
+ if (path.node.declaration) {
514
+ if (t__namespace.isVariableDeclaration(path.node.declaration)) {
515
+ path.replaceWith(
516
+ t__namespace.importDeclaration(
517
+ path.node.declaration.declarations.map(
518
+ (decl) => t__namespace.importSpecifier(
519
+ t__namespace.identifier(decl.id.name),
520
+ t__namespace.identifier(decl.id.name)
521
+ )
522
+ ),
523
+ t__namespace.stringLiteral(
524
+ removeSplitSearchParamFromFilename(opts.filename)
525
+ )
526
+ )
527
+ );
528
+ }
529
+ }
530
+ }
531
+ });
532
+ }
533
+ }
534
+ });
535
+ babelDeadCodeElimination.deadCodeElimination(ast, refIdents);
536
+ if (knownExportedIdents.size > 0) {
537
+ const list = Array.from(knownExportedIdents).reduce((str, ident) => {
538
+ str += `
539
+ - ${ident}`;
540
+ return str;
541
+ }, "");
542
+ const warningMessage = `These exports from "${opts.filename}" are not being code-split and will increase your bundle size: ${list}
543
+ These should either have their export statements removed or be imported from another file that is not a route.`;
544
+ console.warn(warningMessage);
545
+ if (process.env.NODE_ENV !== "production") {
546
+ const warningTemplate = template__namespace.statement(
547
+ `console.warn(${JSON.stringify(warningMessage)})`
548
+ )();
549
+ ast.program.body.unshift(warningTemplate);
550
+ }
551
+ }
552
+ return routerUtils.generateFromAst(ast, {
553
+ sourceMaps: true,
554
+ sourceFileName: opts.filename,
555
+ filename: opts.filename
556
+ });
557
+ }
558
+ function detectCodeSplitGroupingsFromRoute(opts) {
559
+ const ast = routerUtils.parseAst(opts);
560
+ let routeId = "";
561
+ let codeSplitGroupings = void 0;
562
+ babel.traverse(ast, {
563
+ Program: {
564
+ enter(programPath) {
565
+ programPath.traverse({
566
+ CallExpression(path) {
567
+ if (!t__namespace.isIdentifier(path.node.callee)) {
568
+ return;
569
+ }
570
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
571
+ return;
572
+ }
573
+ if (t__namespace.isCallExpression(path.parentPath.node)) {
574
+ if (t__namespace.isCallExpression(path.parentPath.node.callee)) {
575
+ const callee = path.parentPath.node.callee;
576
+ if (t__namespace.isIdentifier(callee.callee)) {
577
+ const firstArg = callee.arguments[0];
578
+ if (t__namespace.isStringLiteral(firstArg)) {
579
+ routeId = firstArg.value;
580
+ }
581
+ }
582
+ }
583
+ const options = resolveIdentifier(
584
+ path,
585
+ path.parentPath.node.arguments[0]
586
+ );
587
+ if (t__namespace.isObjectExpression(options)) {
588
+ options.properties.forEach((prop) => {
589
+ if (t__namespace.isObjectProperty(prop)) {
590
+ if (t__namespace.isIdentifier(prop.key)) {
591
+ if (prop.key.name === "codeSplitGroupings") {
592
+ const value = prop.value;
593
+ if (t__namespace.isArrayExpression(value)) {
594
+ codeSplitGroupings = value.elements.map((group) => {
595
+ if (t__namespace.isArrayExpression(group)) {
596
+ return group.elements.map((node) => {
597
+ if (!t__namespace.isStringLiteral(node)) {
598
+ throw new Error(
599
+ "You must provide a string literal for the codeSplitGroupings"
600
+ );
601
+ }
602
+ return node.value;
603
+ });
604
+ }
605
+ throw new Error(
606
+ "You must provide arrays with codeSplitGroupings options."
607
+ );
608
+ });
609
+ } else {
610
+ throw new Error(
611
+ "You must provide an array of arrays for the codeSplitGroupings."
612
+ );
613
+ }
614
+ }
615
+ }
616
+ }
617
+ });
618
+ }
619
+ }
620
+ }
621
+ });
622
+ }
623
+ }
624
+ });
625
+ return { groupings: codeSplitGroupings, routeId };
626
+ }
627
+ function getImportSpecifierAndPathFromLocalName(programPath, name) {
628
+ let specifier = null;
629
+ let path = null;
630
+ programPath.traverse({
631
+ ImportDeclaration(importPath) {
632
+ const found = importPath.node.specifiers.find(
633
+ (targetSpecifier) => targetSpecifier.local.name === name
634
+ );
635
+ if (found) {
636
+ specifier = found;
637
+ path = importPath.node.source.value;
638
+ }
639
+ }
640
+ });
641
+ return { specifier, path };
642
+ }
643
+ function resolveIdentifier(path, node) {
644
+ if (t__namespace.isIdentifier(node)) {
645
+ const binding = path.scope.getBinding(node.name);
646
+ if (binding) {
647
+ const declarator = binding.path.node;
648
+ if (t__namespace.isObjectExpression(declarator.init)) {
649
+ return declarator.init;
650
+ } else if (t__namespace.isFunctionDeclaration(declarator.init)) {
651
+ return declarator.init;
652
+ }
653
+ }
654
+ return void 0;
655
+ }
656
+ return node;
657
+ }
658
+ function removeIdentifierLiteral(path, node) {
659
+ if (t__namespace.isIdentifier(node)) {
660
+ const binding = path.scope.getBinding(node.name);
661
+ if (binding) {
662
+ binding.path.remove();
663
+ }
664
+ }
665
+ }
666
+ function hasExport(ast, node) {
667
+ let found = false;
668
+ babel.traverse(ast, {
669
+ ExportNamedDeclaration(path) {
670
+ if (path.node.declaration) {
671
+ if (t__namespace.isVariableDeclaration(path.node.declaration)) {
672
+ path.node.declaration.declarations.forEach((decl) => {
673
+ if (t__namespace.isVariableDeclarator(decl)) {
674
+ if (t__namespace.isIdentifier(decl.id)) {
675
+ if (decl.id.name === node.name) {
676
+ found = true;
677
+ }
678
+ }
679
+ }
680
+ });
681
+ }
682
+ if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
683
+ if (t__namespace.isIdentifier(path.node.declaration.id)) {
684
+ if (path.node.declaration.id.name === node.name) {
685
+ found = true;
686
+ }
687
+ }
688
+ }
689
+ }
690
+ },
691
+ ExportDefaultDeclaration(path) {
692
+ if (t__namespace.isIdentifier(path.node.declaration)) {
693
+ if (path.node.declaration.name === node.name) {
694
+ found = true;
695
+ }
696
+ }
697
+ if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
698
+ if (t__namespace.isIdentifier(path.node.declaration.id)) {
699
+ if (path.node.declaration.id.name === node.name) {
700
+ found = true;
701
+ }
702
+ }
703
+ }
704
+ }
705
+ });
706
+ return found;
707
+ }
708
+ function removeExports(ast, node) {
709
+ let removed = false;
710
+ babel.traverse(ast, {
711
+ ExportNamedDeclaration(path) {
712
+ if (path.node.declaration) {
713
+ if (t__namespace.isVariableDeclaration(path.node.declaration)) {
714
+ path.node.declaration.declarations.forEach((decl) => {
715
+ if (t__namespace.isVariableDeclarator(decl)) {
716
+ if (t__namespace.isIdentifier(decl.id)) {
717
+ if (decl.id.name === node.name) {
718
+ path.remove();
719
+ removed = true;
720
+ }
721
+ }
722
+ }
723
+ });
724
+ } else if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
725
+ if (t__namespace.isIdentifier(path.node.declaration.id)) {
726
+ if (path.node.declaration.id.name === node.name) {
727
+ path.remove();
728
+ removed = true;
729
+ }
730
+ }
731
+ }
732
+ }
733
+ },
734
+ ExportDefaultDeclaration(path) {
735
+ if (t__namespace.isIdentifier(path.node.declaration)) {
736
+ if (path.node.declaration.name === node.name) {
737
+ path.remove();
738
+ removed = true;
739
+ }
740
+ } else if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
741
+ if (t__namespace.isIdentifier(path.node.declaration.id)) {
742
+ if (path.node.declaration.id.name === node.name) {
743
+ path.remove();
744
+ removed = true;
745
+ }
746
+ }
747
+ }
748
+ }
749
+ });
750
+ return removed;
751
+ }
752
+ exports.compileCodeSplitReferenceRoute = compileCodeSplitReferenceRoute;
753
+ exports.compileCodeSplitVirtualRoute = compileCodeSplitVirtualRoute;
754
+ exports.detectCodeSplitGroupingsFromRoute = detectCodeSplitGroupingsFromRoute;
755
+ //# sourceMappingURL=compilers.cjs.map