@tanstack/router-plugin 1.40.3 → 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.
@@ -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 generate = require("@babel/generator");
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 eliminateUnreferencedIdentifiers = require("./eliminateUnreferencedIdentifiers.cjs");
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,148 +25,314 @@ function _interopNamespaceDefault(e) {
24
25
  }
25
26
  const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
26
27
  const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
27
- async function compileFile(opts) {
28
- return await opts.compileAst({
29
- code: opts.code,
30
- filename: opts.filename,
31
- getBabelConfig: () => ({
32
- plugins: [
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
- visitor: {
36
- Program: {
37
- enter(programPath, state) {
38
- const splitUrl = `${constants.splitPrefix}:${opts.filename}?${constants.splitPrefix}`;
39
- let existingCompImportPath = null;
40
- let existingLoaderImportPath = null;
41
- programPath.traverse(
42
- {
43
- CallExpression: (path) => {
44
- if (!t__namespace.isIdentifier(path.node.callee)) {
45
- return;
46
- }
47
- if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
48
- return;
49
- }
50
- if (t__namespace.isCallExpression(path.parentPath.node)) {
51
- const options = resolveIdentifier(
52
- path,
53
- path.parentPath.node.arguments[0]
54
- );
55
- let found = false;
56
- const hasImportedOrDefinedIdentifier = (name) => {
57
- return programPath.scope.hasBinding(name);
58
- };
59
- if (t__namespace.isObjectExpression(options)) {
60
- options.properties.forEach((prop) => {
61
- if (t__namespace.isObjectProperty(prop)) {
62
- if (t__namespace.isIdentifier(prop.key)) {
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 (found) {
135
- programPath.pushContainer("body", [
77
+ if (!hasImportedOrDefinedIdentifier(
78
+ "lazyRouteComponent"
79
+ )) {
80
+ programPath.unshiftContainer("body", [
136
81
  template__namespace.statement(
137
- `function TSR_Dummy_Component() {}`
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
- state
145
- );
146
- eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
147
- if (existingCompImportPath || existingLoaderImportPath) {
148
- programPath.traverse({
149
- ImportDeclaration(path) {
150
- if (path.node.specifiers.length > 0)
151
- return;
152
- if (path.node.source.value === existingCompImportPath || path.node.source.value === existingLoaderImportPath) {
153
- path.remove();
154
- }
155
- }
156
- });
157
- }
135
+ }
136
+ programPath.scope.crawl();
137
+ });
138
+ }
139
+ if (found) {
140
+ programPath.pushContainer("body", [
141
+ template__namespace.statement(`function TSR_Dummy_Component() {}`)()
142
+ ]);
158
143
  }
159
144
  }
160
145
  }
161
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(
162
185
  {
163
- root: process.cwd(),
164
- minify: process.env.NODE_ENV === "production"
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;
165
221
  }
166
- ]
167
- ].filter(Boolean)
168
- })
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"
169
336
  });
170
337
  }
171
338
  function getImportSpecifierAndPathFromLocalName(programPath, name) {
@@ -207,192 +374,6 @@ function removeIdentifierLiteral(path, node) {
207
374
  }
208
375
  }
209
376
  }
210
- const splitNodeTypes = ["component", "loader"];
211
- async function splitFile(opts) {
212
- return await opts.compileAst({
213
- code: opts.code,
214
- filename: opts.filename,
215
- getBabelConfig: () => ({
216
- plugins: [
217
- [
218
- {
219
- visitor: {
220
- Program: {
221
- enter(programPath, state) {
222
- const splitNodesByType = {
223
- component: void 0,
224
- loader: void 0
225
- };
226
- programPath.traverse(
227
- {
228
- CallExpression: (path) => {
229
- if (!t__namespace.isIdentifier(path.node.callee)) {
230
- return;
231
- }
232
- if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
233
- return;
234
- }
235
- if (t__namespace.isCallExpression(path.parentPath.node)) {
236
- const options = resolveIdentifier(
237
- path,
238
- path.parentPath.node.arguments[0]
239
- );
240
- if (t__namespace.isObjectExpression(options)) {
241
- options.properties.forEach((prop) => {
242
- if (t__namespace.isObjectProperty(prop)) {
243
- splitNodeTypes.forEach((type) => {
244
- if (t__namespace.isIdentifier(prop.key)) {
245
- if (prop.key.name === type) {
246
- splitNodesByType[type] = prop.value;
247
- }
248
- }
249
- });
250
- }
251
- });
252
- options.properties = [];
253
- }
254
- }
255
- }
256
- },
257
- state
258
- );
259
- splitNodeTypes.forEach((splitType) => {
260
- let splitNode = splitNodesByType[splitType];
261
- if (!splitNode) {
262
- return;
263
- }
264
- while (t__namespace.isIdentifier(splitNode)) {
265
- const binding = programPath.scope.getBinding(
266
- splitNode.name
267
- );
268
- splitNode = binding == null ? void 0 : binding.path.node;
269
- }
270
- if (splitNode) {
271
- if (t__namespace.isFunctionDeclaration(splitNode)) {
272
- programPath.pushContainer(
273
- "body",
274
- t__namespace.variableDeclaration("const", [
275
- t__namespace.variableDeclarator(
276
- t__namespace.identifier(splitType),
277
- t__namespace.functionExpression(
278
- splitNode.id || null,
279
- // Anonymize the function expression
280
- splitNode.params,
281
- splitNode.body,
282
- splitNode.generator,
283
- splitNode.async
284
- )
285
- )
286
- ])
287
- );
288
- } else if (t__namespace.isFunctionExpression(splitNode) || t__namespace.isArrowFunctionExpression(splitNode)) {
289
- programPath.pushContainer(
290
- "body",
291
- t__namespace.variableDeclaration("const", [
292
- t__namespace.variableDeclarator(
293
- t__namespace.identifier(splitType),
294
- splitNode
295
- )
296
- ])
297
- );
298
- } else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
299
- programPath.pushContainer(
300
- "body",
301
- t__namespace.variableDeclaration("const", [
302
- t__namespace.variableDeclarator(
303
- t__namespace.identifier(splitType),
304
- splitNode.local
305
- )
306
- ])
307
- );
308
- } else if (t__namespace.isCallExpression(splitNode)) {
309
- const outputSplitNodeCode = generate(splitNode).code;
310
- const splitNodeAst = babel.parse(outputSplitNodeCode);
311
- if (!splitNodeAst) {
312
- throw new Error(
313
- `Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}"`
314
- );
315
- }
316
- const statement = splitNodeAst.program.body[0];
317
- if (!statement) {
318
- throw new Error(
319
- `Failed to parse the generated code for "${splitType}" in the node type "${splitNode.type}" as no statement was found in the program body`
320
- );
321
- }
322
- if (t__namespace.isExpressionStatement(statement)) {
323
- const expression = statement.expression;
324
- programPath.pushContainer(
325
- "body",
326
- t__namespace.variableDeclaration("const", [
327
- t__namespace.variableDeclarator(
328
- t__namespace.identifier(splitType),
329
- expression
330
- )
331
- ])
332
- );
333
- } else {
334
- throw new Error(
335
- `Unexpected expression type encounter for "${splitType}" in the node type "${splitNode.type}"`
336
- );
337
- }
338
- } else {
339
- console.info("Unexpected splitNode type:", splitNode);
340
- throw new Error(
341
- `Unexpected splitNode type ☝️: ${splitNode.type}`
342
- );
343
- }
344
- }
345
- programPath.node.body = programPath.node.body.filter(
346
- (node) => {
347
- return node !== splitNode;
348
- }
349
- );
350
- programPath.pushContainer("body", [
351
- t__namespace.exportNamedDeclaration(null, [
352
- t__namespace.exportSpecifier(
353
- t__namespace.identifier(splitType),
354
- t__namespace.identifier(splitType)
355
- )
356
- ])
357
- ]);
358
- });
359
- programPath.traverse({
360
- ExportNamedDeclaration(path) {
361
- if (path.node.declaration) {
362
- if (t__namespace.isVariableDeclaration(path.node.declaration)) {
363
- path.replaceWith(
364
- t__namespace.importDeclaration(
365
- path.node.declaration.declarations.map(
366
- (decl) => t__namespace.importSpecifier(
367
- t__namespace.identifier(decl.id.name),
368
- t__namespace.identifier(decl.id.name)
369
- )
370
- ),
371
- t__namespace.stringLiteral(
372
- opts.filename.split(
373
- `?${constants.splitPrefix}`
374
- )[0]
375
- )
376
- )
377
- );
378
- }
379
- }
380
- }
381
- });
382
- eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
383
- }
384
- }
385
- }
386
- },
387
- {
388
- root: process.cwd(),
389
- minify: process.env.NODE_ENV === "production"
390
- }
391
- ]
392
- ].filter(Boolean)
393
- })
394
- });
395
- }
396
- exports.compileFile = compileFile;
397
- exports.splitFile = splitFile;
377
+ exports.compileCodeSplitReferenceRoute = compileCodeSplitReferenceRoute;
378
+ exports.compileCodeSplitVirtualRoute = compileCodeSplitVirtualRoute;
398
379
  //# sourceMappingURL=compilers.cjs.map