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