@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.
- package/dist/cjs/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/core/code-splitter/compilers.cjs +866 -1324
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/framework-options.cjs +39 -41
- package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/path-ids.cjs +26 -31
- package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs +11 -14
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs +31 -65
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs.map +1 -1
- package/dist/cjs/core/config.cjs +36 -46
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/constants.cjs +16 -15
- package/dist/cjs/core/constants.cjs.map +1 -1
- package/dist/cjs/core/route-autoimport-plugin.cjs +81 -115
- package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
- package/dist/cjs/core/route-hmr-statement.cjs +34 -56
- package/dist/cjs/core/route-hmr-statement.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +236 -306
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-composed-plugin.cjs +28 -34
- package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +101 -133
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +81 -81
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/utils.cjs +23 -30
- package/dist/cjs/core/utils.cjs.map +1 -1
- package/dist/cjs/esbuild.cjs +47 -18
- package/dist/cjs/esbuild.cjs.map +1 -1
- package/dist/cjs/index.cjs +11 -13
- package/dist/cjs/rspack.cjs +58 -19
- package/dist/cjs/rspack.cjs.map +1 -1
- package/dist/cjs/vite.cjs +52 -20
- package/dist/cjs/vite.cjs.map +1 -1
- package/dist/cjs/webpack.cjs +46 -19
- package/dist/cjs/webpack.cjs.map +1 -1
- package/dist/esm/core/code-splitter/compilers.js +859 -1307
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/framework-options.js +40 -42
- package/dist/esm/core/code-splitter/framework-options.js.map +1 -1
- package/dist/esm/core/code-splitter/path-ids.js +27 -33
- package/dist/esm/core/code-splitter/path-ids.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js +11 -14
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js +29 -48
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js.map +1 -1
- package/dist/esm/core/config.js +35 -48
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/constants.js +17 -19
- package/dist/esm/core/constants.js.map +1 -1
- package/dist/esm/core/route-autoimport-plugin.js +78 -98
- package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
- package/dist/esm/core/route-hmr-statement.js +32 -39
- package/dist/esm/core/route-hmr-statement.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +234 -305
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-composed-plugin.js +25 -32
- package/dist/esm/core/router-composed-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +100 -111
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +79 -80
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/core/utils.js +21 -15
- package/dist/esm/core/utils.js.map +1 -1
- package/dist/esm/esbuild.js +37 -18
- package/dist/esm/esbuild.js.map +1 -1
- package/dist/esm/index.js +2 -11
- package/dist/esm/rspack.js +49 -20
- package/dist/esm/rspack.js.map +1 -1
- package/dist/esm/vite.js +42 -21
- package/dist/esm/vite.js.map +1 -1
- package/dist/esm/webpack.js +37 -20
- package/dist/esm/webpack.js.map +1 -1
- package/package.json +6 -6
- package/src/core/code-splitter/compilers.ts +1 -1
- package/src/core/route-autoimport-plugin.ts +1 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.js.map +0 -1
|
@@ -1,1382 +1,924 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
51
|
+
var KNOWN_SPLIT_ROUTE_IDENTS = [...SPLIT_NODES_CONFIG.keys()];
|
|
97
52
|
function addSplitSearchParamToFilename(filename, grouping) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
106
|
-
|
|
59
|
+
const [bareFilename] = filename.split("?");
|
|
60
|
+
return bareFilename;
|
|
107
61
|
}
|
|
108
62
|
function addSharedSearchParamToFilename(filename) {
|
|
109
|
-
|
|
110
|
-
|
|
63
|
+
const [bareFilename] = filename.split("?");
|
|
64
|
+
return `${bareFilename}?${require_constants.tsrShared}=1`;
|
|
111
65
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
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
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
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
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
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
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
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
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
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
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
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
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
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
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
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
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
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
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1379
|
-
|
|
1380
|
-
exports.expandTransitively = expandTransitively;
|
|
1381
|
-
exports.removeBindingsDependingOnRoute = removeBindingsDependingOnRoute;
|
|
1382
|
-
//# sourceMappingURL=compilers.cjs.map
|
|
923
|
+
|
|
924
|
+
//# sourceMappingURL=compilers.cjs.map
|