@tanstack/router-plugin 1.166.9 → 1.166.11

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 (80) hide show
  1. package/dist/cjs/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/cjs/core/code-splitter/compilers.cjs +866 -1324
  3. package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
  4. package/dist/cjs/core/code-splitter/framework-options.cjs +39 -41
  5. package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -1
  6. package/dist/cjs/core/code-splitter/path-ids.cjs +26 -31
  7. package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -1
  8. package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs +11 -14
  9. package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs.map +1 -1
  10. package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs +31 -65
  11. package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs.map +1 -1
  12. package/dist/cjs/core/config.cjs +36 -46
  13. package/dist/cjs/core/config.cjs.map +1 -1
  14. package/dist/cjs/core/constants.cjs +16 -15
  15. package/dist/cjs/core/constants.cjs.map +1 -1
  16. package/dist/cjs/core/route-autoimport-plugin.cjs +81 -115
  17. package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
  18. package/dist/cjs/core/route-hmr-statement.cjs +34 -56
  19. package/dist/cjs/core/route-hmr-statement.cjs.map +1 -1
  20. package/dist/cjs/core/router-code-splitter-plugin.cjs +236 -306
  21. package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
  22. package/dist/cjs/core/router-composed-plugin.cjs +28 -34
  23. package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
  24. package/dist/cjs/core/router-generator-plugin.cjs +101 -133
  25. package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
  26. package/dist/cjs/core/router-hmr-plugin.cjs +81 -81
  27. package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
  28. package/dist/cjs/core/utils.cjs +23 -30
  29. package/dist/cjs/core/utils.cjs.map +1 -1
  30. package/dist/cjs/esbuild.cjs +47 -18
  31. package/dist/cjs/esbuild.cjs.map +1 -1
  32. package/dist/cjs/index.cjs +11 -13
  33. package/dist/cjs/rspack.cjs +58 -19
  34. package/dist/cjs/rspack.cjs.map +1 -1
  35. package/dist/cjs/vite.cjs +52 -20
  36. package/dist/cjs/vite.cjs.map +1 -1
  37. package/dist/cjs/webpack.cjs +46 -19
  38. package/dist/cjs/webpack.cjs.map +1 -1
  39. package/dist/esm/core/code-splitter/compilers.js +859 -1307
  40. package/dist/esm/core/code-splitter/compilers.js.map +1 -1
  41. package/dist/esm/core/code-splitter/framework-options.js +40 -42
  42. package/dist/esm/core/code-splitter/framework-options.js.map +1 -1
  43. package/dist/esm/core/code-splitter/path-ids.js +27 -33
  44. package/dist/esm/core/code-splitter/path-ids.js.map +1 -1
  45. package/dist/esm/core/code-splitter/plugins/framework-plugins.js +11 -14
  46. package/dist/esm/core/code-splitter/plugins/framework-plugins.js.map +1 -1
  47. package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js +29 -48
  48. package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js.map +1 -1
  49. package/dist/esm/core/config.js +35 -48
  50. package/dist/esm/core/config.js.map +1 -1
  51. package/dist/esm/core/constants.js +17 -19
  52. package/dist/esm/core/constants.js.map +1 -1
  53. package/dist/esm/core/route-autoimport-plugin.js +78 -98
  54. package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
  55. package/dist/esm/core/route-hmr-statement.js +32 -39
  56. package/dist/esm/core/route-hmr-statement.js.map +1 -1
  57. package/dist/esm/core/router-code-splitter-plugin.js +234 -305
  58. package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
  59. package/dist/esm/core/router-composed-plugin.js +25 -32
  60. package/dist/esm/core/router-composed-plugin.js.map +1 -1
  61. package/dist/esm/core/router-generator-plugin.js +100 -111
  62. package/dist/esm/core/router-generator-plugin.js.map +1 -1
  63. package/dist/esm/core/router-hmr-plugin.js +79 -80
  64. package/dist/esm/core/router-hmr-plugin.js.map +1 -1
  65. package/dist/esm/core/utils.js +21 -15
  66. package/dist/esm/core/utils.js.map +1 -1
  67. package/dist/esm/esbuild.js +37 -18
  68. package/dist/esm/esbuild.js.map +1 -1
  69. package/dist/esm/index.js +2 -11
  70. package/dist/esm/rspack.js +49 -20
  71. package/dist/esm/rspack.js.map +1 -1
  72. package/dist/esm/vite.js +42 -21
  73. package/dist/esm/vite.js.map +1 -1
  74. package/dist/esm/webpack.js +37 -20
  75. package/dist/esm/webpack.js.map +1 -1
  76. package/package.json +6 -6
  77. package/src/core/code-splitter/compilers.ts +1 -1
  78. package/src/core/route-autoimport-plugin.ts +1 -1
  79. package/dist/cjs/index.cjs.map +0 -1
  80. package/dist/esm/index.js.map +0 -1
@@ -1,1382 +1,924 @@
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 routerUtils = require("@tanstack/router-utils");
7
- const constants = require("../constants.cjs");
8
- const routeHmrStatement = require("../route-hmr-statement.cjs");
9
- const pathIds = require("./path-ids.cjs");
10
- const frameworkOptions = require("./framework-options.cjs");
11
- function _interopNamespaceDefault(e) {
12
- const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
13
- if (e) {
14
- for (const k in e) {
15
- if (k !== "default") {
16
- const d = Object.getOwnPropertyDescriptor(e, k);
17
- Object.defineProperty(n, k, d.get ? d : {
18
- enumerable: true,
19
- get: () => e[k]
20
- });
21
- }
22
- }
23
- }
24
- n.default = e;
25
- return Object.freeze(n);
26
- }
27
- const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
28
- const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
29
- const SPLIT_NODES_CONFIG = /* @__PURE__ */ new Map([
30
- [
31
- "loader",
32
- {
33
- routeIdent: "loader",
34
- localImporterIdent: "$$splitLoaderImporter",
35
- // const $$splitLoaderImporter = () => import('...')
36
- splitStrategy: "lazyFn",
37
- localExporterIdent: "SplitLoader",
38
- // const SplitLoader = ...
39
- exporterIdent: "loader"
40
- // export { SplitLoader as loader }
41
- }
42
- ],
43
- [
44
- "component",
45
- {
46
- routeIdent: "component",
47
- localImporterIdent: "$$splitComponentImporter",
48
- // const $$splitComponentImporter = () => import('...')
49
- splitStrategy: "lazyRouteComponent",
50
- localExporterIdent: "SplitComponent",
51
- // const SplitComponent = ...
52
- exporterIdent: "component"
53
- // export { SplitComponent as component }
54
- }
55
- ],
56
- [
57
- "pendingComponent",
58
- {
59
- routeIdent: "pendingComponent",
60
- localImporterIdent: "$$splitPendingComponentImporter",
61
- // const $$splitPendingComponentImporter = () => import('...')
62
- splitStrategy: "lazyRouteComponent",
63
- localExporterIdent: "SplitPendingComponent",
64
- // const SplitPendingComponent = ...
65
- exporterIdent: "pendingComponent"
66
- // export { SplitPendingComponent as pendingComponent }
67
- }
68
- ],
69
- [
70
- "errorComponent",
71
- {
72
- routeIdent: "errorComponent",
73
- localImporterIdent: "$$splitErrorComponentImporter",
74
- // const $$splitErrorComponentImporter = () => import('...')
75
- splitStrategy: "lazyRouteComponent",
76
- localExporterIdent: "SplitErrorComponent",
77
- // const SplitErrorComponent = ...
78
- exporterIdent: "errorComponent"
79
- // export { SplitErrorComponent as errorComponent }
80
- }
81
- ],
82
- [
83
- "notFoundComponent",
84
- {
85
- routeIdent: "notFoundComponent",
86
- localImporterIdent: "$$splitNotFoundComponentImporter",
87
- // const $$splitNotFoundComponentImporter = () => import('...')
88
- splitStrategy: "lazyRouteComponent",
89
- localExporterIdent: "SplitNotFoundComponent",
90
- // const SplitNotFoundComponent = ...
91
- exporterIdent: "notFoundComponent"
92
- // export { SplitNotFoundComponent as notFoundComponent }
93
- }
94
- ]
1
+ const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
2
+ const require_constants = require("../constants.cjs");
3
+ const require_route_hmr_statement = require("../route-hmr-statement.cjs");
4
+ const require_path_ids = require("./path-ids.cjs");
5
+ const require_framework_options = require("./framework-options.cjs");
6
+ let _tanstack_router_utils = require("@tanstack/router-utils");
7
+ let _babel_types = require("@babel/types");
8
+ _babel_types = require_runtime.__toESM(_babel_types);
9
+ let _babel_core = require("@babel/core");
10
+ _babel_core = require_runtime.__toESM(_babel_core);
11
+ let _babel_template = require("@babel/template");
12
+ _babel_template = require_runtime.__toESM(_babel_template);
13
+ //#region src/core/code-splitter/compilers.ts
14
+ var SPLIT_NODES_CONFIG = new Map([
15
+ ["loader", {
16
+ routeIdent: "loader",
17
+ localImporterIdent: "$$splitLoaderImporter",
18
+ splitStrategy: "lazyFn",
19
+ localExporterIdent: "SplitLoader",
20
+ exporterIdent: "loader"
21
+ }],
22
+ ["component", {
23
+ routeIdent: "component",
24
+ localImporterIdent: "$$splitComponentImporter",
25
+ splitStrategy: "lazyRouteComponent",
26
+ localExporterIdent: "SplitComponent",
27
+ exporterIdent: "component"
28
+ }],
29
+ ["pendingComponent", {
30
+ routeIdent: "pendingComponent",
31
+ localImporterIdent: "$$splitPendingComponentImporter",
32
+ splitStrategy: "lazyRouteComponent",
33
+ localExporterIdent: "SplitPendingComponent",
34
+ exporterIdent: "pendingComponent"
35
+ }],
36
+ ["errorComponent", {
37
+ routeIdent: "errorComponent",
38
+ localImporterIdent: "$$splitErrorComponentImporter",
39
+ splitStrategy: "lazyRouteComponent",
40
+ localExporterIdent: "SplitErrorComponent",
41
+ exporterIdent: "errorComponent"
42
+ }],
43
+ ["notFoundComponent", {
44
+ routeIdent: "notFoundComponent",
45
+ localImporterIdent: "$$splitNotFoundComponentImporter",
46
+ splitStrategy: "lazyRouteComponent",
47
+ localExporterIdent: "SplitNotFoundComponent",
48
+ exporterIdent: "notFoundComponent"
49
+ }]
95
50
  ]);
96
- const KNOWN_SPLIT_ROUTE_IDENTS = [...SPLIT_NODES_CONFIG.keys()];
51
+ var KNOWN_SPLIT_ROUTE_IDENTS = [...SPLIT_NODES_CONFIG.keys()];
97
52
  function addSplitSearchParamToFilename(filename, grouping) {
98
- const [bareFilename] = filename.split("?");
99
- const params = new URLSearchParams();
100
- params.append(constants.tsrSplit, pathIds.createIdentifier(grouping));
101
- const result = `${bareFilename}?${params.toString()}`;
102
- return result;
53
+ const [bareFilename] = filename.split("?");
54
+ const params = new URLSearchParams();
55
+ params.append(require_constants.tsrSplit, require_path_ids.createIdentifier(grouping));
56
+ return `${bareFilename}?${params.toString()}`;
103
57
  }
104
58
  function removeSplitSearchParamFromFilename(filename) {
105
- const [bareFilename] = filename.split("?");
106
- return bareFilename;
59
+ const [bareFilename] = filename.split("?");
60
+ return bareFilename;
107
61
  }
108
62
  function addSharedSearchParamToFilename(filename) {
109
- const [bareFilename] = filename.split("?");
110
- return `${bareFilename}?${constants.tsrShared}=1`;
63
+ const [bareFilename] = filename.split("?");
64
+ return `${bareFilename}?${require_constants.tsrShared}=1`;
111
65
  }
112
- const splittableCreateRouteFns = ["createFileRoute"];
113
- const unsplittableCreateRouteFns = [
114
- "createRootRoute",
115
- "createRootRouteWithContext"
116
- ];
117
- const allCreateRouteFns = [
118
- ...splittableCreateRouteFns,
119
- ...unsplittableCreateRouteFns
120
- ];
66
+ var splittableCreateRouteFns = ["createFileRoute"];
67
+ var unsplittableCreateRouteFns = ["createRootRoute", "createRootRouteWithContext"];
68
+ var allCreateRouteFns = [...splittableCreateRouteFns, ...unsplittableCreateRouteFns];
69
+ /**
70
+ * Recursively walk an AST node and collect referenced identifier-like names.
71
+ * Much cheaper than babel.traverse — no path/scope overhead.
72
+ *
73
+ * Notes:
74
+ * - Uses @babel/types `isReferenced` to avoid collecting non-references like
75
+ * object keys, member expression properties, or binding identifiers.
76
+ * - Also handles JSX identifiers for component references.
77
+ */
121
78
  function collectIdentifiersFromNode(node) {
122
- const ids = /* @__PURE__ */ new Set();
123
- (function walk(n, parent, grandparent, parentKey) {
124
- if (!n) return;
125
- if (t__namespace.isIdentifier(n)) {
126
- if (!parent || t__namespace.isReferenced(n, parent, grandparent)) {
127
- ids.add(n.name);
128
- }
129
- return;
130
- }
131
- if (t__namespace.isJSXIdentifier(n)) {
132
- if (parent && t__namespace.isJSXAttribute(parent) && parentKey === "name") {
133
- return;
134
- }
135
- if (parent && t__namespace.isJSXMemberExpression(parent) && parentKey === "property") {
136
- return;
137
- }
138
- const first = n.name[0];
139
- if (first && first === first.toLowerCase()) {
140
- return;
141
- }
142
- ids.add(n.name);
143
- return;
144
- }
145
- for (const key of t__namespace.VISITOR_KEYS[n.type] || []) {
146
- const child = n[key];
147
- if (Array.isArray(child)) {
148
- for (const c of child) {
149
- if (c && typeof c.type === "string") {
150
- walk(c, n, parent, key);
151
- }
152
- }
153
- } else if (child && typeof child.type === "string") {
154
- walk(child, n, parent, key);
155
- }
156
- }
157
- })(node);
158
- return ids;
79
+ const ids = /* @__PURE__ */ new Set();
80
+ (function walk(n, parent, grandparent, parentKey) {
81
+ if (!n) return;
82
+ if (_babel_types.isIdentifier(n)) {
83
+ if (!parent || _babel_types.isReferenced(n, parent, grandparent)) ids.add(n.name);
84
+ return;
85
+ }
86
+ if (_babel_types.isJSXIdentifier(n)) {
87
+ if (parent && _babel_types.isJSXAttribute(parent) && parentKey === "name") return;
88
+ if (parent && _babel_types.isJSXMemberExpression(parent) && parentKey === "property") return;
89
+ const first = n.name[0];
90
+ if (first && first === first.toLowerCase()) return;
91
+ ids.add(n.name);
92
+ return;
93
+ }
94
+ for (const key of _babel_types.VISITOR_KEYS[n.type] || []) {
95
+ const child = n[key];
96
+ if (Array.isArray(child)) {
97
+ for (const c of child) if (c && typeof c.type === "string") walk(c, n, parent, key);
98
+ } else if (child && typeof child.type === "string") walk(child, n, parent, key);
99
+ }
100
+ })(node);
101
+ return ids;
159
102
  }
103
+ /**
104
+ * Build a map from binding name → declaration AST node for all
105
+ * locally-declared module-level bindings. Built once, O(1) lookup.
106
+ */
160
107
  function buildDeclarationMap(ast) {
161
- const map = /* @__PURE__ */ new Map();
162
- for (const stmt of ast.program.body) {
163
- const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
164
- if (t__namespace.isVariableDeclaration(decl)) {
165
- for (const declarator of decl.declarations) {
166
- for (const name of collectIdentifiersFromPattern(declarator.id)) {
167
- map.set(name, declarator);
168
- }
169
- }
170
- } else if (t__namespace.isFunctionDeclaration(decl) && decl.id) {
171
- map.set(decl.id.name, decl);
172
- } else if (t__namespace.isClassDeclaration(decl) && decl.id) {
173
- map.set(decl.id.name, decl);
174
- }
175
- }
176
- return map;
108
+ const map = /* @__PURE__ */ new Map();
109
+ for (const stmt of ast.program.body) {
110
+ const decl = _babel_types.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
111
+ if (_babel_types.isVariableDeclaration(decl)) for (const declarator of decl.declarations) for (const name of collectIdentifiersFromPattern(declarator.id)) map.set(name, declarator);
112
+ else if (_babel_types.isFunctionDeclaration(decl) && decl.id) map.set(decl.id.name, decl);
113
+ else if (_babel_types.isClassDeclaration(decl) && decl.id) map.set(decl.id.name, decl);
114
+ }
115
+ return map;
177
116
  }
117
+ /**
118
+ * Build a dependency graph: for each local binding, the set of other local
119
+ * bindings its declaration references. Built once via simple node walking.
120
+ */
178
121
  function buildDependencyGraph(declMap, localBindings) {
179
- const graph = /* @__PURE__ */ new Map();
180
- for (const [name, declNode] of declMap) {
181
- if (!localBindings.has(name)) continue;
182
- const allIds = collectIdentifiersFromNode(declNode);
183
- const deps = /* @__PURE__ */ new Set();
184
- for (const id of allIds) {
185
- if (id !== name && localBindings.has(id)) deps.add(id);
186
- }
187
- graph.set(name, deps);
188
- }
189
- return graph;
122
+ const graph = /* @__PURE__ */ new Map();
123
+ for (const [name, declNode] of declMap) {
124
+ if (!localBindings.has(name)) continue;
125
+ const allIds = collectIdentifiersFromNode(declNode);
126
+ const deps = /* @__PURE__ */ new Set();
127
+ for (const id of allIds) if (id !== name && localBindings.has(id)) deps.add(id);
128
+ graph.set(name, deps);
129
+ }
130
+ return graph;
190
131
  }
132
+ /**
133
+ * Computes module-level bindings that are shared between split and non-split
134
+ * route properties. These bindings need to be extracted into a shared virtual
135
+ * module to avoid double-initialization.
136
+ *
137
+ * A binding is "shared" if it is referenced by at least one split property
138
+ * AND at least one non-split property. Only locally-declared module-level
139
+ * bindings are candidates (not imports — bundlers dedupe those).
140
+ */
191
141
  function computeSharedBindings(opts) {
192
- const ast = routerUtils.parseAst(opts);
193
- const localModuleLevelBindings = /* @__PURE__ */ new Set();
194
- for (const node of ast.program.body) {
195
- collectLocalBindingsFromStatement(node, localModuleLevelBindings);
196
- }
197
- localModuleLevelBindings.delete("Route");
198
- if (localModuleLevelBindings.size === 0) {
199
- return /* @__PURE__ */ new Set();
200
- }
201
- function findIndexForSplitNode(str) {
202
- return opts.codeSplitGroupings.findIndex(
203
- (group) => group.includes(str)
204
- );
205
- }
206
- let routeOptions;
207
- babel.traverse(ast, {
208
- CallExpression(path) {
209
- if (!t__namespace.isIdentifier(path.node.callee)) return;
210
- if (!splittableCreateRouteFns.includes(path.node.callee.name)) return;
211
- if (t__namespace.isCallExpression(path.parentPath.node)) {
212
- const opts2 = resolveIdentifier(path, path.parentPath.node.arguments[0]);
213
- if (t__namespace.isObjectExpression(opts2)) routeOptions = opts2;
214
- } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
215
- const caller = resolveIdentifier(path, path.parentPath.node.init);
216
- if (t__namespace.isCallExpression(caller)) {
217
- const opts2 = resolveIdentifier(path, caller.arguments[0]);
218
- if (t__namespace.isObjectExpression(opts2)) routeOptions = opts2;
219
- }
220
- }
221
- }
222
- });
223
- if (!routeOptions) return /* @__PURE__ */ new Set();
224
- const splitGroupsPresent = /* @__PURE__ */ new Set();
225
- let hasNonSplit = false;
226
- for (const prop of routeOptions.properties) {
227
- if (!t__namespace.isObjectProperty(prop) || !t__namespace.isIdentifier(prop.key)) continue;
228
- if (prop.key.name === "codeSplitGroupings") continue;
229
- if (t__namespace.isIdentifier(prop.value) && prop.value.name === "undefined") continue;
230
- const groupIndex = findIndexForSplitNode(prop.key.name);
231
- if (groupIndex === -1) {
232
- hasNonSplit = true;
233
- } else {
234
- splitGroupsPresent.add(groupIndex);
235
- }
236
- }
237
- if (!hasNonSplit && splitGroupsPresent.size < 2) return /* @__PURE__ */ new Set();
238
- const declMap = buildDeclarationMap(ast);
239
- const depGraph = buildDependencyGraph(declMap, localModuleLevelBindings);
240
- const allLocalBindings = new Set(localModuleLevelBindings);
241
- allLocalBindings.add("Route");
242
- const fullDepGraph = buildDependencyGraph(declMap, allLocalBindings);
243
- const refsByGroup = /* @__PURE__ */ new Map();
244
- for (const prop of routeOptions.properties) {
245
- if (!t__namespace.isObjectProperty(prop) || !t__namespace.isIdentifier(prop.key)) continue;
246
- const key = prop.key.name;
247
- if (key === "codeSplitGroupings") continue;
248
- const groupIndex = findIndexForSplitNode(key);
249
- const directRefs = collectModuleLevelRefsFromNode(
250
- prop.value,
251
- localModuleLevelBindings
252
- );
253
- const allRefs = new Set(directRefs);
254
- expandTransitively(allRefs, depGraph);
255
- for (const ref of allRefs) {
256
- let groups = refsByGroup.get(ref);
257
- if (!groups) {
258
- groups = /* @__PURE__ */ new Set();
259
- refsByGroup.set(ref, groups);
260
- }
261
- groups.add(groupIndex);
262
- }
263
- }
264
- const shared = /* @__PURE__ */ new Set();
265
- for (const [name, groups] of refsByGroup) {
266
- if (groups.size >= 2) shared.add(name);
267
- }
268
- expandSharedDestructuredDeclarators(ast, refsByGroup, shared);
269
- if (shared.size === 0) return shared;
270
- expandDestructuredDeclarations(ast, shared);
271
- removeBindingsDependingOnRoute(shared, fullDepGraph);
272
- return shared;
142
+ const ast = (0, _tanstack_router_utils.parseAst)(opts);
143
+ const localModuleLevelBindings = /* @__PURE__ */ new Set();
144
+ for (const node of ast.program.body) collectLocalBindingsFromStatement(node, localModuleLevelBindings);
145
+ localModuleLevelBindings.delete("Route");
146
+ if (localModuleLevelBindings.size === 0) return /* @__PURE__ */ new Set();
147
+ function findIndexForSplitNode(str) {
148
+ return opts.codeSplitGroupings.findIndex((group) => group.includes(str));
149
+ }
150
+ let routeOptions;
151
+ _babel_core.traverse(ast, { CallExpression(path) {
152
+ if (!_babel_types.isIdentifier(path.node.callee)) return;
153
+ if (!splittableCreateRouteFns.includes(path.node.callee.name)) return;
154
+ if (_babel_types.isCallExpression(path.parentPath.node)) {
155
+ const opts = resolveIdentifier(path, path.parentPath.node.arguments[0]);
156
+ if (_babel_types.isObjectExpression(opts)) routeOptions = opts;
157
+ } else if (_babel_types.isVariableDeclarator(path.parentPath.node)) {
158
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
159
+ if (_babel_types.isCallExpression(caller)) {
160
+ const opts = resolveIdentifier(path, caller.arguments[0]);
161
+ if (_babel_types.isObjectExpression(opts)) routeOptions = opts;
162
+ }
163
+ }
164
+ } });
165
+ if (!routeOptions) return /* @__PURE__ */ new Set();
166
+ const splitGroupsPresent = /* @__PURE__ */ new Set();
167
+ let hasNonSplit = false;
168
+ for (const prop of routeOptions.properties) {
169
+ if (!_babel_types.isObjectProperty(prop) || !_babel_types.isIdentifier(prop.key)) continue;
170
+ if (prop.key.name === "codeSplitGroupings") continue;
171
+ if (_babel_types.isIdentifier(prop.value) && prop.value.name === "undefined") continue;
172
+ const groupIndex = findIndexForSplitNode(prop.key.name);
173
+ if (groupIndex === -1) hasNonSplit = true;
174
+ else splitGroupsPresent.add(groupIndex);
175
+ }
176
+ if (!hasNonSplit && splitGroupsPresent.size < 2) return /* @__PURE__ */ new Set();
177
+ const declMap = buildDeclarationMap(ast);
178
+ const depGraph = buildDependencyGraph(declMap, localModuleLevelBindings);
179
+ const allLocalBindings = new Set(localModuleLevelBindings);
180
+ allLocalBindings.add("Route");
181
+ const fullDepGraph = buildDependencyGraph(declMap, allLocalBindings);
182
+ const refsByGroup = /* @__PURE__ */ new Map();
183
+ for (const prop of routeOptions.properties) {
184
+ if (!_babel_types.isObjectProperty(prop) || !_babel_types.isIdentifier(prop.key)) continue;
185
+ const key = prop.key.name;
186
+ if (key === "codeSplitGroupings") continue;
187
+ const groupIndex = findIndexForSplitNode(key);
188
+ const directRefs = collectModuleLevelRefsFromNode(prop.value, localModuleLevelBindings);
189
+ const allRefs = new Set(directRefs);
190
+ expandTransitively(allRefs, depGraph);
191
+ for (const ref of allRefs) {
192
+ let groups = refsByGroup.get(ref);
193
+ if (!groups) {
194
+ groups = /* @__PURE__ */ new Set();
195
+ refsByGroup.set(ref, groups);
196
+ }
197
+ groups.add(groupIndex);
198
+ }
199
+ }
200
+ const shared = /* @__PURE__ */ new Set();
201
+ for (const [name, groups] of refsByGroup) if (groups.size >= 2) shared.add(name);
202
+ expandSharedDestructuredDeclarators(ast, refsByGroup, shared);
203
+ if (shared.size === 0) return shared;
204
+ expandDestructuredDeclarations(ast, shared);
205
+ removeBindingsDependingOnRoute(shared, fullDepGraph);
206
+ return shared;
273
207
  }
208
+ /**
209
+ * If bindings from the same destructured declarator are referenced by
210
+ * different groups, mark all bindings from that declarator as shared.
211
+ */
274
212
  function expandSharedDestructuredDeclarators(ast, refsByGroup, shared) {
275
- for (const stmt of ast.program.body) {
276
- const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
277
- if (!t__namespace.isVariableDeclaration(decl)) continue;
278
- for (const declarator of decl.declarations) {
279
- if (!t__namespace.isObjectPattern(declarator.id) && !t__namespace.isArrayPattern(declarator.id))
280
- continue;
281
- const names = collectIdentifiersFromPattern(declarator.id);
282
- const usedGroups = /* @__PURE__ */ new Set();
283
- for (const name of names) {
284
- const groups = refsByGroup.get(name);
285
- if (!groups) continue;
286
- for (const g of groups) usedGroups.add(g);
287
- }
288
- if (usedGroups.size >= 2) {
289
- for (const name of names) {
290
- shared.add(name);
291
- }
292
- }
293
- }
294
- }
213
+ for (const stmt of ast.program.body) {
214
+ const decl = _babel_types.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
215
+ if (!_babel_types.isVariableDeclaration(decl)) continue;
216
+ for (const declarator of decl.declarations) {
217
+ if (!_babel_types.isObjectPattern(declarator.id) && !_babel_types.isArrayPattern(declarator.id)) continue;
218
+ const names = collectIdentifiersFromPattern(declarator.id);
219
+ const usedGroups = /* @__PURE__ */ new Set();
220
+ for (const name of names) {
221
+ const groups = refsByGroup.get(name);
222
+ if (!groups) continue;
223
+ for (const g of groups) usedGroups.add(g);
224
+ }
225
+ if (usedGroups.size >= 2) for (const name of names) shared.add(name);
226
+ }
227
+ }
295
228
  }
229
+ /**
230
+ * Collect locally-declared module-level binding names from a statement.
231
+ * Pure node inspection, no traversal.
232
+ */
296
233
  function collectLocalBindingsFromStatement(node, bindings) {
297
- const decl = t__namespace.isExportNamedDeclaration(node) && node.declaration ? node.declaration : node;
298
- if (t__namespace.isVariableDeclaration(decl)) {
299
- for (const declarator of decl.declarations) {
300
- for (const name of collectIdentifiersFromPattern(declarator.id)) {
301
- bindings.add(name);
302
- }
303
- }
304
- } else if (t__namespace.isFunctionDeclaration(decl) && decl.id) {
305
- bindings.add(decl.id.name);
306
- } else if (t__namespace.isClassDeclaration(decl) && decl.id) {
307
- bindings.add(decl.id.name);
308
- }
234
+ const decl = _babel_types.isExportNamedDeclaration(node) && node.declaration ? node.declaration : node;
235
+ if (_babel_types.isVariableDeclaration(decl)) for (const declarator of decl.declarations) for (const name of collectIdentifiersFromPattern(declarator.id)) bindings.add(name);
236
+ else if (_babel_types.isFunctionDeclaration(decl) && decl.id) bindings.add(decl.id.name);
237
+ else if (_babel_types.isClassDeclaration(decl) && decl.id) bindings.add(decl.id.name);
309
238
  }
239
+ /**
240
+ * Collect direct module-level binding names referenced from a given AST node.
241
+ * Uses a simple recursive walk instead of babel.traverse.
242
+ */
310
243
  function collectModuleLevelRefsFromNode(node, localModuleLevelBindings) {
311
- const allIds = collectIdentifiersFromNode(node);
312
- const refs = /* @__PURE__ */ new Set();
313
- for (const name of allIds) {
314
- if (localModuleLevelBindings.has(name)) refs.add(name);
315
- }
316
- return refs;
244
+ const allIds = collectIdentifiersFromNode(node);
245
+ const refs = /* @__PURE__ */ new Set();
246
+ for (const name of allIds) if (localModuleLevelBindings.has(name)) refs.add(name);
247
+ return refs;
317
248
  }
249
+ /**
250
+ * Expand the shared set transitively using a prebuilt dependency graph.
251
+ * No AST traversals — pure graph BFS.
252
+ */
318
253
  function expandTransitively(shared, depGraph) {
319
- const queue = [...shared];
320
- const visited = /* @__PURE__ */ new Set();
321
- while (queue.length > 0) {
322
- const name = queue.pop();
323
- if (visited.has(name)) continue;
324
- visited.add(name);
325
- const deps = depGraph.get(name);
326
- if (!deps) continue;
327
- for (const dep of deps) {
328
- if (!shared.has(dep)) {
329
- shared.add(dep);
330
- queue.push(dep);
331
- }
332
- }
333
- }
254
+ const queue = [...shared];
255
+ const visited = /* @__PURE__ */ new Set();
256
+ while (queue.length > 0) {
257
+ const name = queue.pop();
258
+ if (visited.has(name)) continue;
259
+ visited.add(name);
260
+ const deps = depGraph.get(name);
261
+ if (!deps) continue;
262
+ for (const dep of deps) if (!shared.has(dep)) {
263
+ shared.add(dep);
264
+ queue.push(dep);
265
+ }
266
+ }
334
267
  }
268
+ /**
269
+ * Remove any bindings from `shared` that transitively depend on `Route`.
270
+ * The Route singleton must remain in the reference file; if a shared binding
271
+ * references it (directly or transitively), extracting that binding would
272
+ * duplicate Route in the shared module.
273
+ *
274
+ * Uses `depGraph` which must include `Route` as a node so the dependency
275
+ * chain is visible.
276
+ */
335
277
  function removeBindingsDependingOnRoute(shared, depGraph) {
336
- const reverseGraph = /* @__PURE__ */ new Map();
337
- for (const [name, deps] of depGraph) {
338
- for (const dep of deps) {
339
- let parents = reverseGraph.get(dep);
340
- if (!parents) {
341
- parents = /* @__PURE__ */ new Set();
342
- reverseGraph.set(dep, parents);
343
- }
344
- parents.add(name);
345
- }
346
- }
347
- const visited = /* @__PURE__ */ new Set();
348
- const queue = ["Route"];
349
- while (queue.length > 0) {
350
- const cur = queue.pop();
351
- if (visited.has(cur)) continue;
352
- visited.add(cur);
353
- const parents = reverseGraph.get(cur);
354
- if (!parents) continue;
355
- for (const parent of parents) {
356
- if (!visited.has(parent)) queue.push(parent);
357
- }
358
- }
359
- for (const name of [...shared]) {
360
- if (visited.has(name)) {
361
- shared.delete(name);
362
- }
363
- }
278
+ const reverseGraph = /* @__PURE__ */ new Map();
279
+ for (const [name, deps] of depGraph) for (const dep of deps) {
280
+ let parents = reverseGraph.get(dep);
281
+ if (!parents) {
282
+ parents = /* @__PURE__ */ new Set();
283
+ reverseGraph.set(dep, parents);
284
+ }
285
+ parents.add(name);
286
+ }
287
+ const visited = /* @__PURE__ */ new Set();
288
+ const queue = ["Route"];
289
+ while (queue.length > 0) {
290
+ const cur = queue.pop();
291
+ if (visited.has(cur)) continue;
292
+ visited.add(cur);
293
+ const parents = reverseGraph.get(cur);
294
+ if (!parents) continue;
295
+ for (const parent of parents) if (!visited.has(parent)) queue.push(parent);
296
+ }
297
+ for (const name of [...shared]) if (visited.has(name)) shared.delete(name);
364
298
  }
299
+ /**
300
+ * If any binding from a destructured declaration is shared,
301
+ * ensure all bindings from that same declaration are also shared.
302
+ * Pure node inspection of program.body, no traversal.
303
+ */
365
304
  function expandDestructuredDeclarations(ast, shared) {
366
- for (const stmt of ast.program.body) {
367
- const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
368
- if (!t__namespace.isVariableDeclaration(decl)) continue;
369
- for (const declarator of decl.declarations) {
370
- if (!t__namespace.isObjectPattern(declarator.id) && !t__namespace.isArrayPattern(declarator.id))
371
- continue;
372
- const names = collectIdentifiersFromPattern(declarator.id);
373
- const hasShared = names.some((n) => shared.has(n));
374
- if (hasShared) {
375
- for (const n of names) {
376
- shared.add(n);
377
- }
378
- }
379
- }
380
- }
305
+ for (const stmt of ast.program.body) {
306
+ const decl = _babel_types.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
307
+ if (!_babel_types.isVariableDeclaration(decl)) continue;
308
+ for (const declarator of decl.declarations) {
309
+ if (!_babel_types.isObjectPattern(declarator.id) && !_babel_types.isArrayPattern(declarator.id)) continue;
310
+ const names = collectIdentifiersFromPattern(declarator.id);
311
+ if (names.some((n) => shared.has(n))) for (const n of names) shared.add(n);
312
+ }
313
+ }
381
314
  }
315
+ /**
316
+ * Find which shared bindings are user-exported in the original source.
317
+ * These need to be re-exported from the shared module.
318
+ */
382
319
  function findExportedSharedBindings(ast, sharedBindings) {
383
- const exported = /* @__PURE__ */ new Set();
384
- for (const stmt of ast.program.body) {
385
- if (!t__namespace.isExportNamedDeclaration(stmt) || !stmt.declaration) continue;
386
- if (t__namespace.isVariableDeclaration(stmt.declaration)) {
387
- for (const decl of stmt.declaration.declarations) {
388
- for (const name of collectIdentifiersFromPattern(decl.id)) {
389
- if (sharedBindings.has(name)) exported.add(name);
390
- }
391
- }
392
- } else if (t__namespace.isFunctionDeclaration(stmt.declaration) && stmt.declaration.id) {
393
- if (sharedBindings.has(stmt.declaration.id.name))
394
- exported.add(stmt.declaration.id.name);
395
- } else if (t__namespace.isClassDeclaration(stmt.declaration) && stmt.declaration.id) {
396
- if (sharedBindings.has(stmt.declaration.id.name))
397
- exported.add(stmt.declaration.id.name);
398
- }
399
- }
400
- return exported;
320
+ const exported = /* @__PURE__ */ new Set();
321
+ for (const stmt of ast.program.body) {
322
+ if (!_babel_types.isExportNamedDeclaration(stmt) || !stmt.declaration) continue;
323
+ if (_babel_types.isVariableDeclaration(stmt.declaration)) {
324
+ for (const decl of stmt.declaration.declarations) for (const name of collectIdentifiersFromPattern(decl.id)) if (sharedBindings.has(name)) exported.add(name);
325
+ } else if (_babel_types.isFunctionDeclaration(stmt.declaration) && stmt.declaration.id) {
326
+ if (sharedBindings.has(stmt.declaration.id.name)) exported.add(stmt.declaration.id.name);
327
+ } else if (_babel_types.isClassDeclaration(stmt.declaration) && stmt.declaration.id) {
328
+ if (sharedBindings.has(stmt.declaration.id.name)) exported.add(stmt.declaration.id.name);
329
+ }
330
+ }
331
+ return exported;
401
332
  }
333
+ /**
334
+ * Remove declarations of shared bindings from the AST.
335
+ * Handles both plain and exported declarations, including destructured patterns.
336
+ * Removes the entire statement if all bindings in it are shared.
337
+ */
402
338
  function removeSharedDeclarations(ast, sharedBindings) {
403
- ast.program.body = ast.program.body.filter((stmt) => {
404
- const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
405
- if (t__namespace.isVariableDeclaration(decl)) {
406
- decl.declarations = decl.declarations.filter((declarator) => {
407
- const names = collectIdentifiersFromPattern(declarator.id);
408
- return !names.every((n) => sharedBindings.has(n));
409
- });
410
- if (decl.declarations.length === 0) return false;
411
- } else if (t__namespace.isFunctionDeclaration(decl) && decl.id) {
412
- if (sharedBindings.has(decl.id.name)) return false;
413
- } else if (t__namespace.isClassDeclaration(decl) && decl.id) {
414
- if (sharedBindings.has(decl.id.name)) return false;
415
- }
416
- return true;
417
- });
339
+ ast.program.body = ast.program.body.filter((stmt) => {
340
+ const decl = _babel_types.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
341
+ if (_babel_types.isVariableDeclaration(decl)) {
342
+ decl.declarations = decl.declarations.filter((declarator) => {
343
+ return !collectIdentifiersFromPattern(declarator.id).every((n) => sharedBindings.has(n));
344
+ });
345
+ if (decl.declarations.length === 0) return false;
346
+ } else if (_babel_types.isFunctionDeclaration(decl) && decl.id) {
347
+ if (sharedBindings.has(decl.id.name)) return false;
348
+ } else if (_babel_types.isClassDeclaration(decl) && decl.id) {
349
+ if (sharedBindings.has(decl.id.name)) return false;
350
+ }
351
+ return true;
352
+ });
418
353
  }
419
354
  function compileCodeSplitReferenceRoute(opts) {
420
- const ast = routerUtils.parseAst(opts);
421
- const refIdents = routerUtils.findReferencedIdentifiers(ast);
422
- const knownExportedIdents = /* @__PURE__ */ new Set();
423
- function findIndexForSplitNode(str) {
424
- return opts.codeSplitGroupings.findIndex(
425
- (group) => group.includes(str)
426
- );
427
- }
428
- const frameworkOptions$1 = frameworkOptions.getFrameworkOptions(opts.targetFramework);
429
- const PACKAGE = frameworkOptions$1.package;
430
- const LAZY_ROUTE_COMPONENT_IDENT = frameworkOptions$1.idents.lazyRouteComponent;
431
- const LAZY_FN_IDENT = frameworkOptions$1.idents.lazyFn;
432
- let createRouteFn;
433
- let modified = false;
434
- let hmrAdded = false;
435
- let sharedExportedNames;
436
- babel.traverse(ast, {
437
- Program: {
438
- enter(programPath) {
439
- const removableImportPaths = /* @__PURE__ */ new Set([]);
440
- programPath.traverse({
441
- CallExpression: (path) => {
442
- if (!t__namespace.isIdentifier(path.node.callee)) {
443
- return;
444
- }
445
- if (!allCreateRouteFns.includes(path.node.callee.name)) {
446
- return;
447
- }
448
- createRouteFn = path.node.callee.name;
449
- function babelHandleReference(routeOptions) {
450
- const hasImportedOrDefinedIdentifier = (name) => {
451
- return programPath.scope.hasBinding(name);
452
- };
453
- if (t__namespace.isObjectExpression(routeOptions)) {
454
- if (opts.deleteNodes && opts.deleteNodes.size > 0) {
455
- routeOptions.properties = routeOptions.properties.filter(
456
- (prop) => {
457
- if (t__namespace.isObjectProperty(prop)) {
458
- if (t__namespace.isIdentifier(prop.key)) {
459
- if (opts.deleteNodes.has(prop.key.name)) {
460
- modified = true;
461
- return false;
462
- }
463
- }
464
- }
465
- return true;
466
- }
467
- );
468
- }
469
- if (!splittableCreateRouteFns.includes(createRouteFn)) {
470
- const insertionPath = path.getStatementParent() ?? path;
471
- opts.compilerPlugins?.forEach((plugin) => {
472
- const pluginResult = plugin.onUnsplittableRoute?.({
473
- programPath,
474
- callExpressionPath: path,
475
- insertionPath,
476
- routeOptions,
477
- createRouteFn,
478
- opts
479
- });
480
- if (pluginResult?.modified) {
481
- modified = true;
482
- }
483
- });
484
- if (opts.addHmr && !hmrAdded) {
485
- programPath.pushContainer("body", routeHmrStatement.routeHmrStatement);
486
- modified = true;
487
- hmrAdded = true;
488
- }
489
- return programPath.stop();
490
- }
491
- routeOptions.properties.forEach((prop) => {
492
- if (t__namespace.isObjectProperty(prop)) {
493
- if (t__namespace.isIdentifier(prop.key)) {
494
- const key = prop.key.name;
495
- const codeSplitGroupingByKey = findIndexForSplitNode(key);
496
- if (codeSplitGroupingByKey === -1) {
497
- return;
498
- }
499
- const codeSplitGroup = [
500
- ...new Set(
501
- opts.codeSplitGroupings[codeSplitGroupingByKey]
502
- )
503
- ];
504
- const isNodeConfigAvailable = SPLIT_NODES_CONFIG.has(
505
- key
506
- );
507
- if (!isNodeConfigAvailable) {
508
- return;
509
- }
510
- if (t__namespace.isBooleanLiteral(prop.value) || t__namespace.isNullLiteral(prop.value) || t__namespace.isIdentifier(prop.value) && prop.value.name === "undefined") {
511
- return;
512
- }
513
- const splitNodeMeta = SPLIT_NODES_CONFIG.get(key);
514
- const splitUrl = addSplitSearchParamToFilename(
515
- opts.filename,
516
- codeSplitGroup
517
- );
518
- if (splitNodeMeta.splitStrategy === "lazyRouteComponent") {
519
- const value = prop.value;
520
- let shouldSplit = true;
521
- if (t__namespace.isIdentifier(value)) {
522
- const existingImportPath = getImportSpecifierAndPathFromLocalName(
523
- programPath,
524
- value.name
525
- ).path;
526
- if (existingImportPath) {
527
- removableImportPaths.add(existingImportPath);
528
- }
529
- const isExported = hasExport(ast, value);
530
- if (isExported) {
531
- knownExportedIdents.add(value.name);
532
- }
533
- shouldSplit = !isExported;
534
- if (shouldSplit) {
535
- removeIdentifierLiteral(path, value);
536
- }
537
- }
538
- if (!shouldSplit) {
539
- return;
540
- }
541
- modified = true;
542
- if (!hasImportedOrDefinedIdentifier(
543
- LAZY_ROUTE_COMPONENT_IDENT
544
- )) {
545
- programPath.unshiftContainer("body", [
546
- template__namespace.statement(
547
- `import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`
548
- )()
549
- ]);
550
- }
551
- if (!hasImportedOrDefinedIdentifier(
552
- splitNodeMeta.localImporterIdent
553
- )) {
554
- programPath.unshiftContainer("body", [
555
- template__namespace.statement(
556
- `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
557
- )()
558
- ]);
559
- }
560
- prop.value = template__namespace.expression(
561
- `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
562
- )();
563
- if (opts.addHmr && !hmrAdded) {
564
- programPath.pushContainer("body", routeHmrStatement.routeHmrStatement);
565
- modified = true;
566
- hmrAdded = true;
567
- }
568
- } else {
569
- const value = prop.value;
570
- let shouldSplit = true;
571
- if (t__namespace.isIdentifier(value)) {
572
- const existingImportPath = getImportSpecifierAndPathFromLocalName(
573
- programPath,
574
- value.name
575
- ).path;
576
- if (existingImportPath) {
577
- removableImportPaths.add(existingImportPath);
578
- }
579
- const isExported = hasExport(ast, value);
580
- if (isExported) {
581
- knownExportedIdents.add(value.name);
582
- }
583
- shouldSplit = !isExported;
584
- if (shouldSplit) {
585
- removeIdentifierLiteral(path, value);
586
- }
587
- }
588
- if (!shouldSplit) {
589
- return;
590
- }
591
- modified = true;
592
- if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
593
- programPath.unshiftContainer(
594
- "body",
595
- template__namespace.smart(
596
- `import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`
597
- )()
598
- );
599
- }
600
- if (!hasImportedOrDefinedIdentifier(
601
- splitNodeMeta.localImporterIdent
602
- )) {
603
- programPath.unshiftContainer("body", [
604
- template__namespace.statement(
605
- `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
606
- )()
607
- ]);
608
- }
609
- prop.value = template__namespace.expression(
610
- `${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
611
- )();
612
- }
613
- }
614
- }
615
- programPath.scope.crawl();
616
- });
617
- }
618
- }
619
- if (t__namespace.isCallExpression(path.parentPath.node)) {
620
- const options = resolveIdentifier(
621
- path,
622
- path.parentPath.node.arguments[0]
623
- );
624
- babelHandleReference(options);
625
- } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
626
- const caller = resolveIdentifier(path, path.parentPath.node.init);
627
- if (t__namespace.isCallExpression(caller)) {
628
- const options = resolveIdentifier(path, caller.arguments[0]);
629
- babelHandleReference(options);
630
- }
631
- }
632
- }
633
- });
634
- if (removableImportPaths.size > 0) {
635
- modified = true;
636
- programPath.traverse({
637
- ImportDeclaration(path) {
638
- if (path.node.specifiers.length > 0) return;
639
- if (removableImportPaths.has(path.node.source.value)) {
640
- path.remove();
641
- }
642
- }
643
- });
644
- }
645
- if (opts.sharedBindings && opts.sharedBindings.size > 0) {
646
- sharedExportedNames = findExportedSharedBindings(
647
- ast,
648
- opts.sharedBindings
649
- );
650
- removeSharedDeclarations(ast, opts.sharedBindings);
651
- const sharedModuleUrl = addSharedSearchParamToFilename(opts.filename);
652
- const sharedImportSpecifiers = [...opts.sharedBindings].map(
653
- (name) => t__namespace.importSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
654
- );
655
- const [sharedImportPath] = programPath.unshiftContainer(
656
- "body",
657
- t__namespace.importDeclaration(
658
- sharedImportSpecifiers,
659
- t__namespace.stringLiteral(sharedModuleUrl)
660
- )
661
- );
662
- sharedImportPath.traverse({
663
- Identifier(identPath) {
664
- if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") {
665
- refIdents.add(identPath);
666
- }
667
- }
668
- });
669
- if (sharedExportedNames.size > 0) {
670
- const reExportSpecifiers = [...sharedExportedNames].map(
671
- (name) => t__namespace.exportSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
672
- );
673
- programPath.pushContainer(
674
- "body",
675
- t__namespace.exportNamedDeclaration(
676
- null,
677
- reExportSpecifiers,
678
- t__namespace.stringLiteral(sharedModuleUrl)
679
- )
680
- );
681
- }
682
- }
683
- }
684
- }
685
- });
686
- if (!modified) {
687
- return null;
688
- }
689
- routerUtils.deadCodeElimination(ast, refIdents);
690
- if (knownExportedIdents.size > 0) {
691
- const warningMessage = createNotExportableMessage(
692
- opts.filename,
693
- knownExportedIdents
694
- );
695
- console.warn(warningMessage);
696
- if (process.env.NODE_ENV !== "production") {
697
- const warningTemplate = template__namespace.statement(
698
- `console.warn(${JSON.stringify(warningMessage)})`
699
- )();
700
- ast.program.body.unshift(warningTemplate);
701
- }
702
- }
703
- const result = routerUtils.generateFromAst(ast, {
704
- sourceMaps: true,
705
- sourceFileName: opts.filename,
706
- filename: opts.filename
707
- });
708
- if (result.map) {
709
- result.map.sourcesContent = [opts.code];
710
- }
711
- return result;
355
+ const ast = (0, _tanstack_router_utils.parseAst)(opts);
356
+ const refIdents = (0, _tanstack_router_utils.findReferencedIdentifiers)(ast);
357
+ const knownExportedIdents = /* @__PURE__ */ new Set();
358
+ function findIndexForSplitNode(str) {
359
+ return opts.codeSplitGroupings.findIndex((group) => group.includes(str));
360
+ }
361
+ const frameworkOptions = require_framework_options.getFrameworkOptions(opts.targetFramework);
362
+ const PACKAGE = frameworkOptions.package;
363
+ const LAZY_ROUTE_COMPONENT_IDENT = frameworkOptions.idents.lazyRouteComponent;
364
+ const LAZY_FN_IDENT = frameworkOptions.idents.lazyFn;
365
+ let createRouteFn;
366
+ let modified = false;
367
+ let hmrAdded = false;
368
+ let sharedExportedNames;
369
+ _babel_core.traverse(ast, { Program: { enter(programPath) {
370
+ /**
371
+ * If the component for the route is being imported from
372
+ * another file, this is to track the path to that file
373
+ * the path itself doesn't matter, we just need to keep
374
+ * track of it so that we can remove it from the imports
375
+ * list if it's not being used like:
376
+ *
377
+ * `import '../shared/imported'`
378
+ */
379
+ const removableImportPaths = /* @__PURE__ */ new Set([]);
380
+ programPath.traverse({ CallExpression: (path) => {
381
+ if (!_babel_types.isIdentifier(path.node.callee)) return;
382
+ if (!allCreateRouteFns.includes(path.node.callee.name)) return;
383
+ createRouteFn = path.node.callee.name;
384
+ function babelHandleReference(routeOptions) {
385
+ const hasImportedOrDefinedIdentifier = (name) => {
386
+ return programPath.scope.hasBinding(name);
387
+ };
388
+ if (_babel_types.isObjectExpression(routeOptions)) {
389
+ if (opts.deleteNodes && opts.deleteNodes.size > 0) routeOptions.properties = routeOptions.properties.filter((prop) => {
390
+ if (_babel_types.isObjectProperty(prop)) {
391
+ if (_babel_types.isIdentifier(prop.key)) {
392
+ if (opts.deleteNodes.has(prop.key.name)) {
393
+ modified = true;
394
+ return false;
395
+ }
396
+ }
397
+ }
398
+ return true;
399
+ });
400
+ if (!splittableCreateRouteFns.includes(createRouteFn)) {
401
+ const insertionPath = path.getStatementParent() ?? path;
402
+ opts.compilerPlugins?.forEach((plugin) => {
403
+ if ((plugin.onUnsplittableRoute?.({
404
+ programPath,
405
+ callExpressionPath: path,
406
+ insertionPath,
407
+ routeOptions,
408
+ createRouteFn,
409
+ opts
410
+ }))?.modified) modified = true;
411
+ });
412
+ if (opts.addHmr && !hmrAdded) {
413
+ programPath.pushContainer("body", require_route_hmr_statement.routeHmrStatement);
414
+ modified = true;
415
+ hmrAdded = true;
416
+ }
417
+ return programPath.stop();
418
+ }
419
+ routeOptions.properties.forEach((prop) => {
420
+ if (_babel_types.isObjectProperty(prop)) {
421
+ if (_babel_types.isIdentifier(prop.key)) {
422
+ const key = prop.key.name;
423
+ const codeSplitGroupingByKey = findIndexForSplitNode(key);
424
+ if (codeSplitGroupingByKey === -1) return;
425
+ const codeSplitGroup = [...new Set(opts.codeSplitGroupings[codeSplitGroupingByKey])];
426
+ if (!SPLIT_NODES_CONFIG.has(key)) return;
427
+ if (_babel_types.isBooleanLiteral(prop.value) || _babel_types.isNullLiteral(prop.value) || _babel_types.isIdentifier(prop.value) && prop.value.name === "undefined") return;
428
+ const splitNodeMeta = SPLIT_NODES_CONFIG.get(key);
429
+ const splitUrl = addSplitSearchParamToFilename(opts.filename, codeSplitGroup);
430
+ if (splitNodeMeta.splitStrategy === "lazyRouteComponent") {
431
+ const value = prop.value;
432
+ let shouldSplit = true;
433
+ if (_babel_types.isIdentifier(value)) {
434
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(programPath, value.name).path;
435
+ if (existingImportPath) removableImportPaths.add(existingImportPath);
436
+ const isExported = hasExport(ast, value);
437
+ if (isExported) knownExportedIdents.add(value.name);
438
+ shouldSplit = !isExported;
439
+ if (shouldSplit) removeIdentifierLiteral(path, value);
440
+ }
441
+ if (!shouldSplit) return;
442
+ modified = true;
443
+ if (!hasImportedOrDefinedIdentifier(LAZY_ROUTE_COMPONENT_IDENT)) programPath.unshiftContainer("body", [_babel_template.statement(`import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`)()]);
444
+ if (!hasImportedOrDefinedIdentifier(splitNodeMeta.localImporterIdent)) programPath.unshiftContainer("body", [_babel_template.statement(`const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`)()]);
445
+ prop.value = _babel_template.expression(`${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`)();
446
+ if (opts.addHmr && !hmrAdded) {
447
+ programPath.pushContainer("body", require_route_hmr_statement.routeHmrStatement);
448
+ modified = true;
449
+ hmrAdded = true;
450
+ }
451
+ } else {
452
+ const value = prop.value;
453
+ let shouldSplit = true;
454
+ if (_babel_types.isIdentifier(value)) {
455
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(programPath, value.name).path;
456
+ if (existingImportPath) removableImportPaths.add(existingImportPath);
457
+ const isExported = hasExport(ast, value);
458
+ if (isExported) knownExportedIdents.add(value.name);
459
+ shouldSplit = !isExported;
460
+ if (shouldSplit) removeIdentifierLiteral(path, value);
461
+ }
462
+ if (!shouldSplit) return;
463
+ modified = true;
464
+ if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) programPath.unshiftContainer("body", _babel_template.smart(`import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`)());
465
+ if (!hasImportedOrDefinedIdentifier(splitNodeMeta.localImporterIdent)) programPath.unshiftContainer("body", [_babel_template.statement(`const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`)()]);
466
+ prop.value = _babel_template.expression(`${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`)();
467
+ }
468
+ }
469
+ }
470
+ programPath.scope.crawl();
471
+ });
472
+ }
473
+ }
474
+ if (_babel_types.isCallExpression(path.parentPath.node)) babelHandleReference(resolveIdentifier(path, path.parentPath.node.arguments[0]));
475
+ else if (_babel_types.isVariableDeclarator(path.parentPath.node)) {
476
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
477
+ if (_babel_types.isCallExpression(caller)) babelHandleReference(resolveIdentifier(path, caller.arguments[0]));
478
+ }
479
+ } });
480
+ /**
481
+ * If the component for the route is being imported,
482
+ * and it's not being used, remove the import statement
483
+ * from the program, by checking that the import has no
484
+ * specifiers
485
+ */
486
+ if (removableImportPaths.size > 0) {
487
+ modified = true;
488
+ programPath.traverse({ ImportDeclaration(path) {
489
+ if (path.node.specifiers.length > 0) return;
490
+ if (removableImportPaths.has(path.node.source.value)) path.remove();
491
+ } });
492
+ }
493
+ if (opts.sharedBindings && opts.sharedBindings.size > 0) {
494
+ sharedExportedNames = findExportedSharedBindings(ast, opts.sharedBindings);
495
+ removeSharedDeclarations(ast, opts.sharedBindings);
496
+ const sharedModuleUrl = addSharedSearchParamToFilename(opts.filename);
497
+ const sharedImportSpecifiers = [...opts.sharedBindings].map((name) => _babel_types.importSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
498
+ const [sharedImportPath] = programPath.unshiftContainer("body", _babel_types.importDeclaration(sharedImportSpecifiers, _babel_types.stringLiteral(sharedModuleUrl)));
499
+ sharedImportPath.traverse({ Identifier(identPath) {
500
+ if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") refIdents.add(identPath);
501
+ } });
502
+ if (sharedExportedNames.size > 0) {
503
+ const reExportSpecifiers = [...sharedExportedNames].map((name) => _babel_types.exportSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
504
+ programPath.pushContainer("body", _babel_types.exportNamedDeclaration(null, reExportSpecifiers, _babel_types.stringLiteral(sharedModuleUrl)));
505
+ }
506
+ }
507
+ } } });
508
+ if (!modified) return null;
509
+ (0, _tanstack_router_utils.deadCodeElimination)(ast, refIdents);
510
+ if (knownExportedIdents.size > 0) {
511
+ const warningMessage = createNotExportableMessage(opts.filename, knownExportedIdents);
512
+ console.warn(warningMessage);
513
+ if (process.env.NODE_ENV !== "production") {
514
+ const warningTemplate = _babel_template.statement(`console.warn(${JSON.stringify(warningMessage)})`)();
515
+ ast.program.body.unshift(warningTemplate);
516
+ }
517
+ }
518
+ const result = (0, _tanstack_router_utils.generateFromAst)(ast, {
519
+ sourceMaps: true,
520
+ sourceFileName: opts.filename,
521
+ filename: opts.filename
522
+ });
523
+ if (result.map) result.map.sourcesContent = [opts.code];
524
+ return result;
712
525
  }
713
526
  function compileCodeSplitVirtualRoute(opts) {
714
- const ast = routerUtils.parseAst(opts);
715
- const refIdents = routerUtils.findReferencedIdentifiers(ast);
716
- if (opts.sharedBindings && opts.sharedBindings.size > 0) {
717
- removeSharedDeclarations(ast, opts.sharedBindings);
718
- }
719
- const intendedSplitNodes = new Set(opts.splitTargets);
720
- const knownExportedIdents = /* @__PURE__ */ new Set();
721
- babel.traverse(ast, {
722
- Program: {
723
- enter(programPath) {
724
- const trackedNodesToSplitByType = {
725
- component: void 0,
726
- loader: void 0,
727
- pendingComponent: void 0,
728
- errorComponent: void 0,
729
- notFoundComponent: void 0
730
- };
731
- programPath.traverse({
732
- CallExpression: (path) => {
733
- if (!t__namespace.isIdentifier(path.node.callee)) {
734
- return;
735
- }
736
- if (!splittableCreateRouteFns.includes(path.node.callee.name)) {
737
- return;
738
- }
739
- function babelHandleVirtual(options) {
740
- if (t__namespace.isObjectExpression(options)) {
741
- options.properties.forEach((prop) => {
742
- if (t__namespace.isObjectProperty(prop)) {
743
- KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
744
- if (!t__namespace.isIdentifier(prop.key) || prop.key.name !== splitType) {
745
- return;
746
- }
747
- const value = prop.value;
748
- if (t__namespace.isIdentifier(value) && value.name === "undefined") {
749
- return;
750
- }
751
- let isExported = false;
752
- if (t__namespace.isIdentifier(value)) {
753
- isExported = hasExport(ast, value);
754
- if (isExported) {
755
- knownExportedIdents.add(value.name);
756
- }
757
- }
758
- if (isExported && t__namespace.isIdentifier(value)) {
759
- removeExports(ast, value);
760
- } else {
761
- const meta = SPLIT_NODES_CONFIG.get(splitType);
762
- trackedNodesToSplitByType[splitType] = {
763
- node: prop.value,
764
- meta
765
- };
766
- }
767
- });
768
- }
769
- });
770
- options.properties = [];
771
- }
772
- }
773
- if (t__namespace.isCallExpression(path.parentPath.node)) {
774
- const options = resolveIdentifier(
775
- path,
776
- path.parentPath.node.arguments[0]
777
- );
778
- babelHandleVirtual(options);
779
- } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
780
- const caller = resolveIdentifier(path, path.parentPath.node.init);
781
- if (t__namespace.isCallExpression(caller)) {
782
- const options = resolveIdentifier(path, caller.arguments[0]);
783
- babelHandleVirtual(options);
784
- }
785
- }
786
- }
787
- });
788
- intendedSplitNodes.forEach((SPLIT_TYPE) => {
789
- const splitKey = trackedNodesToSplitByType[SPLIT_TYPE];
790
- if (!splitKey) {
791
- return;
792
- }
793
- let splitNode = splitKey.node;
794
- const splitMeta = { ...splitKey.meta, shouldRemoveNode: true };
795
- let originalIdentName;
796
- if (t__namespace.isIdentifier(splitNode)) {
797
- originalIdentName = splitNode.name;
798
- }
799
- while (t__namespace.isIdentifier(splitNode)) {
800
- const binding = programPath.scope.getBinding(splitNode.name);
801
- splitNode = binding?.path.node;
802
- }
803
- if (splitNode) {
804
- if (t__namespace.isFunctionDeclaration(splitNode)) {
805
- if (!splitNode.id) {
806
- throw new Error(
807
- `Function declaration for "${SPLIT_TYPE}" must have an identifier.`
808
- );
809
- }
810
- splitMeta.shouldRemoveNode = false;
811
- splitMeta.localExporterIdent = splitNode.id.name;
812
- } else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
813
- programPath.pushContainer(
814
- "body",
815
- t__namespace.variableDeclaration("const", [
816
- t__namespace.variableDeclarator(
817
- t__namespace.identifier(splitMeta.localExporterIdent),
818
- splitNode
819
- )
820
- ])
821
- );
822
- } else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
823
- programPath.pushContainer(
824
- "body",
825
- t__namespace.variableDeclaration("const", [
826
- t__namespace.variableDeclarator(
827
- t__namespace.identifier(splitMeta.localExporterIdent),
828
- splitNode.local
829
- )
830
- ])
831
- );
832
- } else if (t__namespace.isVariableDeclarator(splitNode)) {
833
- if (t__namespace.isIdentifier(splitNode.id)) {
834
- splitMeta.localExporterIdent = splitNode.id.name;
835
- splitMeta.shouldRemoveNode = false;
836
- } else if (t__namespace.isObjectPattern(splitNode.id)) {
837
- if (originalIdentName) {
838
- splitMeta.localExporterIdent = originalIdentName;
839
- }
840
- splitMeta.shouldRemoveNode = false;
841
- } else {
842
- throw new Error(
843
- `Unexpected splitNode type ☝️: ${splitNode.type}`
844
- );
845
- }
846
- } else if (t__namespace.isCallExpression(splitNode)) {
847
- const outputSplitNodeCode = routerUtils.generateFromAst(splitNode).code;
848
- const splitNodeAst = babel.parse(outputSplitNodeCode);
849
- if (!splitNodeAst) {
850
- throw new Error(
851
- `Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`
852
- );
853
- }
854
- const statement = splitNodeAst.program.body[0];
855
- if (!statement) {
856
- throw new Error(
857
- `Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}" as no statement was found in the program body`
858
- );
859
- }
860
- if (t__namespace.isExpressionStatement(statement)) {
861
- const expression = statement.expression;
862
- programPath.pushContainer(
863
- "body",
864
- t__namespace.variableDeclaration("const", [
865
- t__namespace.variableDeclarator(
866
- t__namespace.identifier(splitMeta.localExporterIdent),
867
- expression
868
- )
869
- ])
870
- );
871
- } else {
872
- throw new Error(
873
- `Unexpected expression type encounter for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`
874
- );
875
- }
876
- } else if (t__namespace.isConditionalExpression(splitNode)) {
877
- programPath.pushContainer(
878
- "body",
879
- t__namespace.variableDeclaration("const", [
880
- t__namespace.variableDeclarator(
881
- t__namespace.identifier(splitMeta.localExporterIdent),
882
- splitNode
883
- )
884
- ])
885
- );
886
- } else if (t__namespace.isTSAsExpression(splitNode)) {
887
- splitNode = splitNode.expression;
888
- programPath.pushContainer(
889
- "body",
890
- t__namespace.variableDeclaration("const", [
891
- t__namespace.variableDeclarator(
892
- t__namespace.identifier(splitMeta.localExporterIdent),
893
- splitNode
894
- )
895
- ])
896
- );
897
- } else if (t__namespace.isBooleanLiteral(splitNode)) {
898
- return;
899
- } else if (t__namespace.isNullLiteral(splitNode)) {
900
- return;
901
- } else {
902
- console.info("Unexpected splitNode type:", splitNode);
903
- throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
904
- }
905
- }
906
- if (splitMeta.shouldRemoveNode) {
907
- programPath.node.body = programPath.node.body.filter((node) => {
908
- return node !== splitNode;
909
- });
910
- }
911
- programPath.pushContainer("body", [
912
- t__namespace.exportNamedDeclaration(null, [
913
- t__namespace.exportSpecifier(
914
- t__namespace.identifier(splitMeta.localExporterIdent),
915
- // local variable name
916
- t__namespace.identifier(splitMeta.exporterIdent)
917
- // as what name it should be exported as
918
- )
919
- ])
920
- ]);
921
- });
922
- programPath.traverse({
923
- ExportNamedDeclaration(path) {
924
- if (path.node.declaration) {
925
- if (t__namespace.isVariableDeclaration(path.node.declaration)) {
926
- const specifiers = path.node.declaration.declarations.flatMap(
927
- (decl) => {
928
- if (t__namespace.isIdentifier(decl.id)) {
929
- return [
930
- t__namespace.importSpecifier(
931
- t__namespace.identifier(decl.id.name),
932
- t__namespace.identifier(decl.id.name)
933
- )
934
- ];
935
- }
936
- if (t__namespace.isObjectPattern(decl.id)) {
937
- return collectIdentifiersFromPattern(decl.id).map(
938
- (name) => t__namespace.importSpecifier(
939
- t__namespace.identifier(name),
940
- t__namespace.identifier(name)
941
- )
942
- );
943
- }
944
- if (t__namespace.isArrayPattern(decl.id)) {
945
- return collectIdentifiersFromPattern(decl.id).map(
946
- (name) => t__namespace.importSpecifier(
947
- t__namespace.identifier(name),
948
- t__namespace.identifier(name)
949
- )
950
- );
951
- }
952
- return [];
953
- }
954
- );
955
- if (specifiers.length === 0) {
956
- path.remove();
957
- return;
958
- }
959
- const importDecl = t__namespace.importDeclaration(
960
- specifiers,
961
- t__namespace.stringLiteral(
962
- removeSplitSearchParamFromFilename(opts.filename)
963
- )
964
- );
965
- path.replaceWith(importDecl);
966
- path.traverse({
967
- Identifier(identPath) {
968
- if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") {
969
- refIdents.add(identPath);
970
- }
971
- }
972
- });
973
- }
974
- }
975
- }
976
- });
977
- if (opts.sharedBindings && opts.sharedBindings.size > 0) {
978
- const sharedImportSpecifiers = [...opts.sharedBindings].map(
979
- (name) => t__namespace.importSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
980
- );
981
- const sharedModuleUrl = addSharedSearchParamToFilename(
982
- removeSplitSearchParamFromFilename(opts.filename)
983
- );
984
- const [sharedImportPath] = programPath.unshiftContainer(
985
- "body",
986
- t__namespace.importDeclaration(
987
- sharedImportSpecifiers,
988
- t__namespace.stringLiteral(sharedModuleUrl)
989
- )
990
- );
991
- sharedImportPath.traverse({
992
- Identifier(identPath) {
993
- if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") {
994
- refIdents.add(identPath);
995
- }
996
- }
997
- });
998
- }
999
- }
1000
- }
1001
- });
1002
- routerUtils.deadCodeElimination(ast, refIdents);
1003
- {
1004
- const locallyBound = /* @__PURE__ */ new Set();
1005
- for (const stmt of ast.program.body) {
1006
- collectLocalBindingsFromStatement(stmt, locallyBound);
1007
- }
1008
- ast.program.body = ast.program.body.filter((stmt) => {
1009
- if (!t__namespace.isExpressionStatement(stmt)) return true;
1010
- const refs = collectIdentifiersFromNode(stmt);
1011
- return [...refs].some((name) => locallyBound.has(name));
1012
- });
1013
- }
1014
- if (ast.program.body.length === 0) {
1015
- ast.program.directives = [];
1016
- }
1017
- const result = routerUtils.generateFromAst(ast, {
1018
- sourceMaps: true,
1019
- sourceFileName: opts.filename,
1020
- filename: opts.filename
1021
- });
1022
- if (result.map) {
1023
- result.map.sourcesContent = [opts.code];
1024
- }
1025
- return result;
527
+ const ast = (0, _tanstack_router_utils.parseAst)(opts);
528
+ const refIdents = (0, _tanstack_router_utils.findReferencedIdentifiers)(ast);
529
+ if (opts.sharedBindings && opts.sharedBindings.size > 0) removeSharedDeclarations(ast, opts.sharedBindings);
530
+ const intendedSplitNodes = new Set(opts.splitTargets);
531
+ const knownExportedIdents = /* @__PURE__ */ new Set();
532
+ _babel_core.traverse(ast, { Program: { enter(programPath) {
533
+ const trackedNodesToSplitByType = {
534
+ component: void 0,
535
+ loader: void 0,
536
+ pendingComponent: void 0,
537
+ errorComponent: void 0,
538
+ notFoundComponent: void 0
539
+ };
540
+ programPath.traverse({ CallExpression: (path) => {
541
+ if (!_babel_types.isIdentifier(path.node.callee)) return;
542
+ if (!splittableCreateRouteFns.includes(path.node.callee.name)) return;
543
+ function babelHandleVirtual(options) {
544
+ if (_babel_types.isObjectExpression(options)) {
545
+ options.properties.forEach((prop) => {
546
+ if (_babel_types.isObjectProperty(prop)) KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
547
+ if (!_babel_types.isIdentifier(prop.key) || prop.key.name !== splitType) return;
548
+ const value = prop.value;
549
+ if (_babel_types.isIdentifier(value) && value.name === "undefined") return;
550
+ let isExported = false;
551
+ if (_babel_types.isIdentifier(value)) {
552
+ isExported = hasExport(ast, value);
553
+ if (isExported) knownExportedIdents.add(value.name);
554
+ }
555
+ if (isExported && _babel_types.isIdentifier(value)) removeExports(ast, value);
556
+ else {
557
+ const meta = SPLIT_NODES_CONFIG.get(splitType);
558
+ trackedNodesToSplitByType[splitType] = {
559
+ node: prop.value,
560
+ meta
561
+ };
562
+ }
563
+ });
564
+ });
565
+ options.properties = [];
566
+ }
567
+ }
568
+ if (_babel_types.isCallExpression(path.parentPath.node)) babelHandleVirtual(resolveIdentifier(path, path.parentPath.node.arguments[0]));
569
+ else if (_babel_types.isVariableDeclarator(path.parentPath.node)) {
570
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
571
+ if (_babel_types.isCallExpression(caller)) babelHandleVirtual(resolveIdentifier(path, caller.arguments[0]));
572
+ }
573
+ } });
574
+ intendedSplitNodes.forEach((SPLIT_TYPE) => {
575
+ const splitKey = trackedNodesToSplitByType[SPLIT_TYPE];
576
+ if (!splitKey) return;
577
+ let splitNode = splitKey.node;
578
+ const splitMeta = {
579
+ ...splitKey.meta,
580
+ shouldRemoveNode: true
581
+ };
582
+ let originalIdentName;
583
+ if (_babel_types.isIdentifier(splitNode)) originalIdentName = splitNode.name;
584
+ while (_babel_types.isIdentifier(splitNode)) splitNode = programPath.scope.getBinding(splitNode.name)?.path.node;
585
+ if (splitNode) if (_babel_types.isFunctionDeclaration(splitNode)) {
586
+ if (!splitNode.id) throw new Error(`Function declaration for "${SPLIT_TYPE}" must have an identifier.`);
587
+ splitMeta.shouldRemoveNode = false;
588
+ splitMeta.localExporterIdent = splitNode.id.name;
589
+ } else if (_babel_types.isFunctionExpression(splitNode) || _babel_types.isArrowFunctionExpression(splitNode)) programPath.pushContainer("body", _babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(_babel_types.identifier(splitMeta.localExporterIdent), splitNode)]));
590
+ else if (_babel_types.isImportSpecifier(splitNode) || _babel_types.isImportDefaultSpecifier(splitNode)) programPath.pushContainer("body", _babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(_babel_types.identifier(splitMeta.localExporterIdent), splitNode.local)]));
591
+ else if (_babel_types.isVariableDeclarator(splitNode)) if (_babel_types.isIdentifier(splitNode.id)) {
592
+ splitMeta.localExporterIdent = splitNode.id.name;
593
+ splitMeta.shouldRemoveNode = false;
594
+ } else if (_babel_types.isObjectPattern(splitNode.id)) {
595
+ if (originalIdentName) splitMeta.localExporterIdent = originalIdentName;
596
+ splitMeta.shouldRemoveNode = false;
597
+ } else throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
598
+ else if (_babel_types.isCallExpression(splitNode)) {
599
+ const outputSplitNodeCode = (0, _tanstack_router_utils.generateFromAst)(splitNode).code;
600
+ const splitNodeAst = _babel_core.parse(outputSplitNodeCode);
601
+ if (!splitNodeAst) throw new Error(`Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`);
602
+ const statement = splitNodeAst.program.body[0];
603
+ if (!statement) throw new Error(`Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}" as no statement was found in the program body`);
604
+ if (_babel_types.isExpressionStatement(statement)) {
605
+ const expression = statement.expression;
606
+ programPath.pushContainer("body", _babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(_babel_types.identifier(splitMeta.localExporterIdent), expression)]));
607
+ } else throw new Error(`Unexpected expression type encounter for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`);
608
+ } else if (_babel_types.isConditionalExpression(splitNode)) programPath.pushContainer("body", _babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(_babel_types.identifier(splitMeta.localExporterIdent), splitNode)]));
609
+ else if (_babel_types.isTSAsExpression(splitNode)) {
610
+ splitNode = splitNode.expression;
611
+ programPath.pushContainer("body", _babel_types.variableDeclaration("const", [_babel_types.variableDeclarator(_babel_types.identifier(splitMeta.localExporterIdent), splitNode)]));
612
+ } else if (_babel_types.isBooleanLiteral(splitNode)) return;
613
+ else if (_babel_types.isNullLiteral(splitNode)) return;
614
+ else {
615
+ console.info("Unexpected splitNode type:", splitNode);
616
+ throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
617
+ }
618
+ if (splitMeta.shouldRemoveNode) programPath.node.body = programPath.node.body.filter((node) => {
619
+ return node !== splitNode;
620
+ });
621
+ programPath.pushContainer("body", [_babel_types.exportNamedDeclaration(null, [_babel_types.exportSpecifier(_babel_types.identifier(splitMeta.localExporterIdent), _babel_types.identifier(splitMeta.exporterIdent))])]);
622
+ });
623
+ programPath.traverse({ ExportNamedDeclaration(path) {
624
+ if (path.node.declaration) {
625
+ if (_babel_types.isVariableDeclaration(path.node.declaration)) {
626
+ const specifiers = path.node.declaration.declarations.flatMap((decl) => {
627
+ if (_babel_types.isIdentifier(decl.id)) return [_babel_types.importSpecifier(_babel_types.identifier(decl.id.name), _babel_types.identifier(decl.id.name))];
628
+ if (_babel_types.isObjectPattern(decl.id)) return collectIdentifiersFromPattern(decl.id).map((name) => _babel_types.importSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
629
+ if (_babel_types.isArrayPattern(decl.id)) return collectIdentifiersFromPattern(decl.id).map((name) => _babel_types.importSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
630
+ return [];
631
+ });
632
+ if (specifiers.length === 0) {
633
+ path.remove();
634
+ return;
635
+ }
636
+ const importDecl = _babel_types.importDeclaration(specifiers, _babel_types.stringLiteral(removeSplitSearchParamFromFilename(opts.filename)));
637
+ path.replaceWith(importDecl);
638
+ path.traverse({ Identifier(identPath) {
639
+ if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") refIdents.add(identPath);
640
+ } });
641
+ }
642
+ }
643
+ } });
644
+ if (opts.sharedBindings && opts.sharedBindings.size > 0) {
645
+ const sharedImportSpecifiers = [...opts.sharedBindings].map((name) => _babel_types.importSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
646
+ const sharedModuleUrl = addSharedSearchParamToFilename(removeSplitSearchParamFromFilename(opts.filename));
647
+ const [sharedImportPath] = programPath.unshiftContainer("body", _babel_types.importDeclaration(sharedImportSpecifiers, _babel_types.stringLiteral(sharedModuleUrl)));
648
+ sharedImportPath.traverse({ Identifier(identPath) {
649
+ if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") refIdents.add(identPath);
650
+ } });
651
+ }
652
+ } } });
653
+ (0, _tanstack_router_utils.deadCodeElimination)(ast, refIdents);
654
+ {
655
+ const locallyBound = /* @__PURE__ */ new Set();
656
+ for (const stmt of ast.program.body) collectLocalBindingsFromStatement(stmt, locallyBound);
657
+ ast.program.body = ast.program.body.filter((stmt) => {
658
+ if (!_babel_types.isExpressionStatement(stmt)) return true;
659
+ return [...collectIdentifiersFromNode(stmt)].some((name) => locallyBound.has(name));
660
+ });
661
+ }
662
+ if (ast.program.body.length === 0) ast.program.directives = [];
663
+ const result = (0, _tanstack_router_utils.generateFromAst)(ast, {
664
+ sourceMaps: true,
665
+ sourceFileName: opts.filename,
666
+ filename: opts.filename
667
+ });
668
+ if (result.map) result.map.sourcesContent = [opts.code];
669
+ return result;
1026
670
  }
671
+ /**
672
+ * Compile the shared virtual module (`?tsr-shared=1`).
673
+ * Keeps only shared binding declarations, their transitive dependencies,
674
+ * and imports they need. Exports all shared bindings.
675
+ */
1027
676
  function compileCodeSplitSharedRoute(opts) {
1028
- const ast = routerUtils.parseAst(opts);
1029
- const refIdents = routerUtils.findReferencedIdentifiers(ast);
1030
- const localBindings = /* @__PURE__ */ new Set();
1031
- for (const node of ast.program.body) {
1032
- collectLocalBindingsFromStatement(node, localBindings);
1033
- }
1034
- localBindings.delete("Route");
1035
- const declMap = buildDeclarationMap(ast);
1036
- const depGraph = buildDependencyGraph(declMap, localBindings);
1037
- const keepBindings = new Set(opts.sharedBindings);
1038
- keepBindings.delete("Route");
1039
- expandTransitively(keepBindings, depGraph);
1040
- ast.program.body = ast.program.body.filter((stmt) => {
1041
- if (t__namespace.isImportDeclaration(stmt)) return true;
1042
- const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
1043
- if (t__namespace.isVariableDeclaration(decl)) {
1044
- decl.declarations = decl.declarations.filter((declarator) => {
1045
- const names = collectIdentifiersFromPattern(declarator.id);
1046
- return names.some((n) => keepBindings.has(n));
1047
- });
1048
- if (decl.declarations.length === 0) return false;
1049
- if (t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration) {
1050
- return true;
1051
- }
1052
- return true;
1053
- } else if (t__namespace.isFunctionDeclaration(decl) && decl.id) {
1054
- return keepBindings.has(decl.id.name);
1055
- } else if (t__namespace.isClassDeclaration(decl) && decl.id) {
1056
- return keepBindings.has(decl.id.name);
1057
- }
1058
- return false;
1059
- });
1060
- ast.program.body = ast.program.body.map((stmt) => {
1061
- if (t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration) {
1062
- return stmt.declaration;
1063
- }
1064
- return stmt;
1065
- });
1066
- const exportNames = [...opts.sharedBindings].sort(
1067
- (a, b) => a.localeCompare(b)
1068
- );
1069
- const exportSpecifiers = exportNames.map(
1070
- (name) => t__namespace.exportSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
1071
- );
1072
- if (exportSpecifiers.length > 0) {
1073
- const exportDecl = t__namespace.exportNamedDeclaration(null, exportSpecifiers);
1074
- ast.program.body.push(exportDecl);
1075
- babel.traverse(ast, {
1076
- Program(programPath) {
1077
- const bodyPaths = programPath.get("body");
1078
- const last = bodyPaths[bodyPaths.length - 1];
1079
- if (last && last.isExportNamedDeclaration()) {
1080
- last.traverse({
1081
- Identifier(identPath) {
1082
- if (identPath.parentPath.isExportSpecifier() && identPath.key === "local") {
1083
- refIdents.add(identPath);
1084
- }
1085
- }
1086
- });
1087
- }
1088
- programPath.stop();
1089
- }
1090
- });
1091
- }
1092
- routerUtils.deadCodeElimination(ast, refIdents);
1093
- if (ast.program.body.length === 0) {
1094
- ast.program.directives = [];
1095
- }
1096
- const result = routerUtils.generateFromAst(ast, {
1097
- sourceMaps: true,
1098
- sourceFileName: opts.filename,
1099
- filename: opts.filename
1100
- });
1101
- if (result.map) {
1102
- result.map.sourcesContent = [opts.code];
1103
- }
1104
- return result;
677
+ const ast = (0, _tanstack_router_utils.parseAst)(opts);
678
+ const refIdents = (0, _tanstack_router_utils.findReferencedIdentifiers)(ast);
679
+ const localBindings = /* @__PURE__ */ new Set();
680
+ for (const node of ast.program.body) collectLocalBindingsFromStatement(node, localBindings);
681
+ localBindings.delete("Route");
682
+ const depGraph = buildDependencyGraph(buildDeclarationMap(ast), localBindings);
683
+ const keepBindings = new Set(opts.sharedBindings);
684
+ keepBindings.delete("Route");
685
+ expandTransitively(keepBindings, depGraph);
686
+ ast.program.body = ast.program.body.filter((stmt) => {
687
+ if (_babel_types.isImportDeclaration(stmt)) return true;
688
+ const decl = _babel_types.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
689
+ if (_babel_types.isVariableDeclaration(decl)) {
690
+ decl.declarations = decl.declarations.filter((declarator) => {
691
+ return collectIdentifiersFromPattern(declarator.id).some((n) => keepBindings.has(n));
692
+ });
693
+ if (decl.declarations.length === 0) return false;
694
+ if (_babel_types.isExportNamedDeclaration(stmt) && stmt.declaration) return true;
695
+ return true;
696
+ } else if (_babel_types.isFunctionDeclaration(decl) && decl.id) return keepBindings.has(decl.id.name);
697
+ else if (_babel_types.isClassDeclaration(decl) && decl.id) return keepBindings.has(decl.id.name);
698
+ return false;
699
+ });
700
+ ast.program.body = ast.program.body.map((stmt) => {
701
+ if (_babel_types.isExportNamedDeclaration(stmt) && stmt.declaration) return stmt.declaration;
702
+ return stmt;
703
+ });
704
+ const exportSpecifiers = [...opts.sharedBindings].sort((a, b) => a.localeCompare(b)).map((name) => _babel_types.exportSpecifier(_babel_types.identifier(name), _babel_types.identifier(name)));
705
+ if (exportSpecifiers.length > 0) {
706
+ const exportDecl = _babel_types.exportNamedDeclaration(null, exportSpecifiers);
707
+ ast.program.body.push(exportDecl);
708
+ _babel_core.traverse(ast, { Program(programPath) {
709
+ const bodyPaths = programPath.get("body");
710
+ const last = bodyPaths[bodyPaths.length - 1];
711
+ if (last && last.isExportNamedDeclaration()) last.traverse({ Identifier(identPath) {
712
+ if (identPath.parentPath.isExportSpecifier() && identPath.key === "local") refIdents.add(identPath);
713
+ } });
714
+ programPath.stop();
715
+ } });
716
+ }
717
+ (0, _tanstack_router_utils.deadCodeElimination)(ast, refIdents);
718
+ if (ast.program.body.length === 0) ast.program.directives = [];
719
+ const result = (0, _tanstack_router_utils.generateFromAst)(ast, {
720
+ sourceMaps: true,
721
+ sourceFileName: opts.filename,
722
+ filename: opts.filename
723
+ });
724
+ if (result.map) result.map.sourcesContent = [opts.code];
725
+ return result;
1105
726
  }
727
+ /**
728
+ * This function should read get the options from by searching for the key `codeSplitGroupings`
729
+ * on createFileRoute and return it's values if it exists, else return undefined
730
+ */
1106
731
  function detectCodeSplitGroupingsFromRoute(opts) {
1107
- const ast = routerUtils.parseAst(opts);
1108
- let codeSplitGroupings = void 0;
1109
- babel.traverse(ast, {
1110
- Program: {
1111
- enter(programPath) {
1112
- programPath.traverse({
1113
- CallExpression(path) {
1114
- if (!t__namespace.isIdentifier(path.node.callee)) {
1115
- return;
1116
- }
1117
- if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
1118
- return;
1119
- }
1120
- function babelHandleSplittingGroups(routeOptions) {
1121
- if (t__namespace.isObjectExpression(routeOptions)) {
1122
- routeOptions.properties.forEach((prop) => {
1123
- if (t__namespace.isObjectProperty(prop)) {
1124
- if (t__namespace.isIdentifier(prop.key)) {
1125
- if (prop.key.name === "codeSplitGroupings") {
1126
- const value = prop.value;
1127
- if (t__namespace.isArrayExpression(value)) {
1128
- codeSplitGroupings = value.elements.map((group) => {
1129
- if (t__namespace.isArrayExpression(group)) {
1130
- return group.elements.map((node) => {
1131
- if (!t__namespace.isStringLiteral(node)) {
1132
- throw new Error(
1133
- "You must provide a string literal for the codeSplitGroupings"
1134
- );
1135
- }
1136
- return node.value;
1137
- });
1138
- }
1139
- throw new Error(
1140
- "You must provide arrays with codeSplitGroupings options."
1141
- );
1142
- });
1143
- } else {
1144
- throw new Error(
1145
- "You must provide an array of arrays for the codeSplitGroupings."
1146
- );
1147
- }
1148
- }
1149
- }
1150
- }
1151
- });
1152
- }
1153
- }
1154
- if (t__namespace.isCallExpression(path.parentPath.node)) {
1155
- const options = resolveIdentifier(
1156
- path,
1157
- path.parentPath.node.arguments[0]
1158
- );
1159
- babelHandleSplittingGroups(options);
1160
- } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
1161
- const caller = resolveIdentifier(path, path.parentPath.node.init);
1162
- if (t__namespace.isCallExpression(caller)) {
1163
- const options = resolveIdentifier(path, caller.arguments[0]);
1164
- babelHandleSplittingGroups(options);
1165
- }
1166
- }
1167
- }
1168
- });
1169
- }
1170
- }
1171
- });
1172
- return { groupings: codeSplitGroupings };
732
+ const ast = (0, _tanstack_router_utils.parseAst)(opts);
733
+ let codeSplitGroupings = void 0;
734
+ _babel_core.traverse(ast, { Program: { enter(programPath) {
735
+ programPath.traverse({ CallExpression(path) {
736
+ if (!_babel_types.isIdentifier(path.node.callee)) return;
737
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) return;
738
+ function babelHandleSplittingGroups(routeOptions) {
739
+ if (_babel_types.isObjectExpression(routeOptions)) routeOptions.properties.forEach((prop) => {
740
+ if (_babel_types.isObjectProperty(prop)) {
741
+ if (_babel_types.isIdentifier(prop.key)) {
742
+ if (prop.key.name === "codeSplitGroupings") {
743
+ const value = prop.value;
744
+ if (_babel_types.isArrayExpression(value)) codeSplitGroupings = value.elements.map((group) => {
745
+ if (_babel_types.isArrayExpression(group)) return group.elements.map((node) => {
746
+ if (!_babel_types.isStringLiteral(node)) throw new Error("You must provide a string literal for the codeSplitGroupings");
747
+ return node.value;
748
+ });
749
+ throw new Error("You must provide arrays with codeSplitGroupings options.");
750
+ });
751
+ else throw new Error("You must provide an array of arrays for the codeSplitGroupings.");
752
+ }
753
+ }
754
+ }
755
+ });
756
+ }
757
+ if (_babel_types.isCallExpression(path.parentPath.node)) babelHandleSplittingGroups(resolveIdentifier(path, path.parentPath.node.arguments[0]));
758
+ else if (_babel_types.isVariableDeclarator(path.parentPath.node)) {
759
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
760
+ if (_babel_types.isCallExpression(caller)) babelHandleSplittingGroups(resolveIdentifier(path, caller.arguments[0]));
761
+ }
762
+ } });
763
+ } } });
764
+ return { groupings: codeSplitGroupings };
1173
765
  }
1174
766
  function createNotExportableMessage(filename, idents) {
1175
- const list = Array.from(idents).map((d) => `- ${d}`);
1176
- const message = [
1177
- `[tanstack-router] These exports from "${filename}" will not be code-split and will increase your bundle size:`,
1178
- ...list,
1179
- "For the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file."
1180
- ].join("\n");
1181
- return message;
767
+ const list = Array.from(idents).map((d) => `- ${d}`);
768
+ return [
769
+ `[tanstack-router] These exports from "${filename}" will not be code-split and will increase your bundle size:`,
770
+ ...list,
771
+ "For the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file."
772
+ ].join("\n");
1182
773
  }
1183
774
  function getImportSpecifierAndPathFromLocalName(programPath, name) {
1184
- let specifier = null;
1185
- let path = null;
1186
- programPath.traverse({
1187
- ImportDeclaration(importPath) {
1188
- const found = importPath.node.specifiers.find(
1189
- (targetSpecifier) => targetSpecifier.local.name === name
1190
- );
1191
- if (found) {
1192
- specifier = found;
1193
- path = importPath.node.source.value;
1194
- }
1195
- }
1196
- });
1197
- return { specifier, path };
775
+ let specifier = null;
776
+ let path = null;
777
+ programPath.traverse({ ImportDeclaration(importPath) {
778
+ const found = importPath.node.specifiers.find((targetSpecifier) => targetSpecifier.local.name === name);
779
+ if (found) {
780
+ specifier = found;
781
+ path = importPath.node.source.value;
782
+ }
783
+ } });
784
+ return {
785
+ specifier,
786
+ path
787
+ };
1198
788
  }
789
+ /**
790
+ * Recursively collects all identifier names from a destructuring pattern
791
+ * (ObjectPattern, ArrayPattern, AssignmentPattern, RestElement).
792
+ */
1199
793
  function collectIdentifiersFromPattern(node) {
1200
- if (!node) {
1201
- return [];
1202
- }
1203
- if (t__namespace.isIdentifier(node)) {
1204
- return [node.name];
1205
- }
1206
- if (t__namespace.isAssignmentPattern(node)) {
1207
- return collectIdentifiersFromPattern(node.left);
1208
- }
1209
- if (t__namespace.isRestElement(node)) {
1210
- return collectIdentifiersFromPattern(node.argument);
1211
- }
1212
- if (t__namespace.isObjectPattern(node)) {
1213
- return node.properties.flatMap((prop) => {
1214
- if (t__namespace.isObjectProperty(prop)) {
1215
- return collectIdentifiersFromPattern(prop.value);
1216
- }
1217
- if (t__namespace.isRestElement(prop)) {
1218
- return collectIdentifiersFromPattern(prop.argument);
1219
- }
1220
- return [];
1221
- });
1222
- }
1223
- if (t__namespace.isArrayPattern(node)) {
1224
- return node.elements.flatMap(
1225
- (element) => collectIdentifiersFromPattern(element)
1226
- );
1227
- }
1228
- return [];
794
+ if (!node) return [];
795
+ if (_babel_types.isIdentifier(node)) return [node.name];
796
+ if (_babel_types.isAssignmentPattern(node)) return collectIdentifiersFromPattern(node.left);
797
+ if (_babel_types.isRestElement(node)) return collectIdentifiersFromPattern(node.argument);
798
+ if (_babel_types.isObjectPattern(node)) return node.properties.flatMap((prop) => {
799
+ if (_babel_types.isObjectProperty(prop)) return collectIdentifiersFromPattern(prop.value);
800
+ if (_babel_types.isRestElement(prop)) return collectIdentifiersFromPattern(prop.argument);
801
+ return [];
802
+ });
803
+ if (_babel_types.isArrayPattern(node)) return node.elements.flatMap((element) => collectIdentifiersFromPattern(element));
804
+ return [];
1229
805
  }
1230
806
  function resolveIdentifier(path, node) {
1231
- if (t__namespace.isIdentifier(node)) {
1232
- const binding = path.scope.getBinding(node.name);
1233
- if (binding) {
1234
- const declarator = binding.path.node;
1235
- if (t__namespace.isObjectExpression(declarator.init)) {
1236
- return declarator.init;
1237
- } else if (t__namespace.isFunctionDeclaration(declarator.init)) {
1238
- return declarator.init;
1239
- }
1240
- }
1241
- return void 0;
1242
- }
1243
- return node;
807
+ if (_babel_types.isIdentifier(node)) {
808
+ const binding = path.scope.getBinding(node.name);
809
+ if (binding) {
810
+ const declarator = binding.path.node;
811
+ if (_babel_types.isObjectExpression(declarator.init)) return declarator.init;
812
+ else if (_babel_types.isFunctionDeclaration(declarator.init)) return declarator.init;
813
+ }
814
+ return;
815
+ }
816
+ return node;
1244
817
  }
1245
818
  function removeIdentifierLiteral(path, node) {
1246
- const binding = path.scope.getBinding(node.name);
1247
- if (binding) {
1248
- if (t__namespace.isVariableDeclarator(binding.path.node) && t__namespace.isObjectPattern(binding.path.node.id)) {
1249
- const objectPattern = binding.path.node.id;
1250
- objectPattern.properties = objectPattern.properties.filter((prop) => {
1251
- if (!t__namespace.isObjectProperty(prop)) {
1252
- return true;
1253
- }
1254
- if (t__namespace.isIdentifier(prop.value) && prop.value.name === node.name) {
1255
- return false;
1256
- }
1257
- if (t__namespace.isAssignmentPattern(prop.value) && t__namespace.isIdentifier(prop.value.left) && prop.value.left.name === node.name) {
1258
- return false;
1259
- }
1260
- return true;
1261
- });
1262
- if (objectPattern.properties.length === 0) {
1263
- binding.path.remove();
1264
- }
1265
- return;
1266
- }
1267
- binding.path.remove();
1268
- }
819
+ const binding = path.scope.getBinding(node.name);
820
+ if (binding) {
821
+ if (_babel_types.isVariableDeclarator(binding.path.node) && _babel_types.isObjectPattern(binding.path.node.id)) {
822
+ const objectPattern = binding.path.node.id;
823
+ objectPattern.properties = objectPattern.properties.filter((prop) => {
824
+ if (!_babel_types.isObjectProperty(prop)) return true;
825
+ if (_babel_types.isIdentifier(prop.value) && prop.value.name === node.name) return false;
826
+ if (_babel_types.isAssignmentPattern(prop.value) && _babel_types.isIdentifier(prop.value.left) && prop.value.left.name === node.name) return false;
827
+ return true;
828
+ });
829
+ if (objectPattern.properties.length === 0) binding.path.remove();
830
+ return;
831
+ }
832
+ binding.path.remove();
833
+ }
1269
834
  }
1270
835
  function hasExport(ast, node) {
1271
- let found = false;
1272
- babel.traverse(ast, {
1273
- ExportNamedDeclaration(path) {
1274
- if (path.node.declaration) {
1275
- if (t__namespace.isVariableDeclaration(path.node.declaration)) {
1276
- path.node.declaration.declarations.forEach((decl) => {
1277
- if (t__namespace.isVariableDeclarator(decl)) {
1278
- if (t__namespace.isIdentifier(decl.id)) {
1279
- if (decl.id.name === node.name) {
1280
- found = true;
1281
- }
1282
- } else if (t__namespace.isObjectPattern(decl.id) || t__namespace.isArrayPattern(decl.id)) {
1283
- const names = collectIdentifiersFromPattern(decl.id);
1284
- if (names.includes(node.name)) {
1285
- found = true;
1286
- }
1287
- }
1288
- }
1289
- });
1290
- }
1291
- if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
1292
- if (t__namespace.isIdentifier(path.node.declaration.id)) {
1293
- if (path.node.declaration.id.name === node.name) {
1294
- found = true;
1295
- }
1296
- }
1297
- }
1298
- }
1299
- },
1300
- ExportDefaultDeclaration(path) {
1301
- if (t__namespace.isIdentifier(path.node.declaration)) {
1302
- if (path.node.declaration.name === node.name) {
1303
- found = true;
1304
- }
1305
- }
1306
- if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
1307
- if (t__namespace.isIdentifier(path.node.declaration.id)) {
1308
- if (path.node.declaration.id.name === node.name) {
1309
- found = true;
1310
- }
1311
- }
1312
- }
1313
- }
1314
- });
1315
- return found;
836
+ let found = false;
837
+ _babel_core.traverse(ast, {
838
+ ExportNamedDeclaration(path) {
839
+ if (path.node.declaration) {
840
+ if (_babel_types.isVariableDeclaration(path.node.declaration)) path.node.declaration.declarations.forEach((decl) => {
841
+ if (_babel_types.isVariableDeclarator(decl)) {
842
+ if (_babel_types.isIdentifier(decl.id)) {
843
+ if (decl.id.name === node.name) found = true;
844
+ } else if (_babel_types.isObjectPattern(decl.id) || _babel_types.isArrayPattern(decl.id)) {
845
+ if (collectIdentifiersFromPattern(decl.id).includes(node.name)) found = true;
846
+ }
847
+ }
848
+ });
849
+ if (_babel_types.isFunctionDeclaration(path.node.declaration)) {
850
+ if (_babel_types.isIdentifier(path.node.declaration.id)) {
851
+ if (path.node.declaration.id.name === node.name) found = true;
852
+ }
853
+ }
854
+ }
855
+ },
856
+ ExportDefaultDeclaration(path) {
857
+ if (_babel_types.isIdentifier(path.node.declaration)) {
858
+ if (path.node.declaration.name === node.name) found = true;
859
+ }
860
+ if (_babel_types.isFunctionDeclaration(path.node.declaration)) {
861
+ if (_babel_types.isIdentifier(path.node.declaration.id)) {
862
+ if (path.node.declaration.id.name === node.name) found = true;
863
+ }
864
+ }
865
+ }
866
+ });
867
+ return found;
1316
868
  }
1317
869
  function removeExports(ast, node) {
1318
- let removed = false;
1319
- babel.traverse(ast, {
1320
- ExportNamedDeclaration(path) {
1321
- if (path.node.declaration) {
1322
- if (t__namespace.isVariableDeclaration(path.node.declaration)) {
1323
- path.node.declaration.declarations.forEach((decl) => {
1324
- if (t__namespace.isVariableDeclarator(decl)) {
1325
- if (t__namespace.isIdentifier(decl.id)) {
1326
- if (decl.id.name === node.name) {
1327
- path.remove();
1328
- removed = true;
1329
- }
1330
- } else if (t__namespace.isObjectPattern(decl.id) || t__namespace.isArrayPattern(decl.id)) {
1331
- const names = collectIdentifiersFromPattern(decl.id);
1332
- if (names.includes(node.name)) {
1333
- path.remove();
1334
- removed = true;
1335
- }
1336
- }
1337
- }
1338
- });
1339
- } else if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
1340
- if (t__namespace.isIdentifier(path.node.declaration.id)) {
1341
- if (path.node.declaration.id.name === node.name) {
1342
- path.remove();
1343
- removed = true;
1344
- }
1345
- }
1346
- }
1347
- }
1348
- },
1349
- ExportDefaultDeclaration(path) {
1350
- if (t__namespace.isIdentifier(path.node.declaration)) {
1351
- if (path.node.declaration.name === node.name) {
1352
- path.remove();
1353
- removed = true;
1354
- }
1355
- } else if (t__namespace.isFunctionDeclaration(path.node.declaration)) {
1356
- if (t__namespace.isIdentifier(path.node.declaration.id)) {
1357
- if (path.node.declaration.id.name === node.name) {
1358
- path.remove();
1359
- removed = true;
1360
- }
1361
- }
1362
- }
1363
- }
1364
- });
1365
- return removed;
870
+ let removed = false;
871
+ _babel_core.traverse(ast, {
872
+ ExportNamedDeclaration(path) {
873
+ if (path.node.declaration) {
874
+ if (_babel_types.isVariableDeclaration(path.node.declaration)) path.node.declaration.declarations.forEach((decl) => {
875
+ if (_babel_types.isVariableDeclarator(decl)) {
876
+ if (_babel_types.isIdentifier(decl.id)) {
877
+ if (decl.id.name === node.name) {
878
+ path.remove();
879
+ removed = true;
880
+ }
881
+ } else if (_babel_types.isObjectPattern(decl.id) || _babel_types.isArrayPattern(decl.id)) {
882
+ if (collectIdentifiersFromPattern(decl.id).includes(node.name)) {
883
+ path.remove();
884
+ removed = true;
885
+ }
886
+ }
887
+ }
888
+ });
889
+ else if (_babel_types.isFunctionDeclaration(path.node.declaration)) {
890
+ if (_babel_types.isIdentifier(path.node.declaration.id)) {
891
+ if (path.node.declaration.id.name === node.name) {
892
+ path.remove();
893
+ removed = true;
894
+ }
895
+ }
896
+ }
897
+ }
898
+ },
899
+ ExportDefaultDeclaration(path) {
900
+ if (_babel_types.isIdentifier(path.node.declaration)) {
901
+ if (path.node.declaration.name === node.name) {
902
+ path.remove();
903
+ removed = true;
904
+ }
905
+ } else if (_babel_types.isFunctionDeclaration(path.node.declaration)) {
906
+ if (_babel_types.isIdentifier(path.node.declaration.id)) {
907
+ if (path.node.declaration.id.name === node.name) {
908
+ path.remove();
909
+ removed = true;
910
+ }
911
+ }
912
+ }
913
+ }
914
+ });
915
+ return removed;
1366
916
  }
1367
- exports.addSharedSearchParamToFilename = addSharedSearchParamToFilename;
1368
- exports.buildDeclarationMap = buildDeclarationMap;
1369
- exports.buildDependencyGraph = buildDependencyGraph;
1370
- exports.collectIdentifiersFromNode = collectIdentifiersFromNode;
1371
- exports.collectLocalBindingsFromStatement = collectLocalBindingsFromStatement;
1372
- exports.collectModuleLevelRefsFromNode = collectModuleLevelRefsFromNode;
917
+ //#endregion
1373
918
  exports.compileCodeSplitReferenceRoute = compileCodeSplitReferenceRoute;
1374
919
  exports.compileCodeSplitSharedRoute = compileCodeSplitSharedRoute;
1375
920
  exports.compileCodeSplitVirtualRoute = compileCodeSplitVirtualRoute;
1376
921
  exports.computeSharedBindings = computeSharedBindings;
1377
922
  exports.detectCodeSplitGroupingsFromRoute = detectCodeSplitGroupingsFromRoute;
1378
- exports.expandDestructuredDeclarations = expandDestructuredDeclarations;
1379
- exports.expandSharedDestructuredDeclarators = expandSharedDestructuredDeclarators;
1380
- exports.expandTransitively = expandTransitively;
1381
- exports.removeBindingsDependingOnRoute = removeBindingsDependingOnRoute;
1382
- //# sourceMappingURL=compilers.cjs.map
923
+
924
+ //# sourceMappingURL=compilers.cjs.map