@tanstack/router-plugin 1.168.6 → 1.168.7
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 +32 -233
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +2 -57
- package/dist/cjs/core/code-splitter/plugins.d.cts +1 -0
- package/dist/cjs/core/config.cjs +1 -1
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/config.d.cts +44 -163
- package/dist/cjs/core/router-code-splitter-plugin.cjs +5 -6
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/esbuild.d.cts +26 -26
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/vite.d.cts +26 -26
- package/dist/esm/core/code-splitter/compilers.d.ts +2 -57
- package/dist/esm/core/code-splitter/compilers.js +15 -216
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins.d.ts +1 -0
- package/dist/esm/core/config.d.ts +44 -163
- package/dist/esm/core/config.js +1 -1
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +5 -6
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/esbuild.d.ts +26 -26
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/vite.d.ts +26 -26
- package/package.json +7 -7
- package/src/core/code-splitter/compilers.ts +51 -411
- package/src/core/code-splitter/plugins.ts +3 -0
- package/src/core/config.ts +12 -1
- package/src/core/router-code-splitter-plugin.ts +11 -9
- package/src/index.ts +5 -0
- package/dist/cjs/core/code-splitter/path-ids.cjs +0 -32
- package/dist/cjs/core/code-splitter/path-ids.cjs.map +0 -1
- package/dist/cjs/core/code-splitter/path-ids.d.cts +0 -2
- package/dist/esm/core/code-splitter/path-ids.d.ts +0 -2
- package/dist/esm/core/code-splitter/path-ids.js +0 -31
- package/dist/esm/core/code-splitter/path-ids.js.map +0 -1
- package/src/core/code-splitter/path-ids.ts +0 -39
|
@@ -1,28 +1,9 @@
|
|
|
1
1
|
import { CompileCodeSplitReferenceRouteOptions, ReferenceRouteCompilerPlugin } from './plugins.js';
|
|
2
2
|
import { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils';
|
|
3
3
|
import { CodeSplitGroupings, SplitRouteIdentNodes } from '../constants.js';
|
|
4
|
-
|
|
4
|
+
export { buildDeclarationMap, buildDependencyGraph, collectIdentifiersFromNode, collectLocalBindingsFromStatement, collectModuleLevelRefsFromNode, expandDestructuredDeclarations, expandSharedDestructuredDeclarators, expandTransitively, removeBindingsTransitivelyDependingOn, } from '@tanstack/router-utils';
|
|
5
|
+
export declare function removeBindingsDependingOnRoute(bindings: Set<string>, dependencyGraph: Map<string, Set<string>>): void;
|
|
5
6
|
export declare function addSharedSearchParamToFilename(filename: string): string;
|
|
6
|
-
/**
|
|
7
|
-
* Recursively walk an AST node and collect referenced identifier-like names.
|
|
8
|
-
* Much cheaper than babel.traverse — no path/scope overhead.
|
|
9
|
-
*
|
|
10
|
-
* Notes:
|
|
11
|
-
* - Uses @babel/types `isReferenced` to avoid collecting non-references like
|
|
12
|
-
* object keys, member expression properties, or binding identifiers.
|
|
13
|
-
* - Also handles JSX identifiers for component references.
|
|
14
|
-
*/
|
|
15
|
-
export declare function collectIdentifiersFromNode(node: t.Node): Set<string>;
|
|
16
|
-
/**
|
|
17
|
-
* Build a map from binding name → declaration AST node for all
|
|
18
|
-
* locally-declared module-level bindings. Built once, O(1) lookup.
|
|
19
|
-
*/
|
|
20
|
-
export declare function buildDeclarationMap(ast: t.File): Map<string, t.Node>;
|
|
21
|
-
/**
|
|
22
|
-
* Build a dependency graph: for each local binding, the set of other local
|
|
23
|
-
* bindings its declaration references. Built once via simple node walking.
|
|
24
|
-
*/
|
|
25
|
-
export declare function buildDependencyGraph(declMap: Map<string, t.Node>, localBindings: Set<string>): Map<string, Set<string>>;
|
|
26
7
|
/**
|
|
27
8
|
* Computes module-level bindings that are shared between split and non-split
|
|
28
9
|
* route properties. These bindings need to be extracted into a shared virtual
|
|
@@ -37,42 +18,6 @@ export declare function computeSharedBindings(opts: {
|
|
|
37
18
|
filename?: string;
|
|
38
19
|
codeSplitGroupings: CodeSplitGroupings;
|
|
39
20
|
}): Set<string>;
|
|
40
|
-
/**
|
|
41
|
-
* If bindings from the same destructured declarator are referenced by
|
|
42
|
-
* different groups, mark all bindings from that declarator as shared.
|
|
43
|
-
*/
|
|
44
|
-
export declare function expandSharedDestructuredDeclarators(ast: t.File, refsByGroup: Map<string, Set<number>>, shared: Set<string>): void;
|
|
45
|
-
/**
|
|
46
|
-
* Collect locally-declared module-level binding names from a statement.
|
|
47
|
-
* Pure node inspection, no traversal.
|
|
48
|
-
*/
|
|
49
|
-
export declare function collectLocalBindingsFromStatement(node: t.Statement | t.ModuleDeclaration, bindings: Set<string>): void;
|
|
50
|
-
/**
|
|
51
|
-
* Collect direct module-level binding names referenced from a given AST node.
|
|
52
|
-
* Uses a simple recursive walk instead of babel.traverse.
|
|
53
|
-
*/
|
|
54
|
-
export declare function collectModuleLevelRefsFromNode(node: t.Node, localModuleLevelBindings: Set<string>): Set<string>;
|
|
55
|
-
/**
|
|
56
|
-
* Expand the shared set transitively using a prebuilt dependency graph.
|
|
57
|
-
* No AST traversals — pure graph BFS.
|
|
58
|
-
*/
|
|
59
|
-
export declare function expandTransitively(shared: Set<string>, depGraph: Map<string, Set<string>>): void;
|
|
60
|
-
/**
|
|
61
|
-
* Remove any bindings from `shared` that transitively depend on `Route`.
|
|
62
|
-
* The Route singleton must remain in the reference file; if a shared binding
|
|
63
|
-
* references it (directly or transitively), extracting that binding would
|
|
64
|
-
* duplicate Route in the shared module.
|
|
65
|
-
*
|
|
66
|
-
* Uses `depGraph` which must include `Route` as a node so the dependency
|
|
67
|
-
* chain is visible.
|
|
68
|
-
*/
|
|
69
|
-
export declare function removeBindingsDependingOnRoute(shared: Set<string>, depGraph: Map<string, Set<string>>): void;
|
|
70
|
-
/**
|
|
71
|
-
* If any binding from a destructured declaration is shared,
|
|
72
|
-
* ensure all bindings from that same declaration are also shared.
|
|
73
|
-
* Pure node inspection of program.body, no traversal.
|
|
74
|
-
*/
|
|
75
|
-
export declare function expandDestructuredDeclarations(ast: t.File, shared: Set<string>): void;
|
|
76
21
|
export declare function compileCodeSplitReferenceRoute(opts: ParseAstOptions & CompileCodeSplitReferenceRouteOptions & {
|
|
77
22
|
compilerPlugins?: Array<ReferenceRouteCompilerPlugin>;
|
|
78
23
|
}): GeneratorResult | null;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { tsrShared, tsrSplit } from "../constants.js";
|
|
2
2
|
import { createRouteHmrStatement } from "../hmr/select-adapter.js";
|
|
3
3
|
import { getObjectPropertyKeyName } from "../utils.js";
|
|
4
|
-
import { createIdentifier } from "./path-ids.js";
|
|
5
4
|
import { getFrameworkOptions } from "./framework-options.js";
|
|
6
|
-
import { deadCodeElimination, findReferencedIdentifiers, generateFromAst, parseAst } from "@tanstack/router-utils";
|
|
5
|
+
import { buildDeclarationMap, buildDependencyGraph, collectIdentifiersFromPattern, collectLocalBindingsFromStatement, collectModuleLevelRefsFromNode, createIdentifier, deadCodeElimination, expandDestructuredDeclarations, expandSharedDestructuredDeclarators, expandTransitively, findReferencedIdentifiers, generateFromAst, parseAst, removeBindingsTransitivelyDependingOn, retainModuleLevelDeclarations, stripUnreferencedTopLevelExpressionStatements, unwrapExportedDeclarations } from "@tanstack/router-utils";
|
|
7
6
|
import * as t from "@babel/types";
|
|
8
7
|
import * as babel from "@babel/core";
|
|
9
8
|
import * as template from "@babel/template";
|
|
@@ -64,69 +63,6 @@ var splittableCreateRouteFns = ["createFileRoute"];
|
|
|
64
63
|
var unsplittableCreateRouteFns = ["createRootRoute", "createRootRouteWithContext"];
|
|
65
64
|
var allCreateRouteFns = [...splittableCreateRouteFns, ...unsplittableCreateRouteFns];
|
|
66
65
|
/**
|
|
67
|
-
* Recursively walk an AST node and collect referenced identifier-like names.
|
|
68
|
-
* Much cheaper than babel.traverse — no path/scope overhead.
|
|
69
|
-
*
|
|
70
|
-
* Notes:
|
|
71
|
-
* - Uses @babel/types `isReferenced` to avoid collecting non-references like
|
|
72
|
-
* object keys, member expression properties, or binding identifiers.
|
|
73
|
-
* - Also handles JSX identifiers for component references.
|
|
74
|
-
*/
|
|
75
|
-
function collectIdentifiersFromNode(node) {
|
|
76
|
-
const ids = /* @__PURE__ */ new Set();
|
|
77
|
-
(function walk(n, parent, grandparent, parentKey) {
|
|
78
|
-
if (!n) return;
|
|
79
|
-
if (t.isIdentifier(n)) {
|
|
80
|
-
if (!parent || t.isReferenced(n, parent, grandparent)) ids.add(n.name);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (t.isJSXIdentifier(n)) {
|
|
84
|
-
if (parent && t.isJSXAttribute(parent) && parentKey === "name") return;
|
|
85
|
-
if (parent && t.isJSXMemberExpression(parent) && parentKey === "property") return;
|
|
86
|
-
const first = n.name[0];
|
|
87
|
-
if (first && first === first.toLowerCase()) return;
|
|
88
|
-
ids.add(n.name);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
for (const key of t.VISITOR_KEYS[n.type] || []) {
|
|
92
|
-
const child = n[key];
|
|
93
|
-
if (Array.isArray(child)) {
|
|
94
|
-
for (const c of child) if (c && typeof c.type === "string") walk(c, n, parent, key);
|
|
95
|
-
} else if (child && typeof child.type === "string") walk(child, n, parent, key);
|
|
96
|
-
}
|
|
97
|
-
})(node);
|
|
98
|
-
return ids;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Build a map from binding name → declaration AST node for all
|
|
102
|
-
* locally-declared module-level bindings. Built once, O(1) lookup.
|
|
103
|
-
*/
|
|
104
|
-
function buildDeclarationMap(ast) {
|
|
105
|
-
const map = /* @__PURE__ */ new Map();
|
|
106
|
-
for (const stmt of ast.program.body) {
|
|
107
|
-
const decl = t.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
108
|
-
if (t.isVariableDeclaration(decl)) for (const declarator of decl.declarations) for (const name of collectIdentifiersFromPattern(declarator.id)) map.set(name, declarator);
|
|
109
|
-
else if (t.isFunctionDeclaration(decl) && decl.id) map.set(decl.id.name, decl);
|
|
110
|
-
else if (t.isClassDeclaration(decl) && decl.id) map.set(decl.id.name, decl);
|
|
111
|
-
}
|
|
112
|
-
return map;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Build a dependency graph: for each local binding, the set of other local
|
|
116
|
-
* bindings its declaration references. Built once via simple node walking.
|
|
117
|
-
*/
|
|
118
|
-
function buildDependencyGraph(declMap, localBindings) {
|
|
119
|
-
const graph = /* @__PURE__ */ new Map();
|
|
120
|
-
for (const [name, declNode] of declMap) {
|
|
121
|
-
if (!localBindings.has(name)) continue;
|
|
122
|
-
const allIds = collectIdentifiersFromNode(declNode);
|
|
123
|
-
const deps = /* @__PURE__ */ new Set();
|
|
124
|
-
for (const id of allIds) if (id !== name && localBindings.has(id)) deps.add(id);
|
|
125
|
-
graph.set(name, deps);
|
|
126
|
-
}
|
|
127
|
-
return graph;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
66
|
* Computes module-level bindings that are shared between split and non-split
|
|
131
67
|
* route properties. These bindings need to be extracted into a shared virtual
|
|
132
68
|
* module to avoid double-initialization.
|
|
@@ -202,117 +138,10 @@ function computeSharedBindings(opts) {
|
|
|
202
138
|
expandSharedDestructuredDeclarators(ast, refsByGroup, shared);
|
|
203
139
|
if (shared.size === 0) return shared;
|
|
204
140
|
expandDestructuredDeclarations(ast, shared);
|
|
205
|
-
|
|
141
|
+
removeBindingsTransitivelyDependingOn(shared, fullDepGraph, ["Route"]);
|
|
206
142
|
return shared;
|
|
207
143
|
}
|
|
208
144
|
/**
|
|
209
|
-
* If bindings from the same destructured declarator are referenced by
|
|
210
|
-
* different groups, mark all bindings from that declarator as shared.
|
|
211
|
-
*/
|
|
212
|
-
function expandSharedDestructuredDeclarators(ast, refsByGroup, shared) {
|
|
213
|
-
for (const stmt of ast.program.body) {
|
|
214
|
-
const decl = t.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
215
|
-
if (!t.isVariableDeclaration(decl)) continue;
|
|
216
|
-
for (const declarator of decl.declarations) {
|
|
217
|
-
if (!t.isObjectPattern(declarator.id) && !t.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
|
-
}
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Collect locally-declared module-level binding names from a statement.
|
|
231
|
-
* Pure node inspection, no traversal.
|
|
232
|
-
*/
|
|
233
|
-
function collectLocalBindingsFromStatement(node, bindings) {
|
|
234
|
-
const decl = t.isExportNamedDeclaration(node) && node.declaration ? node.declaration : node;
|
|
235
|
-
if (t.isVariableDeclaration(decl)) for (const declarator of decl.declarations) for (const name of collectIdentifiersFromPattern(declarator.id)) bindings.add(name);
|
|
236
|
-
else if (t.isFunctionDeclaration(decl) && decl.id) bindings.add(decl.id.name);
|
|
237
|
-
else if (t.isClassDeclaration(decl) && decl.id) bindings.add(decl.id.name);
|
|
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
|
-
*/
|
|
243
|
-
function collectModuleLevelRefsFromNode(node, localModuleLevelBindings) {
|
|
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;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Expand the shared set transitively using a prebuilt dependency graph.
|
|
251
|
-
* No AST traversals — pure graph BFS.
|
|
252
|
-
*/
|
|
253
|
-
function expandTransitively(shared, depGraph) {
|
|
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
|
-
}
|
|
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
|
-
*/
|
|
277
|
-
function removeBindingsDependingOnRoute(shared, depGraph) {
|
|
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);
|
|
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
|
-
*/
|
|
304
|
-
function expandDestructuredDeclarations(ast, shared) {
|
|
305
|
-
for (const stmt of ast.program.body) {
|
|
306
|
-
const decl = t.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
307
|
-
if (!t.isVariableDeclaration(decl)) continue;
|
|
308
|
-
for (const declarator of decl.declarations) {
|
|
309
|
-
if (!t.isObjectPattern(declarator.id) && !t.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
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
145
|
* Find which shared bindings are user-exported in the original source.
|
|
317
146
|
* These need to be re-exported from the shared module.
|
|
318
147
|
*/
|
|
@@ -408,6 +237,16 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
408
237
|
};
|
|
409
238
|
if (t.isObjectExpression(routeOptions)) {
|
|
410
239
|
const insertionPath = path.getStatementParent() ?? path;
|
|
240
|
+
opts.compilerPlugins?.forEach((plugin) => {
|
|
241
|
+
if ((plugin.onRouteOptions?.({
|
|
242
|
+
programPath,
|
|
243
|
+
callExpressionPath: path,
|
|
244
|
+
insertionPath,
|
|
245
|
+
routeOptions,
|
|
246
|
+
createRouteFn,
|
|
247
|
+
opts
|
|
248
|
+
}))?.modified) modified = true;
|
|
249
|
+
});
|
|
411
250
|
if (opts.deleteNodes && opts.deleteNodes.size > 0) routeOptions.properties = routeOptions.properties.filter((prop) => {
|
|
412
251
|
if (t.isObjectProperty(prop)) {
|
|
413
252
|
const key = getObjectPropertyKeyName(prop);
|
|
@@ -683,14 +522,7 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
683
522
|
}
|
|
684
523
|
} } });
|
|
685
524
|
deadCodeElimination(ast, refIdents);
|
|
686
|
-
|
|
687
|
-
const locallyBound = /* @__PURE__ */ new Set();
|
|
688
|
-
for (const stmt of ast.program.body) collectLocalBindingsFromStatement(stmt, locallyBound);
|
|
689
|
-
ast.program.body = ast.program.body.filter((stmt) => {
|
|
690
|
-
if (!t.isExpressionStatement(stmt)) return true;
|
|
691
|
-
return [...collectIdentifiersFromNode(stmt)].some((name) => locallyBound.has(name));
|
|
692
|
-
});
|
|
693
|
-
}
|
|
525
|
+
stripUnreferencedTopLevelExpressionStatements(ast);
|
|
694
526
|
if (ast.program.body.length === 0) ast.program.directives = [];
|
|
695
527
|
const result = generateFromAst(ast, {
|
|
696
528
|
sourceMaps: true,
|
|
@@ -715,24 +547,8 @@ function compileCodeSplitSharedRoute(opts) {
|
|
|
715
547
|
const keepBindings = new Set(opts.sharedBindings);
|
|
716
548
|
keepBindings.delete("Route");
|
|
717
549
|
expandTransitively(keepBindings, depGraph);
|
|
718
|
-
ast
|
|
719
|
-
|
|
720
|
-
const decl = t.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
721
|
-
if (t.isVariableDeclaration(decl)) {
|
|
722
|
-
decl.declarations = decl.declarations.filter((declarator) => {
|
|
723
|
-
return collectIdentifiersFromPattern(declarator.id).some((n) => keepBindings.has(n));
|
|
724
|
-
});
|
|
725
|
-
if (decl.declarations.length === 0) return false;
|
|
726
|
-
if (t.isExportNamedDeclaration(stmt) && stmt.declaration) return true;
|
|
727
|
-
return true;
|
|
728
|
-
} else if (t.isFunctionDeclaration(decl) && decl.id) return keepBindings.has(decl.id.name);
|
|
729
|
-
else if (t.isClassDeclaration(decl) && decl.id) return keepBindings.has(decl.id.name);
|
|
730
|
-
return false;
|
|
731
|
-
});
|
|
732
|
-
ast.program.body = ast.program.body.map((stmt) => {
|
|
733
|
-
if (t.isExportNamedDeclaration(stmt) && stmt.declaration) return stmt.declaration;
|
|
734
|
-
return stmt;
|
|
735
|
-
});
|
|
550
|
+
retainModuleLevelDeclarations(ast, keepBindings);
|
|
551
|
+
unwrapExportedDeclarations(ast);
|
|
736
552
|
const exportSpecifiers = [...opts.sharedBindings].sort((a, b) => a.localeCompare(b)).map((name) => t.exportSpecifier(t.identifier(name), t.identifier(name)));
|
|
737
553
|
if (exportSpecifiers.length > 0) {
|
|
738
554
|
const exportDecl = t.exportNamedDeclaration(null, exportSpecifiers);
|
|
@@ -816,23 +632,6 @@ function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
|
816
632
|
path
|
|
817
633
|
};
|
|
818
634
|
}
|
|
819
|
-
/**
|
|
820
|
-
* Recursively collects all identifier names from a destructuring pattern
|
|
821
|
-
* (ObjectPattern, ArrayPattern, AssignmentPattern, RestElement).
|
|
822
|
-
*/
|
|
823
|
-
function collectIdentifiersFromPattern(node) {
|
|
824
|
-
if (!node) return [];
|
|
825
|
-
if (t.isIdentifier(node)) return [node.name];
|
|
826
|
-
if (t.isAssignmentPattern(node)) return collectIdentifiersFromPattern(node.left);
|
|
827
|
-
if (t.isRestElement(node)) return collectIdentifiersFromPattern(node.argument);
|
|
828
|
-
if (t.isObjectPattern(node)) return node.properties.flatMap((prop) => {
|
|
829
|
-
if (t.isObjectProperty(prop)) return collectIdentifiersFromPattern(prop.value);
|
|
830
|
-
if (t.isRestElement(prop)) return collectIdentifiersFromPattern(prop.argument);
|
|
831
|
-
return [];
|
|
832
|
-
});
|
|
833
|
-
if (t.isArrayPattern(node)) return node.elements.flatMap((element) => collectIdentifiersFromPattern(element));
|
|
834
|
-
return [];
|
|
835
|
-
}
|
|
836
635
|
function resolveIdentifier(path, node) {
|
|
837
636
|
if (t.isIdentifier(node)) {
|
|
838
637
|
const binding = path.scope.getBinding(node.name);
|