@tanstack/router-plugin 1.159.10 → 1.159.12
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/core/code-splitter/compilers.cjs +571 -6
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +82 -0
- package/dist/cjs/core/constants.cjs +2 -0
- package/dist/cjs/core/constants.cjs.map +1 -1
- package/dist/cjs/core/constants.d.cts +1 -0
- package/dist/cjs/core/router-code-splitter-plugin.cjs +62 -3
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/esm/core/code-splitter/compilers.d.ts +82 -0
- package/dist/esm/core/code-splitter/compilers.js +573 -8
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/constants.d.ts +1 -0
- package/dist/esm/core/constants.js +2 -0
- package/dist/esm/core/constants.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +64 -5
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/package.json +3 -3
- package/src/core/code-splitter/compilers.ts +919 -7
- package/src/core/constants.ts +1 -0
- package/src/core/router-code-splitter-plugin.ts +78 -1
|
@@ -105,6 +105,10 @@ function removeSplitSearchParamFromFilename(filename) {
|
|
|
105
105
|
const [bareFilename] = filename.split("?");
|
|
106
106
|
return bareFilename;
|
|
107
107
|
}
|
|
108
|
+
function addSharedSearchParamToFilename(filename) {
|
|
109
|
+
const [bareFilename] = filename.split("?");
|
|
110
|
+
return `${bareFilename}?${constants.tsrShared}=1`;
|
|
111
|
+
}
|
|
108
112
|
const splittableCreateRouteFns = ["createFileRoute"];
|
|
109
113
|
const unsplittableCreateRouteFns = [
|
|
110
114
|
"createRootRoute",
|
|
@@ -114,6 +118,304 @@ const allCreateRouteFns = [
|
|
|
114
118
|
...splittableCreateRouteFns,
|
|
115
119
|
...unsplittableCreateRouteFns
|
|
116
120
|
];
|
|
121
|
+
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;
|
|
159
|
+
}
|
|
160
|
+
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;
|
|
177
|
+
}
|
|
178
|
+
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;
|
|
190
|
+
}
|
|
191
|
+
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;
|
|
273
|
+
}
|
|
274
|
+
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
|
+
}
|
|
295
|
+
}
|
|
296
|
+
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
|
+
}
|
|
309
|
+
}
|
|
310
|
+
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;
|
|
317
|
+
}
|
|
318
|
+
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
|
+
}
|
|
334
|
+
}
|
|
335
|
+
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
|
+
}
|
|
364
|
+
}
|
|
365
|
+
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
|
+
}
|
|
381
|
+
}
|
|
382
|
+
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;
|
|
401
|
+
}
|
|
402
|
+
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
|
+
});
|
|
418
|
+
}
|
|
117
419
|
function compileCodeSplitReferenceRoute(opts) {
|
|
118
420
|
const ast = routerUtils.parseAst(opts);
|
|
119
421
|
const refIdents = routerUtils.findReferencedIdentifiers(ast);
|
|
@@ -130,6 +432,7 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
130
432
|
let createRouteFn;
|
|
131
433
|
let modified = false;
|
|
132
434
|
let hmrAdded = false;
|
|
435
|
+
let sharedExportedNames;
|
|
133
436
|
babel.traverse(ast, {
|
|
134
437
|
Program: {
|
|
135
438
|
enter(programPath) {
|
|
@@ -325,6 +628,44 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
325
628
|
}
|
|
326
629
|
});
|
|
327
630
|
}
|
|
631
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0) {
|
|
632
|
+
sharedExportedNames = findExportedSharedBindings(
|
|
633
|
+
ast,
|
|
634
|
+
opts.sharedBindings
|
|
635
|
+
);
|
|
636
|
+
removeSharedDeclarations(ast, opts.sharedBindings);
|
|
637
|
+
const sharedModuleUrl = addSharedSearchParamToFilename(opts.filename);
|
|
638
|
+
const sharedImportSpecifiers = [...opts.sharedBindings].map(
|
|
639
|
+
(name) => t__namespace.importSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
|
|
640
|
+
);
|
|
641
|
+
const [sharedImportPath] = programPath.unshiftContainer(
|
|
642
|
+
"body",
|
|
643
|
+
t__namespace.importDeclaration(
|
|
644
|
+
sharedImportSpecifiers,
|
|
645
|
+
t__namespace.stringLiteral(sharedModuleUrl)
|
|
646
|
+
)
|
|
647
|
+
);
|
|
648
|
+
sharedImportPath.traverse({
|
|
649
|
+
Identifier(identPath) {
|
|
650
|
+
if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") {
|
|
651
|
+
refIdents.add(identPath);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
if (sharedExportedNames.size > 0) {
|
|
656
|
+
const reExportSpecifiers = [...sharedExportedNames].map(
|
|
657
|
+
(name) => t__namespace.exportSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
|
|
658
|
+
);
|
|
659
|
+
programPath.pushContainer(
|
|
660
|
+
"body",
|
|
661
|
+
t__namespace.exportNamedDeclaration(
|
|
662
|
+
null,
|
|
663
|
+
reExportSpecifiers,
|
|
664
|
+
t__namespace.stringLiteral(sharedModuleUrl)
|
|
665
|
+
)
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
328
669
|
}
|
|
329
670
|
}
|
|
330
671
|
});
|
|
@@ -354,6 +695,9 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
354
695
|
function compileCodeSplitVirtualRoute(opts) {
|
|
355
696
|
const ast = routerUtils.parseAst(opts);
|
|
356
697
|
const refIdents = routerUtils.findReferencedIdentifiers(ast);
|
|
698
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0) {
|
|
699
|
+
removeSharedDeclarations(ast, opts.sharedBindings);
|
|
700
|
+
}
|
|
357
701
|
const intendedSplitNodes = new Set(opts.splitTargets);
|
|
358
702
|
const knownExportedIdents = /* @__PURE__ */ new Set();
|
|
359
703
|
babel.traverse(ast, {
|
|
@@ -430,6 +774,10 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
430
774
|
}
|
|
431
775
|
let splitNode = splitKey.node;
|
|
432
776
|
const splitMeta = { ...splitKey.meta, shouldRemoveNode: true };
|
|
777
|
+
let originalIdentName;
|
|
778
|
+
if (t__namespace.isIdentifier(splitNode)) {
|
|
779
|
+
originalIdentName = splitNode.name;
|
|
780
|
+
}
|
|
433
781
|
while (t__namespace.isIdentifier(splitNode)) {
|
|
434
782
|
const binding = programPath.scope.getBinding(splitNode.name);
|
|
435
783
|
splitNode = binding?.path.node;
|
|
@@ -467,6 +815,11 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
467
815
|
if (t__namespace.isIdentifier(splitNode.id)) {
|
|
468
816
|
splitMeta.localExporterIdent = splitNode.id.name;
|
|
469
817
|
splitMeta.shouldRemoveNode = false;
|
|
818
|
+
} else if (t__namespace.isObjectPattern(splitNode.id)) {
|
|
819
|
+
if (originalIdentName) {
|
|
820
|
+
splitMeta.localExporterIdent = originalIdentName;
|
|
821
|
+
}
|
|
822
|
+
splitMeta.shouldRemoveNode = false;
|
|
470
823
|
} else {
|
|
471
824
|
throw new Error(
|
|
472
825
|
`Unexpected splitNode type ☝️: ${splitNode.type}`
|
|
@@ -552,13 +905,41 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
552
905
|
ExportNamedDeclaration(path) {
|
|
553
906
|
if (path.node.declaration) {
|
|
554
907
|
if (t__namespace.isVariableDeclaration(path.node.declaration)) {
|
|
908
|
+
const specifiers = path.node.declaration.declarations.flatMap(
|
|
909
|
+
(decl) => {
|
|
910
|
+
if (t__namespace.isIdentifier(decl.id)) {
|
|
911
|
+
return [
|
|
912
|
+
t__namespace.importSpecifier(
|
|
913
|
+
t__namespace.identifier(decl.id.name),
|
|
914
|
+
t__namespace.identifier(decl.id.name)
|
|
915
|
+
)
|
|
916
|
+
];
|
|
917
|
+
}
|
|
918
|
+
if (t__namespace.isObjectPattern(decl.id)) {
|
|
919
|
+
return collectIdentifiersFromPattern(decl.id).map(
|
|
920
|
+
(name) => t__namespace.importSpecifier(
|
|
921
|
+
t__namespace.identifier(name),
|
|
922
|
+
t__namespace.identifier(name)
|
|
923
|
+
)
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
if (t__namespace.isArrayPattern(decl.id)) {
|
|
927
|
+
return collectIdentifiersFromPattern(decl.id).map(
|
|
928
|
+
(name) => t__namespace.importSpecifier(
|
|
929
|
+
t__namespace.identifier(name),
|
|
930
|
+
t__namespace.identifier(name)
|
|
931
|
+
)
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
return [];
|
|
935
|
+
}
|
|
936
|
+
);
|
|
937
|
+
if (specifiers.length === 0) {
|
|
938
|
+
path.remove();
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
555
941
|
const importDecl = t__namespace.importDeclaration(
|
|
556
|
-
|
|
557
|
-
(decl) => t__namespace.importSpecifier(
|
|
558
|
-
t__namespace.identifier(decl.id.name),
|
|
559
|
-
t__namespace.identifier(decl.id.name)
|
|
560
|
-
)
|
|
561
|
-
),
|
|
942
|
+
specifiers,
|
|
562
943
|
t__namespace.stringLiteral(
|
|
563
944
|
removeSplitSearchParamFromFilename(opts.filename)
|
|
564
945
|
)
|
|
@@ -575,10 +956,121 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
575
956
|
}
|
|
576
957
|
}
|
|
577
958
|
});
|
|
959
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0) {
|
|
960
|
+
const sharedImportSpecifiers = [...opts.sharedBindings].map(
|
|
961
|
+
(name) => t__namespace.importSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
|
|
962
|
+
);
|
|
963
|
+
const sharedModuleUrl = addSharedSearchParamToFilename(
|
|
964
|
+
removeSplitSearchParamFromFilename(opts.filename)
|
|
965
|
+
);
|
|
966
|
+
const [sharedImportPath] = programPath.unshiftContainer(
|
|
967
|
+
"body",
|
|
968
|
+
t__namespace.importDeclaration(
|
|
969
|
+
sharedImportSpecifiers,
|
|
970
|
+
t__namespace.stringLiteral(sharedModuleUrl)
|
|
971
|
+
)
|
|
972
|
+
);
|
|
973
|
+
sharedImportPath.traverse({
|
|
974
|
+
Identifier(identPath) {
|
|
975
|
+
if (identPath.parentPath.isImportSpecifier() && identPath.key === "local") {
|
|
976
|
+
refIdents.add(identPath);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
routerUtils.deadCodeElimination(ast, refIdents);
|
|
985
|
+
{
|
|
986
|
+
const locallyBound = /* @__PURE__ */ new Set();
|
|
987
|
+
for (const stmt of ast.program.body) {
|
|
988
|
+
collectLocalBindingsFromStatement(stmt, locallyBound);
|
|
989
|
+
}
|
|
990
|
+
ast.program.body = ast.program.body.filter((stmt) => {
|
|
991
|
+
if (!t__namespace.isExpressionStatement(stmt)) return true;
|
|
992
|
+
const refs = collectIdentifiersFromNode(stmt);
|
|
993
|
+
return [...refs].some((name) => locallyBound.has(name));
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
if (ast.program.body.length === 0) {
|
|
997
|
+
ast.program.directives = [];
|
|
998
|
+
}
|
|
999
|
+
return routerUtils.generateFromAst(ast, {
|
|
1000
|
+
sourceMaps: true,
|
|
1001
|
+
sourceFileName: opts.filename,
|
|
1002
|
+
filename: opts.filename
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
function compileCodeSplitSharedRoute(opts) {
|
|
1006
|
+
const ast = routerUtils.parseAst(opts);
|
|
1007
|
+
const refIdents = routerUtils.findReferencedIdentifiers(ast);
|
|
1008
|
+
const localBindings = /* @__PURE__ */ new Set();
|
|
1009
|
+
for (const node of ast.program.body) {
|
|
1010
|
+
collectLocalBindingsFromStatement(node, localBindings);
|
|
1011
|
+
}
|
|
1012
|
+
localBindings.delete("Route");
|
|
1013
|
+
const declMap = buildDeclarationMap(ast);
|
|
1014
|
+
const depGraph = buildDependencyGraph(declMap, localBindings);
|
|
1015
|
+
const keepBindings = new Set(opts.sharedBindings);
|
|
1016
|
+
keepBindings.delete("Route");
|
|
1017
|
+
expandTransitively(keepBindings, depGraph);
|
|
1018
|
+
ast.program.body = ast.program.body.filter((stmt) => {
|
|
1019
|
+
if (t__namespace.isImportDeclaration(stmt)) return true;
|
|
1020
|
+
const decl = t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
1021
|
+
if (t__namespace.isVariableDeclaration(decl)) {
|
|
1022
|
+
decl.declarations = decl.declarations.filter((declarator) => {
|
|
1023
|
+
const names = collectIdentifiersFromPattern(declarator.id);
|
|
1024
|
+
return names.some((n) => keepBindings.has(n));
|
|
1025
|
+
});
|
|
1026
|
+
if (decl.declarations.length === 0) return false;
|
|
1027
|
+
if (t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration) {
|
|
1028
|
+
return true;
|
|
578
1029
|
}
|
|
1030
|
+
return true;
|
|
1031
|
+
} else if (t__namespace.isFunctionDeclaration(decl) && decl.id) {
|
|
1032
|
+
return keepBindings.has(decl.id.name);
|
|
1033
|
+
} else if (t__namespace.isClassDeclaration(decl) && decl.id) {
|
|
1034
|
+
return keepBindings.has(decl.id.name);
|
|
1035
|
+
}
|
|
1036
|
+
return false;
|
|
1037
|
+
});
|
|
1038
|
+
ast.program.body = ast.program.body.map((stmt) => {
|
|
1039
|
+
if (t__namespace.isExportNamedDeclaration(stmt) && stmt.declaration) {
|
|
1040
|
+
return stmt.declaration;
|
|
579
1041
|
}
|
|
1042
|
+
return stmt;
|
|
580
1043
|
});
|
|
1044
|
+
const exportNames = [...opts.sharedBindings].sort(
|
|
1045
|
+
(a, b) => a.localeCompare(b)
|
|
1046
|
+
);
|
|
1047
|
+
const exportSpecifiers = exportNames.map(
|
|
1048
|
+
(name) => t__namespace.exportSpecifier(t__namespace.identifier(name), t__namespace.identifier(name))
|
|
1049
|
+
);
|
|
1050
|
+
if (exportSpecifiers.length > 0) {
|
|
1051
|
+
const exportDecl = t__namespace.exportNamedDeclaration(null, exportSpecifiers);
|
|
1052
|
+
ast.program.body.push(exportDecl);
|
|
1053
|
+
babel.traverse(ast, {
|
|
1054
|
+
Program(programPath) {
|
|
1055
|
+
const bodyPaths = programPath.get("body");
|
|
1056
|
+
const last = bodyPaths[bodyPaths.length - 1];
|
|
1057
|
+
if (last && last.isExportNamedDeclaration()) {
|
|
1058
|
+
last.traverse({
|
|
1059
|
+
Identifier(identPath) {
|
|
1060
|
+
if (identPath.parentPath.isExportSpecifier() && identPath.key === "local") {
|
|
1061
|
+
refIdents.add(identPath);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
programPath.stop();
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
581
1070
|
routerUtils.deadCodeElimination(ast, refIdents);
|
|
1071
|
+
if (ast.program.body.length === 0) {
|
|
1072
|
+
ast.program.directives = [];
|
|
1073
|
+
}
|
|
582
1074
|
return routerUtils.generateFromAst(ast, {
|
|
583
1075
|
sourceMaps: true,
|
|
584
1076
|
sourceFileName: opts.filename,
|
|
@@ -678,6 +1170,37 @@ function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
|
678
1170
|
});
|
|
679
1171
|
return { specifier, path };
|
|
680
1172
|
}
|
|
1173
|
+
function collectIdentifiersFromPattern(node) {
|
|
1174
|
+
if (!node) {
|
|
1175
|
+
return [];
|
|
1176
|
+
}
|
|
1177
|
+
if (t__namespace.isIdentifier(node)) {
|
|
1178
|
+
return [node.name];
|
|
1179
|
+
}
|
|
1180
|
+
if (t__namespace.isAssignmentPattern(node)) {
|
|
1181
|
+
return collectIdentifiersFromPattern(node.left);
|
|
1182
|
+
}
|
|
1183
|
+
if (t__namespace.isRestElement(node)) {
|
|
1184
|
+
return collectIdentifiersFromPattern(node.argument);
|
|
1185
|
+
}
|
|
1186
|
+
if (t__namespace.isObjectPattern(node)) {
|
|
1187
|
+
return node.properties.flatMap((prop) => {
|
|
1188
|
+
if (t__namespace.isObjectProperty(prop)) {
|
|
1189
|
+
return collectIdentifiersFromPattern(prop.value);
|
|
1190
|
+
}
|
|
1191
|
+
if (t__namespace.isRestElement(prop)) {
|
|
1192
|
+
return collectIdentifiersFromPattern(prop.argument);
|
|
1193
|
+
}
|
|
1194
|
+
return [];
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
if (t__namespace.isArrayPattern(node)) {
|
|
1198
|
+
return node.elements.flatMap(
|
|
1199
|
+
(element) => collectIdentifiersFromPattern(element)
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
return [];
|
|
1203
|
+
}
|
|
681
1204
|
function resolveIdentifier(path, node) {
|
|
682
1205
|
if (t__namespace.isIdentifier(node)) {
|
|
683
1206
|
const binding = path.scope.getBinding(node.name);
|
|
@@ -696,6 +1219,25 @@ function resolveIdentifier(path, node) {
|
|
|
696
1219
|
function removeIdentifierLiteral(path, node) {
|
|
697
1220
|
const binding = path.scope.getBinding(node.name);
|
|
698
1221
|
if (binding) {
|
|
1222
|
+
if (t__namespace.isVariableDeclarator(binding.path.node) && t__namespace.isObjectPattern(binding.path.node.id)) {
|
|
1223
|
+
const objectPattern = binding.path.node.id;
|
|
1224
|
+
objectPattern.properties = objectPattern.properties.filter((prop) => {
|
|
1225
|
+
if (!t__namespace.isObjectProperty(prop)) {
|
|
1226
|
+
return true;
|
|
1227
|
+
}
|
|
1228
|
+
if (t__namespace.isIdentifier(prop.value) && prop.value.name === node.name) {
|
|
1229
|
+
return false;
|
|
1230
|
+
}
|
|
1231
|
+
if (t__namespace.isAssignmentPattern(prop.value) && t__namespace.isIdentifier(prop.value.left) && prop.value.left.name === node.name) {
|
|
1232
|
+
return false;
|
|
1233
|
+
}
|
|
1234
|
+
return true;
|
|
1235
|
+
});
|
|
1236
|
+
if (objectPattern.properties.length === 0) {
|
|
1237
|
+
binding.path.remove();
|
|
1238
|
+
}
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
699
1241
|
binding.path.remove();
|
|
700
1242
|
}
|
|
701
1243
|
}
|
|
@@ -711,6 +1253,11 @@ function hasExport(ast, node) {
|
|
|
711
1253
|
if (decl.id.name === node.name) {
|
|
712
1254
|
found = true;
|
|
713
1255
|
}
|
|
1256
|
+
} else if (t__namespace.isObjectPattern(decl.id) || t__namespace.isArrayPattern(decl.id)) {
|
|
1257
|
+
const names = collectIdentifiersFromPattern(decl.id);
|
|
1258
|
+
if (names.includes(node.name)) {
|
|
1259
|
+
found = true;
|
|
1260
|
+
}
|
|
714
1261
|
}
|
|
715
1262
|
}
|
|
716
1263
|
});
|
|
@@ -754,6 +1301,12 @@ function removeExports(ast, node) {
|
|
|
754
1301
|
path.remove();
|
|
755
1302
|
removed = true;
|
|
756
1303
|
}
|
|
1304
|
+
} else if (t__namespace.isObjectPattern(decl.id) || t__namespace.isArrayPattern(decl.id)) {
|
|
1305
|
+
const names = collectIdentifiersFromPattern(decl.id);
|
|
1306
|
+
if (names.includes(node.name)) {
|
|
1307
|
+
path.remove();
|
|
1308
|
+
removed = true;
|
|
1309
|
+
}
|
|
757
1310
|
}
|
|
758
1311
|
}
|
|
759
1312
|
});
|
|
@@ -785,7 +1338,19 @@ function removeExports(ast, node) {
|
|
|
785
1338
|
});
|
|
786
1339
|
return removed;
|
|
787
1340
|
}
|
|
1341
|
+
exports.addSharedSearchParamToFilename = addSharedSearchParamToFilename;
|
|
1342
|
+
exports.buildDeclarationMap = buildDeclarationMap;
|
|
1343
|
+
exports.buildDependencyGraph = buildDependencyGraph;
|
|
1344
|
+
exports.collectIdentifiersFromNode = collectIdentifiersFromNode;
|
|
1345
|
+
exports.collectLocalBindingsFromStatement = collectLocalBindingsFromStatement;
|
|
1346
|
+
exports.collectModuleLevelRefsFromNode = collectModuleLevelRefsFromNode;
|
|
788
1347
|
exports.compileCodeSplitReferenceRoute = compileCodeSplitReferenceRoute;
|
|
1348
|
+
exports.compileCodeSplitSharedRoute = compileCodeSplitSharedRoute;
|
|
789
1349
|
exports.compileCodeSplitVirtualRoute = compileCodeSplitVirtualRoute;
|
|
1350
|
+
exports.computeSharedBindings = computeSharedBindings;
|
|
790
1351
|
exports.detectCodeSplitGroupingsFromRoute = detectCodeSplitGroupingsFromRoute;
|
|
1352
|
+
exports.expandDestructuredDeclarations = expandDestructuredDeclarations;
|
|
1353
|
+
exports.expandSharedDestructuredDeclarators = expandSharedDestructuredDeclarators;
|
|
1354
|
+
exports.expandTransitively = expandTransitively;
|
|
1355
|
+
exports.removeBindingsDependingOnRoute = removeBindingsDependingOnRoute;
|
|
791
1356
|
//# sourceMappingURL=compilers.cjs.map
|