@tanstack/router-plugin 1.120.5 → 1.121.0-alpha.2

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.
Files changed (88) hide show
  1. package/dist/cjs/core/code-splitter/compilers.cjs +195 -194
  2. package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
  3. package/dist/cjs/core/code-splitter/compilers.d.cts +3 -0
  4. package/dist/cjs/core/code-splitter/framework-options.cjs +4 -8
  5. package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -1
  6. package/dist/cjs/core/code-splitter/framework-options.d.cts +0 -2
  7. package/dist/cjs/core/config.d.cts +31 -40
  8. package/dist/cjs/core/route-autoimport-plugin.cjs +98 -0
  9. package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -0
  10. package/dist/cjs/core/route-autoimport-plugin.d.cts +6 -0
  11. package/dist/cjs/core/route-hmr-statement.cjs +33 -0
  12. package/dist/cjs/core/route-hmr-statement.cjs.map +1 -0
  13. package/dist/cjs/core/route-hmr-statement.d.cts +1 -0
  14. package/dist/cjs/core/router-code-splitter-plugin.cjs +11 -20
  15. package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
  16. package/dist/cjs/core/router-composed-plugin.cjs +19 -5
  17. package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
  18. package/dist/cjs/core/router-generator-plugin.cjs +8 -2
  19. package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
  20. package/dist/cjs/core/router-hmr-plugin.cjs +51 -0
  21. package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -0
  22. package/dist/cjs/core/router-hmr-plugin.d.cts +8 -0
  23. package/dist/cjs/core/utils.cjs +12 -0
  24. package/dist/cjs/core/utils.cjs.map +1 -0
  25. package/dist/cjs/core/utils.d.cts +2 -0
  26. package/dist/cjs/esbuild.cjs +2 -0
  27. package/dist/cjs/esbuild.cjs.map +1 -1
  28. package/dist/cjs/esbuild.d.cts +53 -27
  29. package/dist/cjs/rspack.cjs +2 -0
  30. package/dist/cjs/rspack.cjs.map +1 -1
  31. package/dist/cjs/rspack.d.cts +53 -27
  32. package/dist/cjs/vite.cjs +2 -0
  33. package/dist/cjs/vite.cjs.map +1 -1
  34. package/dist/cjs/vite.d.cts +52 -26
  35. package/dist/cjs/webpack.cjs +2 -0
  36. package/dist/cjs/webpack.cjs.map +1 -1
  37. package/dist/cjs/webpack.d.cts +53 -27
  38. package/dist/esm/core/code-splitter/compilers.d.ts +3 -0
  39. package/dist/esm/core/code-splitter/compilers.js +195 -194
  40. package/dist/esm/core/code-splitter/compilers.js.map +1 -1
  41. package/dist/esm/core/code-splitter/framework-options.d.ts +0 -2
  42. package/dist/esm/core/code-splitter/framework-options.js +4 -8
  43. package/dist/esm/core/code-splitter/framework-options.js.map +1 -1
  44. package/dist/esm/core/config.d.ts +31 -40
  45. package/dist/esm/core/route-autoimport-plugin.d.ts +6 -0
  46. package/dist/esm/core/route-autoimport-plugin.js +81 -0
  47. package/dist/esm/core/route-autoimport-plugin.js.map +1 -0
  48. package/dist/esm/core/route-hmr-statement.d.ts +1 -0
  49. package/dist/esm/core/route-hmr-statement.js +16 -0
  50. package/dist/esm/core/route-hmr-statement.js.map +1 -0
  51. package/dist/esm/core/router-code-splitter-plugin.js +5 -14
  52. package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
  53. package/dist/esm/core/router-composed-plugin.js +19 -5
  54. package/dist/esm/core/router-composed-plugin.js.map +1 -1
  55. package/dist/esm/core/router-generator-plugin.js +8 -2
  56. package/dist/esm/core/router-generator-plugin.js.map +1 -1
  57. package/dist/esm/core/router-hmr-plugin.d.ts +8 -0
  58. package/dist/esm/core/router-hmr-plugin.js +51 -0
  59. package/dist/esm/core/router-hmr-plugin.js.map +1 -0
  60. package/dist/esm/core/utils.d.ts +2 -0
  61. package/dist/esm/core/utils.js +12 -0
  62. package/dist/esm/core/utils.js.map +1 -0
  63. package/dist/esm/esbuild.d.ts +53 -27
  64. package/dist/esm/esbuild.js +3 -1
  65. package/dist/esm/esbuild.js.map +1 -1
  66. package/dist/esm/rspack.d.ts +53 -27
  67. package/dist/esm/rspack.js +3 -1
  68. package/dist/esm/rspack.js.map +1 -1
  69. package/dist/esm/vite.d.ts +52 -26
  70. package/dist/esm/vite.js +3 -1
  71. package/dist/esm/vite.js.map +1 -1
  72. package/dist/esm/webpack.d.ts +53 -27
  73. package/dist/esm/webpack.js +3 -1
  74. package/dist/esm/webpack.js.map +1 -1
  75. package/package.json +6 -6
  76. package/src/core/code-splitter/compilers.ts +265 -277
  77. package/src/core/code-splitter/framework-options.ts +0 -6
  78. package/src/core/route-autoimport-plugin.ts +102 -0
  79. package/src/core/route-hmr-statement.ts +13 -0
  80. package/src/core/router-code-splitter-plugin.ts +3 -23
  81. package/src/core/router-composed-plugin.ts +20 -10
  82. package/src/core/router-generator-plugin.ts +12 -1
  83. package/src/core/router-hmr-plugin.ts +65 -0
  84. package/src/core/utils.ts +18 -0
  85. package/src/esbuild.ts +3 -2
  86. package/src/rspack.ts +3 -2
  87. package/src/vite.ts +3 -0
  88. package/src/webpack.ts +3 -1
@@ -6,6 +6,7 @@ const template = require("@babel/template");
6
6
  const babelDeadCodeElimination = require("babel-dead-code-elimination");
7
7
  const routerUtils = require("@tanstack/router-utils");
8
8
  const constants = require("../constants.cjs");
9
+ const routeHmrStatement = require("../route-hmr-statement.cjs");
9
10
  const pathIds = require("./path-ids.cjs");
10
11
  const frameworkOptions = require("./framework-options.cjs");
11
12
  function _interopNamespaceDefault(e) {
@@ -26,7 +27,6 @@ function _interopNamespaceDefault(e) {
26
27
  }
27
28
  const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
28
29
  const template__namespace = /* @__PURE__ */ _interopNamespaceDefault(template);
29
- process.env.TSR_VITE_DEBUG;
30
30
  const SPLIT_NODES_CONFIG = /* @__PURE__ */ new Map([
31
31
  [
32
32
  "loader",
@@ -119,164 +119,160 @@ function compileCodeSplitReferenceRoute(opts) {
119
119
  const LAZY_FN_IDENT = frameworkOptions$1.idents.lazyFn;
120
120
  babel.traverse(ast, {
121
121
  Program: {
122
- enter(programPath, programState) {
123
- const state = programState;
122
+ enter(programPath) {
124
123
  const removableImportPaths = /* @__PURE__ */ new Set([]);
125
- programPath.traverse(
126
- {
127
- CallExpression: (path) => {
128
- if (!t__namespace.isIdentifier(path.node.callee)) {
129
- return;
130
- }
131
- if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
132
- return;
133
- }
134
- if (t__namespace.isCallExpression(path.parentPath.node)) {
135
- const options = resolveIdentifier(
136
- path,
137
- path.parentPath.node.arguments[0]
138
- );
139
- const hasImportedOrDefinedIdentifier = (name) => {
140
- return programPath.scope.hasBinding(name);
141
- };
142
- if (t__namespace.isObjectExpression(options)) {
143
- options.properties.forEach((prop) => {
144
- if (t__namespace.isObjectProperty(prop)) {
145
- if (t__namespace.isIdentifier(prop.key)) {
146
- const codeSplitGroupingByKey = findIndexForSplitNode(
147
- prop.key.name
148
- );
149
- if (codeSplitGroupingByKey === -1) {
150
- return;
124
+ programPath.traverse({
125
+ CallExpression: (path) => {
126
+ if (!t__namespace.isIdentifier(path.node.callee)) {
127
+ return;
128
+ }
129
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
130
+ return;
131
+ }
132
+ function babelHandleReference(routeOptions) {
133
+ const hasImportedOrDefinedIdentifier = (name) => {
134
+ return programPath.scope.hasBinding(name);
135
+ };
136
+ if (t__namespace.isObjectExpression(routeOptions)) {
137
+ routeOptions.properties.forEach((prop) => {
138
+ if (t__namespace.isObjectProperty(prop)) {
139
+ if (t__namespace.isIdentifier(prop.key)) {
140
+ const codeSplitGroupingByKey = findIndexForSplitNode(
141
+ prop.key.name
142
+ );
143
+ if (codeSplitGroupingByKey === -1) {
144
+ return;
145
+ }
146
+ const codeSplitGroup = [
147
+ ...new Set(
148
+ opts.codeSplitGroupings[codeSplitGroupingByKey]
149
+ )
150
+ ];
151
+ const key = prop.key.name;
152
+ const isNodeConfigAvailable = SPLIT_NODES_CONFIG.has(
153
+ key
154
+ );
155
+ if (!isNodeConfigAvailable) {
156
+ return;
157
+ }
158
+ const splitNodeMeta = SPLIT_NODES_CONFIG.get(key);
159
+ const splitUrl = addSplitSearchParamToFilename(
160
+ opts.filename,
161
+ codeSplitGroup
162
+ );
163
+ if (splitNodeMeta.splitStrategy === "lazyRouteComponent") {
164
+ const value = prop.value;
165
+ let shouldSplit = true;
166
+ if (t__namespace.isIdentifier(value)) {
167
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(
168
+ programPath,
169
+ value.name
170
+ ).path;
171
+ if (existingImportPath) {
172
+ removableImportPaths.add(existingImportPath);
173
+ }
174
+ const isExported = hasExport(ast, value);
175
+ shouldSplit = !isExported;
176
+ if (shouldSplit) {
177
+ removeIdentifierLiteral(path, value);
178
+ }
151
179
  }
152
- const codeSplitGroup = [
153
- ...new Set(
154
- opts.codeSplitGroupings[codeSplitGroupingByKey]
155
- )
156
- ];
157
- const key = prop.key.name;
158
- const isNodeConfigAvailable = SPLIT_NODES_CONFIG.has(
159
- key
160
- );
161
- if (!isNodeConfigAvailable) {
180
+ if (!shouldSplit) {
162
181
  return;
163
182
  }
164
- const splitNodeMeta = SPLIT_NODES_CONFIG.get(
165
- key
166
- );
167
- const splitUrl = addSplitSearchParamToFilename(
168
- opts.filename,
169
- codeSplitGroup
170
- );
171
- if (splitNodeMeta.splitStrategy === "lazyRouteComponent") {
172
- const value = prop.value;
173
- let shouldSplit = true;
174
- if (t__namespace.isIdentifier(value)) {
175
- const existingImportPath = getImportSpecifierAndPathFromLocalName(
176
- programPath,
177
- value.name
178
- ).path;
179
- if (existingImportPath) {
180
- removableImportPaths.add(existingImportPath);
181
- }
182
- const isExported = hasExport(ast, value);
183
- shouldSplit = !isExported;
184
- if (shouldSplit) {
185
- removeIdentifierLiteral(path, value);
186
- }
187
- }
188
- if (!shouldSplit) {
189
- return;
190
- }
191
- if (!hasImportedOrDefinedIdentifier(
192
- LAZY_ROUTE_COMPONENT_IDENT
193
- )) {
194
- programPath.unshiftContainer("body", [
195
- template__namespace.statement(
196
- `import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`
197
- )()
198
- ]);
199
- }
200
- if (!hasImportedOrDefinedIdentifier(
201
- splitNodeMeta.localImporterIdent
202
- )) {
203
- programPath.unshiftContainer("body", [
204
- template__namespace.statement(
205
- `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
206
- )()
207
- ]);
208
- }
209
- if (key === "component") {
210
- prop.value = template__namespace.expression(
211
- `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}', () => Route.ssr)`
212
- )();
213
- } else {
214
- prop.value = template__namespace.expression(
215
- `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
216
- )();
217
- }
218
- if (opts.runtimeEnv !== "prod" && // only in development
219
- !hasImportedOrDefinedIdentifier(
220
- frameworkOptions$1.idents.dummyHMRComponent
221
- )) {
222
- programPath.pushContainer("body", [
223
- template__namespace.statement(
224
- frameworkOptions$1.dummyHMRComponent
225
- )()
226
- ]);
227
- }
183
+ if (!hasImportedOrDefinedIdentifier(
184
+ LAZY_ROUTE_COMPONENT_IDENT
185
+ )) {
186
+ programPath.unshiftContainer("body", [
187
+ template__namespace.statement(
188
+ `import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`
189
+ )()
190
+ ]);
228
191
  }
229
- if (splitNodeMeta.splitStrategy === "lazyFn") {
230
- const value = prop.value;
231
- let shouldSplit = true;
232
- if (t__namespace.isIdentifier(value)) {
233
- const existingImportPath = getImportSpecifierAndPathFromLocalName(
234
- programPath,
235
- value.name
236
- ).path;
237
- if (existingImportPath) {
238
- removableImportPaths.add(existingImportPath);
239
- }
240
- const isExported = hasExport(ast, value);
241
- shouldSplit = !isExported;
242
- if (shouldSplit) {
243
- removeIdentifierLiteral(path, value);
244
- }
245
- }
246
- if (!shouldSplit) {
247
- return;
248
- }
249
- if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
250
- programPath.unshiftContainer(
251
- "body",
252
- template__namespace.smart(
253
- `import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`
254
- )()
255
- );
256
- }
257
- if (!hasImportedOrDefinedIdentifier(
258
- splitNodeMeta.localImporterIdent
259
- )) {
260
- programPath.unshiftContainer("body", [
261
- template__namespace.statement(
262
- `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
263
- )()
264
- ]);
265
- }
192
+ if (!hasImportedOrDefinedIdentifier(
193
+ splitNodeMeta.localImporterIdent
194
+ )) {
195
+ programPath.unshiftContainer("body", [
196
+ template__namespace.statement(
197
+ `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
198
+ )()
199
+ ]);
200
+ }
201
+ if (key === "component") {
266
202
  prop.value = template__namespace.expression(
267
- `${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
203
+ `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}', () => Route.ssr)`
268
204
  )();
205
+ } else {
206
+ prop.value = template__namespace.expression(
207
+ `${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
208
+ )();
209
+ }
210
+ if (opts.runtimeEnv !== "prod") {
211
+ programPath.pushContainer("body", routeHmrStatement.routeHmrStatement);
212
+ }
213
+ }
214
+ if (splitNodeMeta.splitStrategy === "lazyFn") {
215
+ const value = prop.value;
216
+ let shouldSplit = true;
217
+ if (t__namespace.isIdentifier(value)) {
218
+ const existingImportPath = getImportSpecifierAndPathFromLocalName(
219
+ programPath,
220
+ value.name
221
+ ).path;
222
+ if (existingImportPath) {
223
+ removableImportPaths.add(existingImportPath);
224
+ }
225
+ const isExported = hasExport(ast, value);
226
+ shouldSplit = !isExported;
227
+ if (shouldSplit) {
228
+ removeIdentifierLiteral(path, value);
229
+ }
230
+ }
231
+ if (!shouldSplit) {
232
+ return;
233
+ }
234
+ if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
235
+ programPath.unshiftContainer(
236
+ "body",
237
+ template__namespace.smart(
238
+ `import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`
239
+ )()
240
+ );
269
241
  }
242
+ if (!hasImportedOrDefinedIdentifier(
243
+ splitNodeMeta.localImporterIdent
244
+ )) {
245
+ programPath.unshiftContainer("body", [
246
+ template__namespace.statement(
247
+ `const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`
248
+ )()
249
+ ]);
250
+ }
251
+ prop.value = template__namespace.expression(
252
+ `${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`
253
+ )();
270
254
  }
271
255
  }
272
- programPath.scope.crawl();
273
- });
274
- }
256
+ }
257
+ programPath.scope.crawl();
258
+ });
275
259
  }
276
260
  }
277
- },
278
- state
279
- );
261
+ if (t__namespace.isCallExpression(path.parentPath.node)) {
262
+ const options = resolveIdentifier(
263
+ path,
264
+ path.parentPath.node.arguments[0]
265
+ );
266
+ babelHandleReference(options);
267
+ } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
268
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
269
+ if (t__namespace.isCallExpression(caller)) {
270
+ const options = resolveIdentifier(path, caller.arguments[0]);
271
+ babelHandleReference(options);
272
+ }
273
+ }
274
+ }
275
+ });
280
276
  if (removableImportPaths.size > 0) {
281
277
  programPath.traverse({
282
278
  ImportDeclaration(path) {
@@ -304,8 +300,7 @@ function compileCodeSplitVirtualRoute(opts) {
304
300
  const knownExportedIdents = /* @__PURE__ */ new Set();
305
301
  babel.traverse(ast, {
306
302
  Program: {
307
- enter(programPath, programState) {
308
- const state = programState;
303
+ enter(programPath) {
309
304
  const trackedNodesToSplitByType = {
310
305
  component: void 0,
311
306
  loader: void 0,
@@ -313,54 +308,60 @@ function compileCodeSplitVirtualRoute(opts) {
313
308
  errorComponent: void 0,
314
309
  notFoundComponent: void 0
315
310
  };
316
- programPath.traverse(
317
- {
318
- CallExpression: (path) => {
319
- if (!t__namespace.isIdentifier(path.node.callee)) {
320
- return;
321
- }
322
- if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
323
- return;
324
- }
325
- if (t__namespace.isCallExpression(path.parentPath.node)) {
326
- const options = resolveIdentifier(
327
- path,
328
- path.parentPath.node.arguments[0]
329
- );
330
- if (t__namespace.isObjectExpression(options)) {
331
- options.properties.forEach((prop) => {
332
- if (t__namespace.isObjectProperty(prop)) {
333
- KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
334
- if (!t__namespace.isIdentifier(prop.key) || prop.key.name !== splitType) {
335
- return;
336
- }
337
- const value = prop.value;
338
- let isExported = false;
339
- if (t__namespace.isIdentifier(value)) {
340
- isExported = hasExport(ast, value);
341
- if (isExported) {
342
- knownExportedIdents.add(value.name);
343
- }
344
- }
345
- if (isExported && t__namespace.isIdentifier(value)) {
346
- removeExports(ast, value);
347
- } else {
348
- const meta = SPLIT_NODES_CONFIG.get(splitType);
349
- trackedNodesToSplitByType[splitType] = {
350
- node: prop.value,
351
- meta
352
- };
311
+ programPath.traverse({
312
+ CallExpression: (path) => {
313
+ if (!t__namespace.isIdentifier(path.node.callee)) {
314
+ return;
315
+ }
316
+ if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
317
+ return;
318
+ }
319
+ function babelHandleVirtual(options) {
320
+ if (t__namespace.isObjectExpression(options)) {
321
+ options.properties.forEach((prop) => {
322
+ if (t__namespace.isObjectProperty(prop)) {
323
+ KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
324
+ if (!t__namespace.isIdentifier(prop.key) || prop.key.name !== splitType) {
325
+ return;
326
+ }
327
+ const value = prop.value;
328
+ let isExported = false;
329
+ if (t__namespace.isIdentifier(value)) {
330
+ isExported = hasExport(ast, value);
331
+ if (isExported) {
332
+ knownExportedIdents.add(value.name);
353
333
  }
354
- });
355
- }
356
- });
357
- options.properties = [];
358
- }
334
+ }
335
+ if (isExported && t__namespace.isIdentifier(value)) {
336
+ removeExports(ast, value);
337
+ } else {
338
+ const meta = SPLIT_NODES_CONFIG.get(splitType);
339
+ trackedNodesToSplitByType[splitType] = {
340
+ node: prop.value,
341
+ meta
342
+ };
343
+ }
344
+ });
345
+ }
346
+ });
347
+ options.properties = [];
348
+ }
349
+ }
350
+ if (t__namespace.isCallExpression(path.parentPath.node)) {
351
+ const options = resolveIdentifier(
352
+ path,
353
+ path.parentPath.node.arguments[0]
354
+ );
355
+ babelHandleVirtual(options);
356
+ } else if (t__namespace.isVariableDeclarator(path.parentPath.node)) {
357
+ const caller = resolveIdentifier(path, path.parentPath.node.init);
358
+ if (t__namespace.isCallExpression(caller)) {
359
+ const options = resolveIdentifier(path, caller.arguments[0]);
360
+ babelHandleVirtual(options);
359
361
  }
360
362
  }
361
- },
362
- state
363
- );
363
+ }
364
+ });
364
365
  intendedSplitNodes.forEach((SPLIT_TYPE) => {
365
366
  const splitKey = trackedNodesToSplitByType[SPLIT_TYPE];
366
367
  if (!splitKey) {