@tanstack/router-plugin 1.41.0 → 1.43.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/ast.cjs +19 -29
- package/dist/cjs/ast.cjs.map +1 -1
- package/dist/cjs/ast.d.cts +4 -19
- package/dist/cjs/code-splitter.cjs +8 -15
- package/dist/cjs/code-splitter.cjs.map +1 -1
- package/dist/cjs/compilers.cjs +299 -317
- package/dist/cjs/compilers.cjs.map +1 -1
- package/dist/cjs/compilers.d.cts +3 -17
- package/dist/esm/ast.d.ts +4 -19
- package/dist/esm/ast.js +19 -29
- package/dist/esm/ast.js.map +1 -1
- package/dist/esm/code-splitter.js +9 -16
- package/dist/esm/code-splitter.js.map +1 -1
- package/dist/esm/compilers.d.ts +3 -17
- package/dist/esm/compilers.js +299 -317
- package/dist/esm/compilers.js.map +1 -1
- package/package.json +5 -3
- package/src/ast.ts +21 -42
- package/src/code-splitter.ts +12 -18
- package/src/compilers.ts +413 -432
- package/dist/cjs/eliminateUnreferencedIdentifiers.cjs +0 -148
- package/dist/cjs/eliminateUnreferencedIdentifiers.cjs.map +0 -1
- package/dist/cjs/eliminateUnreferencedIdentifiers.d.cts +0 -9
- package/dist/esm/eliminateUnreferencedIdentifiers.d.ts +0 -9
- package/dist/esm/eliminateUnreferencedIdentifiers.js +0 -131
- package/dist/esm/eliminateUnreferencedIdentifiers.js.map +0 -1
- package/src/eliminateUnreferencedIdentifiers.ts +0 -211
package/dist/cjs/compilers.cjs
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const t = require("@babel/types");
|
|
4
4
|
const babel = require("@babel/core");
|
|
5
|
-
const
|
|
5
|
+
const _generate = require("@babel/generator");
|
|
6
6
|
const template = require("@babel/template");
|
|
7
|
+
const babelDeadCodeElimination = require("babel-dead-code-elimination");
|
|
7
8
|
const constants = require("./constants.cjs");
|
|
8
|
-
const
|
|
9
|
+
const ast = require("./ast.cjs");
|
|
9
10
|
function _interopNamespaceDefault(e) {
|
|
10
11
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
11
12
|
if (e) {
|
|
@@ -24,147 +25,314 @@ function _interopNamespaceDefault(e) {
|
|
|
24
25
|
}
|
|
25
26
|
const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
|
|
26
27
|
const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
let generate = _generate["default"];
|
|
29
|
+
if (!generate) {
|
|
30
|
+
generate = _generate;
|
|
31
|
+
}
|
|
32
|
+
function compileCodeSplitReferenceRoute(opts) {
|
|
33
|
+
const ast$1 = ast.parseAst(opts);
|
|
34
|
+
if (!ast$1) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Failed to compile ast for compileCodeSplitReferenceRoute() for the file: ${opts.filename}`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
babel.traverse(ast$1, {
|
|
40
|
+
Program: {
|
|
41
|
+
enter(programPath, programState) {
|
|
42
|
+
const state = programState;
|
|
43
|
+
const splitUrl = `${constants.splitPrefix}:${opts.filename}?${constants.splitPrefix}`;
|
|
44
|
+
let existingCompImportPath = null;
|
|
45
|
+
let existingLoaderImportPath = null;
|
|
46
|
+
programPath.traverse(
|
|
34
47
|
{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (prop.key.name === "component") {
|
|
64
|
-
const value = prop.value;
|
|
65
|
-
if (t__namespace.isIdentifier(value)) {
|
|
66
|
-
existingCompImportPath = getImportSpecifierAndPathFromLocalName(
|
|
67
|
-
programPath,
|
|
68
|
-
value.name
|
|
69
|
-
).path;
|
|
70
|
-
removeIdentifierLiteral(path, value);
|
|
71
|
-
}
|
|
72
|
-
if (!hasImportedOrDefinedIdentifier(
|
|
73
|
-
"lazyRouteComponent"
|
|
74
|
-
)) {
|
|
75
|
-
programPath.unshiftContainer("body", [
|
|
76
|
-
template__namespace.statement(
|
|
77
|
-
`import { lazyRouteComponent } from '@tanstack/react-router'`
|
|
78
|
-
)()
|
|
79
|
-
]);
|
|
80
|
-
}
|
|
81
|
-
if (!hasImportedOrDefinedIdentifier(
|
|
82
|
-
"$$splitComponentImporter"
|
|
83
|
-
)) {
|
|
84
|
-
programPath.unshiftContainer("body", [
|
|
85
|
-
template__namespace.statement(
|
|
86
|
-
`const $$splitComponentImporter = () => import('${splitUrl}')`
|
|
87
|
-
)()
|
|
88
|
-
]);
|
|
89
|
-
}
|
|
90
|
-
prop.value = template__namespace.expression(
|
|
91
|
-
`lazyRouteComponent($$splitComponentImporter, 'component')`
|
|
92
|
-
)();
|
|
93
|
-
programPath.pushContainer("body", [
|
|
94
|
-
template__namespace.statement(
|
|
95
|
-
`function DummyComponent() { return null }`
|
|
96
|
-
)()
|
|
97
|
-
]);
|
|
98
|
-
found = true;
|
|
99
|
-
} else if (prop.key.name === "loader") {
|
|
100
|
-
const value = prop.value;
|
|
101
|
-
if (t__namespace.isIdentifier(value)) {
|
|
102
|
-
existingLoaderImportPath = getImportSpecifierAndPathFromLocalName(
|
|
103
|
-
programPath,
|
|
104
|
-
value.name
|
|
105
|
-
).path;
|
|
106
|
-
removeIdentifierLiteral(path, value);
|
|
107
|
-
}
|
|
108
|
-
if (!hasImportedOrDefinedIdentifier("lazyFn")) {
|
|
109
|
-
programPath.unshiftContainer("body", [
|
|
110
|
-
template__namespace.smart(
|
|
111
|
-
`import { lazyFn } from '@tanstack/react-router'`
|
|
112
|
-
)()
|
|
113
|
-
]);
|
|
114
|
-
}
|
|
115
|
-
if (!hasImportedOrDefinedIdentifier(
|
|
116
|
-
"$$splitLoaderImporter"
|
|
117
|
-
)) {
|
|
118
|
-
programPath.unshiftContainer("body", [
|
|
119
|
-
template__namespace.statement(
|
|
120
|
-
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
121
|
-
)()
|
|
122
|
-
]);
|
|
123
|
-
}
|
|
124
|
-
prop.value = template__namespace.expression(
|
|
125
|
-
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
126
|
-
)();
|
|
127
|
-
found = true;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
programPath.scope.crawl();
|
|
132
|
-
});
|
|
48
|
+
CallExpression: (path) => {
|
|
49
|
+
if (!t__namespace.isIdentifier(path.node.callee)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (t__namespace.isCallExpression(path.parentPath.node)) {
|
|
56
|
+
const options = resolveIdentifier(
|
|
57
|
+
path,
|
|
58
|
+
path.parentPath.node.arguments[0]
|
|
59
|
+
);
|
|
60
|
+
let found = false;
|
|
61
|
+
const hasImportedOrDefinedIdentifier = (name) => {
|
|
62
|
+
return programPath.scope.hasBinding(name);
|
|
63
|
+
};
|
|
64
|
+
if (t__namespace.isObjectExpression(options)) {
|
|
65
|
+
options.properties.forEach((prop) => {
|
|
66
|
+
if (t__namespace.isObjectProperty(prop)) {
|
|
67
|
+
if (t__namespace.isIdentifier(prop.key)) {
|
|
68
|
+
if (prop.key.name === "component") {
|
|
69
|
+
const value = prop.value;
|
|
70
|
+
if (t__namespace.isIdentifier(value)) {
|
|
71
|
+
existingCompImportPath = getImportSpecifierAndPathFromLocalName(
|
|
72
|
+
programPath,
|
|
73
|
+
value.name
|
|
74
|
+
).path;
|
|
75
|
+
removeIdentifierLiteral(path, value);
|
|
133
76
|
}
|
|
134
|
-
if (
|
|
135
|
-
|
|
77
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
78
|
+
"lazyRouteComponent"
|
|
79
|
+
)) {
|
|
80
|
+
programPath.unshiftContainer("body", [
|
|
136
81
|
template__namespace.statement(
|
|
137
|
-
`
|
|
82
|
+
`import { lazyRouteComponent } from '@tanstack/react-router'`
|
|
138
83
|
)()
|
|
139
84
|
]);
|
|
140
85
|
}
|
|
86
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
87
|
+
"$$splitComponentImporter"
|
|
88
|
+
)) {
|
|
89
|
+
programPath.unshiftContainer("body", [
|
|
90
|
+
template__namespace.statement(
|
|
91
|
+
`const $$splitComponentImporter = () => import('${splitUrl}')`
|
|
92
|
+
)()
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
prop.value = template__namespace.expression(
|
|
96
|
+
`lazyRouteComponent($$splitComponentImporter, 'component')`
|
|
97
|
+
)();
|
|
98
|
+
programPath.pushContainer("body", [
|
|
99
|
+
template__namespace.statement(
|
|
100
|
+
`function DummyComponent() { return null }`
|
|
101
|
+
)()
|
|
102
|
+
]);
|
|
103
|
+
found = true;
|
|
104
|
+
} else if (prop.key.name === "loader") {
|
|
105
|
+
const value = prop.value;
|
|
106
|
+
if (t__namespace.isIdentifier(value)) {
|
|
107
|
+
existingLoaderImportPath = getImportSpecifierAndPathFromLocalName(
|
|
108
|
+
programPath,
|
|
109
|
+
value.name
|
|
110
|
+
).path;
|
|
111
|
+
removeIdentifierLiteral(path, value);
|
|
112
|
+
}
|
|
113
|
+
if (!hasImportedOrDefinedIdentifier("lazyFn")) {
|
|
114
|
+
programPath.unshiftContainer("body", [
|
|
115
|
+
template__namespace.smart(
|
|
116
|
+
`import { lazyFn } from '@tanstack/react-router'`
|
|
117
|
+
)()
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
121
|
+
"$$splitLoaderImporter"
|
|
122
|
+
)) {
|
|
123
|
+
programPath.unshiftContainer("body", [
|
|
124
|
+
template__namespace.statement(
|
|
125
|
+
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
126
|
+
)()
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
prop.value = template__namespace.expression(
|
|
130
|
+
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
131
|
+
)();
|
|
132
|
+
found = true;
|
|
141
133
|
}
|
|
142
134
|
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (path.node.source.value === existingCompImportPath || path.node.source.value === existingLoaderImportPath) {
|
|
152
|
-
path.remove();
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
}
|
|
135
|
+
}
|
|
136
|
+
programPath.scope.crawl();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (found) {
|
|
140
|
+
programPath.pushContainer("body", [
|
|
141
|
+
template__namespace.statement(`function TSR_Dummy_Component() {}`)()
|
|
142
|
+
]);
|
|
157
143
|
}
|
|
158
144
|
}
|
|
159
145
|
}
|
|
160
146
|
},
|
|
147
|
+
state
|
|
148
|
+
);
|
|
149
|
+
if (existingCompImportPath || existingLoaderImportPath) {
|
|
150
|
+
programPath.traverse({
|
|
151
|
+
ImportDeclaration(path) {
|
|
152
|
+
if (path.node.specifiers.length > 0) return;
|
|
153
|
+
if (path.node.source.value === existingCompImportPath || path.node.source.value === existingLoaderImportPath) {
|
|
154
|
+
path.remove();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
babelDeadCodeElimination.deadCodeElimination(ast$1);
|
|
163
|
+
return generate(ast$1, {
|
|
164
|
+
sourceMaps: true,
|
|
165
|
+
minified: process.env.NODE_ENV === "production"
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const splitNodeTypes = ["component", "loader"];
|
|
169
|
+
function compileCodeSplitVirtualRoute(opts) {
|
|
170
|
+
const ast$1 = ast.parseAst(opts);
|
|
171
|
+
if (!ast$1) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Failed to compile ast for compileCodeSplitVirtualRoute() for the file: ${opts.filename}`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
babel.traverse(ast$1, {
|
|
177
|
+
Program: {
|
|
178
|
+
enter(programPath, programState) {
|
|
179
|
+
const state = programState;
|
|
180
|
+
const splitNodesByType = {
|
|
181
|
+
component: void 0,
|
|
182
|
+
loader: void 0
|
|
183
|
+
};
|
|
184
|
+
programPath.traverse(
|
|
161
185
|
{
|
|
162
|
-
|
|
163
|
-
|
|
186
|
+
CallExpression: (path) => {
|
|
187
|
+
if (!t__namespace.isIdentifier(path.node.callee)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (t__namespace.isCallExpression(path.parentPath.node)) {
|
|
194
|
+
const options = resolveIdentifier(
|
|
195
|
+
path,
|
|
196
|
+
path.parentPath.node.arguments[0]
|
|
197
|
+
);
|
|
198
|
+
if (t__namespace.isObjectExpression(options)) {
|
|
199
|
+
options.properties.forEach((prop) => {
|
|
200
|
+
if (t__namespace.isObjectProperty(prop)) {
|
|
201
|
+
splitNodeTypes.forEach((type) => {
|
|
202
|
+
if (t__namespace.isIdentifier(prop.key)) {
|
|
203
|
+
if (prop.key.name === type) {
|
|
204
|
+
splitNodesByType[type] = prop.value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
options.properties = [];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
state
|
|
216
|
+
);
|
|
217
|
+
splitNodeTypes.forEach((splitType) => {
|
|
218
|
+
let splitNode = splitNodesByType[splitType];
|
|
219
|
+
if (!splitNode) {
|
|
220
|
+
return;
|
|
164
221
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
222
|
+
while (t__namespace.isIdentifier(splitNode)) {
|
|
223
|
+
const binding = programPath.scope.getBinding(splitNode.name);
|
|
224
|
+
splitNode = binding == null ? void 0 : binding.path.node;
|
|
225
|
+
}
|
|
226
|
+
if (splitNode) {
|
|
227
|
+
if (t__namespace.isFunctionDeclaration(splitNode)) {
|
|
228
|
+
programPath.pushContainer(
|
|
229
|
+
"body",
|
|
230
|
+
t__namespace.variableDeclaration("const", [
|
|
231
|
+
t__namespace.variableDeclarator(
|
|
232
|
+
t__namespace.identifier(splitType),
|
|
233
|
+
t__namespace.functionExpression(
|
|
234
|
+
splitNode.id || null,
|
|
235
|
+
// Anonymize the function expression
|
|
236
|
+
splitNode.params,
|
|
237
|
+
splitNode.body,
|
|
238
|
+
splitNode.generator,
|
|
239
|
+
splitNode.async
|
|
240
|
+
)
|
|
241
|
+
)
|
|
242
|
+
])
|
|
243
|
+
);
|
|
244
|
+
} else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
|
|
245
|
+
programPath.pushContainer(
|
|
246
|
+
"body",
|
|
247
|
+
t__namespace.variableDeclaration("const", [
|
|
248
|
+
t__namespace.variableDeclarator(
|
|
249
|
+
t__namespace.identifier(splitType),
|
|
250
|
+
splitNode
|
|
251
|
+
)
|
|
252
|
+
])
|
|
253
|
+
);
|
|
254
|
+
} else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
|
|
255
|
+
programPath.pushContainer(
|
|
256
|
+
"body",
|
|
257
|
+
t__namespace.variableDeclaration("const", [
|
|
258
|
+
t__namespace.variableDeclarator(
|
|
259
|
+
t__namespace.identifier(splitType),
|
|
260
|
+
splitNode.local
|
|
261
|
+
)
|
|
262
|
+
])
|
|
263
|
+
);
|
|
264
|
+
} else if (t__namespace.isCallExpression(splitNode)) {
|
|
265
|
+
const outputSplitNodeCode = generate(splitNode).code;
|
|
266
|
+
const splitNodeAst = babel.parse(outputSplitNodeCode);
|
|
267
|
+
if (!splitNodeAst) {
|
|
268
|
+
throw new Error(
|
|
269
|
+
`Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}"`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
const statement = splitNodeAst.program.body[0];
|
|
273
|
+
if (!statement) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}" as no statement was found in the program body`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (t__namespace.isExpressionStatement(statement)) {
|
|
279
|
+
const expression = statement.expression;
|
|
280
|
+
programPath.pushContainer(
|
|
281
|
+
"body",
|
|
282
|
+
t__namespace.variableDeclaration("const", [
|
|
283
|
+
t__namespace.variableDeclarator(t__namespace.identifier(splitType), expression)
|
|
284
|
+
])
|
|
285
|
+
);
|
|
286
|
+
} else {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`Unexpected expression type encounter for "${splitType}" in the node type "${splitNode.type}"`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
console.info("Unexpected splitNode type:", splitNode);
|
|
293
|
+
throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
programPath.node.body = programPath.node.body.filter((node) => {
|
|
297
|
+
return node !== splitNode;
|
|
298
|
+
});
|
|
299
|
+
programPath.pushContainer("body", [
|
|
300
|
+
t__namespace.exportNamedDeclaration(null, [
|
|
301
|
+
t__namespace.exportSpecifier(
|
|
302
|
+
t__namespace.identifier(splitType),
|
|
303
|
+
t__namespace.identifier(splitType)
|
|
304
|
+
)
|
|
305
|
+
])
|
|
306
|
+
]);
|
|
307
|
+
});
|
|
308
|
+
programPath.traverse({
|
|
309
|
+
ExportNamedDeclaration(path) {
|
|
310
|
+
if (path.node.declaration) {
|
|
311
|
+
if (t__namespace.isVariableDeclaration(path.node.declaration)) {
|
|
312
|
+
path.replaceWith(
|
|
313
|
+
t__namespace.importDeclaration(
|
|
314
|
+
path.node.declaration.declarations.map(
|
|
315
|
+
(decl) => t__namespace.importSpecifier(
|
|
316
|
+
t__namespace.identifier(decl.id.name),
|
|
317
|
+
t__namespace.identifier(decl.id.name)
|
|
318
|
+
)
|
|
319
|
+
),
|
|
320
|
+
t__namespace.stringLiteral(
|
|
321
|
+
opts.filename.split(`?${constants.splitPrefix}`)[0]
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
babelDeadCodeElimination.deadCodeElimination(ast$1);
|
|
333
|
+
return generate(ast$1, {
|
|
334
|
+
sourceMaps: true,
|
|
335
|
+
minified: process.env.NODE_ENV === "production"
|
|
168
336
|
});
|
|
169
337
|
}
|
|
170
338
|
function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
@@ -206,192 +374,6 @@ function removeIdentifierLiteral(path, node) {
|
|
|
206
374
|
}
|
|
207
375
|
}
|
|
208
376
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return await opts.compileAst({
|
|
212
|
-
code: opts.code,
|
|
213
|
-
filename: opts.filename,
|
|
214
|
-
getBabelConfig: () => ({
|
|
215
|
-
plugins: [
|
|
216
|
-
[
|
|
217
|
-
{
|
|
218
|
-
visitor: {
|
|
219
|
-
Program: {
|
|
220
|
-
enter(programPath, state) {
|
|
221
|
-
const splitNodesByType = {
|
|
222
|
-
component: void 0,
|
|
223
|
-
loader: void 0
|
|
224
|
-
};
|
|
225
|
-
programPath.traverse(
|
|
226
|
-
{
|
|
227
|
-
CallExpression: (path) => {
|
|
228
|
-
if (!t__namespace.isIdentifier(path.node.callee)) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
if (t__namespace.isCallExpression(path.parentPath.node)) {
|
|
235
|
-
const options = resolveIdentifier(
|
|
236
|
-
path,
|
|
237
|
-
path.parentPath.node.arguments[0]
|
|
238
|
-
);
|
|
239
|
-
if (t__namespace.isObjectExpression(options)) {
|
|
240
|
-
options.properties.forEach((prop) => {
|
|
241
|
-
if (t__namespace.isObjectProperty(prop)) {
|
|
242
|
-
splitNodeTypes.forEach((type) => {
|
|
243
|
-
if (t__namespace.isIdentifier(prop.key)) {
|
|
244
|
-
if (prop.key.name === type) {
|
|
245
|
-
splitNodesByType[type] = prop.value;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
options.properties = [];
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
state
|
|
257
|
-
);
|
|
258
|
-
splitNodeTypes.forEach((splitType) => {
|
|
259
|
-
let splitNode = splitNodesByType[splitType];
|
|
260
|
-
if (!splitNode) {
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
while (t__namespace.isIdentifier(splitNode)) {
|
|
264
|
-
const binding = programPath.scope.getBinding(
|
|
265
|
-
splitNode.name
|
|
266
|
-
);
|
|
267
|
-
splitNode = binding == null ? void 0 : binding.path.node;
|
|
268
|
-
}
|
|
269
|
-
if (splitNode) {
|
|
270
|
-
if (t__namespace.isFunctionDeclaration(splitNode)) {
|
|
271
|
-
programPath.pushContainer(
|
|
272
|
-
"body",
|
|
273
|
-
t__namespace.variableDeclaration("const", [
|
|
274
|
-
t__namespace.variableDeclarator(
|
|
275
|
-
t__namespace.identifier(splitType),
|
|
276
|
-
t__namespace.functionExpression(
|
|
277
|
-
splitNode.id || null,
|
|
278
|
-
// Anonymize the function expression
|
|
279
|
-
splitNode.params,
|
|
280
|
-
splitNode.body,
|
|
281
|
-
splitNode.generator,
|
|
282
|
-
splitNode.async
|
|
283
|
-
)
|
|
284
|
-
)
|
|
285
|
-
])
|
|
286
|
-
);
|
|
287
|
-
} else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
|
|
288
|
-
programPath.pushContainer(
|
|
289
|
-
"body",
|
|
290
|
-
t__namespace.variableDeclaration("const", [
|
|
291
|
-
t__namespace.variableDeclarator(
|
|
292
|
-
t__namespace.identifier(splitType),
|
|
293
|
-
splitNode
|
|
294
|
-
)
|
|
295
|
-
])
|
|
296
|
-
);
|
|
297
|
-
} else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
|
|
298
|
-
programPath.pushContainer(
|
|
299
|
-
"body",
|
|
300
|
-
t__namespace.variableDeclaration("const", [
|
|
301
|
-
t__namespace.variableDeclarator(
|
|
302
|
-
t__namespace.identifier(splitType),
|
|
303
|
-
splitNode.local
|
|
304
|
-
)
|
|
305
|
-
])
|
|
306
|
-
);
|
|
307
|
-
} else if (t__namespace.isCallExpression(splitNode)) {
|
|
308
|
-
const outputSplitNodeCode = generate(splitNode).code;
|
|
309
|
-
const splitNodeAst = babel.parse(outputSplitNodeCode);
|
|
310
|
-
if (!splitNodeAst) {
|
|
311
|
-
throw new Error(
|
|
312
|
-
`Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}"`
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
const statement = splitNodeAst.program.body[0];
|
|
316
|
-
if (!statement) {
|
|
317
|
-
throw new Error(
|
|
318
|
-
`Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}" as no statement was found in the program body`
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
if (t__namespace.isExpressionStatement(statement)) {
|
|
322
|
-
const expression = statement.expression;
|
|
323
|
-
programPath.pushContainer(
|
|
324
|
-
"body",
|
|
325
|
-
t__namespace.variableDeclaration("const", [
|
|
326
|
-
t__namespace.variableDeclarator(
|
|
327
|
-
t__namespace.identifier(splitType),
|
|
328
|
-
expression
|
|
329
|
-
)
|
|
330
|
-
])
|
|
331
|
-
);
|
|
332
|
-
} else {
|
|
333
|
-
throw new Error(
|
|
334
|
-
`Unexpected expression type encounter for "${splitType}" in the node type "${splitNode.type}"`
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
} else {
|
|
338
|
-
console.info("Unexpected splitNode type:", splitNode);
|
|
339
|
-
throw new Error(
|
|
340
|
-
`Unexpected splitNode type ☝️: ${splitNode.type}`
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
programPath.node.body = programPath.node.body.filter(
|
|
345
|
-
(node) => {
|
|
346
|
-
return node !== splitNode;
|
|
347
|
-
}
|
|
348
|
-
);
|
|
349
|
-
programPath.pushContainer("body", [
|
|
350
|
-
t__namespace.exportNamedDeclaration(null, [
|
|
351
|
-
t__namespace.exportSpecifier(
|
|
352
|
-
t__namespace.identifier(splitType),
|
|
353
|
-
t__namespace.identifier(splitType)
|
|
354
|
-
)
|
|
355
|
-
])
|
|
356
|
-
]);
|
|
357
|
-
});
|
|
358
|
-
programPath.traverse({
|
|
359
|
-
ExportNamedDeclaration(path) {
|
|
360
|
-
if (path.node.declaration) {
|
|
361
|
-
if (t__namespace.isVariableDeclaration(path.node.declaration)) {
|
|
362
|
-
path.replaceWith(
|
|
363
|
-
t__namespace.importDeclaration(
|
|
364
|
-
path.node.declaration.declarations.map(
|
|
365
|
-
(decl) => t__namespace.importSpecifier(
|
|
366
|
-
t__namespace.identifier(decl.id.name),
|
|
367
|
-
t__namespace.identifier(decl.id.name)
|
|
368
|
-
)
|
|
369
|
-
),
|
|
370
|
-
t__namespace.stringLiteral(
|
|
371
|
-
opts.filename.split(
|
|
372
|
-
`?${constants.splitPrefix}`
|
|
373
|
-
)[0]
|
|
374
|
-
)
|
|
375
|
-
)
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
});
|
|
381
|
-
eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
},
|
|
386
|
-
{
|
|
387
|
-
root: process.cwd(),
|
|
388
|
-
minify: process.env.NODE_ENV === "production"
|
|
389
|
-
}
|
|
390
|
-
]
|
|
391
|
-
].filter(Boolean)
|
|
392
|
-
})
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
exports.compileFile = compileFile;
|
|
396
|
-
exports.splitFile = splitFile;
|
|
377
|
+
exports.compileCodeSplitReferenceRoute = compileCodeSplitReferenceRoute;
|
|
378
|
+
exports.compileCodeSplitVirtualRoute = compileCodeSplitVirtualRoute;
|
|
397
379
|
//# sourceMappingURL=compilers.cjs.map
|