@tanstack/router-plugin 1.121.20 → 1.121.21

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