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