@tanstack/router-plugin 1.167.1 → 1.167.3
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 +63 -34
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs +7 -1
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/react-refresh-ignored-route-exports.cjs +49 -0
- package/dist/cjs/core/code-splitter/plugins/react-refresh-ignored-route-exports.cjs.map +1 -0
- package/dist/cjs/core/code-splitter/plugins/react-refresh-ignored-route-exports.d.cts +2 -0
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs +24 -12
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/react-stable-hmr-split-route-components.cjs +41 -0
- package/dist/cjs/core/code-splitter/plugins/react-stable-hmr-split-route-components.cjs.map +1 -0
- package/dist/cjs/core/code-splitter/plugins/react-stable-hmr-split-route-components.d.cts +2 -0
- package/dist/cjs/core/code-splitter/plugins.d.cts +13 -0
- package/dist/cjs/core/code-splitter/types.d.cts +9 -0
- package/dist/cjs/core/route-hmr-statement.cjs +58 -15
- package/dist/cjs/core/route-hmr-statement.cjs.map +1 -1
- package/dist/cjs/core/route-hmr-statement.d.cts +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +3 -3
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +2 -2
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/utils.cjs +9 -1
- package/dist/cjs/core/utils.cjs.map +1 -1
- package/dist/cjs/core/utils.d.cts +1 -0
- package/dist/esm/core/code-splitter/compilers.js +64 -35
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js +7 -1
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/react-refresh-ignored-route-exports.d.ts +2 -0
- package/dist/esm/core/code-splitter/plugins/react-refresh-ignored-route-exports.js +46 -0
- package/dist/esm/core/code-splitter/plugins/react-refresh-ignored-route-exports.js.map +1 -0
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js +25 -13
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/react-stable-hmr-split-route-components.d.ts +2 -0
- package/dist/esm/core/code-splitter/plugins/react-stable-hmr-split-route-components.js +38 -0
- package/dist/esm/core/code-splitter/plugins/react-stable-hmr-split-route-components.js.map +1 -0
- package/dist/esm/core/code-splitter/plugins.d.ts +13 -0
- package/dist/esm/core/code-splitter/types.d.ts +9 -0
- package/dist/esm/core/route-hmr-statement.d.ts +1 -1
- package/dist/esm/core/route-hmr-statement.js +58 -15
- package/dist/esm/core/route-hmr-statement.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +3 -3
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +3 -3
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/core/utils.d.ts +1 -0
- package/dist/esm/core/utils.js +9 -2
- package/dist/esm/core/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/core/code-splitter/compilers.ts +118 -62
- package/src/core/code-splitter/plugins/framework-plugins.ts +7 -1
- package/src/core/code-splitter/plugins/react-refresh-ignored-route-exports.ts +65 -0
- package/src/core/code-splitter/plugins/react-refresh-route-components.ts +68 -39
- package/src/core/code-splitter/plugins/react-stable-hmr-split-route-components.ts +56 -0
- package/src/core/code-splitter/plugins.ts +18 -0
- package/src/core/code-splitter/types.ts +11 -0
- package/src/core/route-hmr-statement.ts +141 -25
- package/src/core/router-code-splitter-plugin.ts +2 -2
- package/src/core/router-hmr-plugin.ts +7 -6
- package/src/core/utils.ts +27 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { tsrShared, tsrSplit } from "../constants.js";
|
|
2
|
-
import {
|
|
2
|
+
import { createRouteHmrStatement } from "../route-hmr-statement.js";
|
|
3
|
+
import { getObjectPropertyKeyName } from "../utils.js";
|
|
3
4
|
import { createIdentifier } from "./path-ids.js";
|
|
4
5
|
import { getFrameworkOptions } from "./framework-options.js";
|
|
5
6
|
import { deadCodeElimination, findReferencedIdentifiers, generateFromAst, parseAst } from "@tanstack/router-utils";
|
|
@@ -162,10 +163,12 @@ function computeSharedBindings(opts) {
|
|
|
162
163
|
const splitGroupsPresent = /* @__PURE__ */ new Set();
|
|
163
164
|
let hasNonSplit = false;
|
|
164
165
|
for (const prop of routeOptions.properties) {
|
|
165
|
-
if (!t.isObjectProperty(prop)
|
|
166
|
-
|
|
166
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
167
|
+
const key = getObjectPropertyKeyName(prop);
|
|
168
|
+
if (!key) continue;
|
|
169
|
+
if (key === "codeSplitGroupings") continue;
|
|
167
170
|
if (t.isIdentifier(prop.value) && prop.value.name === "undefined") continue;
|
|
168
|
-
const groupIndex = findIndexForSplitNode(
|
|
171
|
+
const groupIndex = findIndexForSplitNode(key);
|
|
169
172
|
if (groupIndex === -1) hasNonSplit = true;
|
|
170
173
|
else splitGroupsPresent.add(groupIndex);
|
|
171
174
|
}
|
|
@@ -177,8 +180,9 @@ function computeSharedBindings(opts) {
|
|
|
177
180
|
const fullDepGraph = buildDependencyGraph(declMap, allLocalBindings);
|
|
178
181
|
const refsByGroup = /* @__PURE__ */ new Map();
|
|
179
182
|
for (const prop of routeOptions.properties) {
|
|
180
|
-
if (!t.isObjectProperty(prop)
|
|
181
|
-
const key = prop
|
|
183
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
184
|
+
const key = getObjectPropertyKeyName(prop);
|
|
185
|
+
if (!key) continue;
|
|
182
186
|
if (key === "codeSplitGroupings") continue;
|
|
183
187
|
const groupIndex = findIndexForSplitNode(key);
|
|
184
188
|
const directRefs = collectModuleLevelRefsFromNode(prop.value, localModuleLevelBindings);
|
|
@@ -358,6 +362,7 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
358
362
|
const PACKAGE = frameworkOptions.package;
|
|
359
363
|
const LAZY_ROUTE_COMPONENT_IDENT = frameworkOptions.idents.lazyRouteComponent;
|
|
360
364
|
const LAZY_FN_IDENT = frameworkOptions.idents.lazyFn;
|
|
365
|
+
const stableRouteOptionKeys = [...new Set((opts.compilerPlugins ?? []).flatMap((plugin) => plugin.getStableRouteOptionKeys?.() ?? []))];
|
|
361
366
|
let createRouteFn;
|
|
362
367
|
let modified = false;
|
|
363
368
|
let hmrAdded = false;
|
|
@@ -381,20 +386,35 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
381
386
|
const hasImportedOrDefinedIdentifier = (name) => {
|
|
382
387
|
return programPath.scope.hasBinding(name);
|
|
383
388
|
};
|
|
389
|
+
const addRouteHmr = (insertionPath, routeOptions) => {
|
|
390
|
+
if (!opts.addHmr || hmrAdded) return;
|
|
391
|
+
opts.compilerPlugins?.forEach((plugin) => {
|
|
392
|
+
if ((plugin.onAddHmr?.({
|
|
393
|
+
programPath,
|
|
394
|
+
callExpressionPath: path,
|
|
395
|
+
insertionPath,
|
|
396
|
+
routeOptions,
|
|
397
|
+
createRouteFn,
|
|
398
|
+
opts
|
|
399
|
+
}))?.modified) modified = true;
|
|
400
|
+
});
|
|
401
|
+
programPath.pushContainer("body", createRouteHmrStatement(stableRouteOptionKeys));
|
|
402
|
+
modified = true;
|
|
403
|
+
hmrAdded = true;
|
|
404
|
+
};
|
|
384
405
|
if (t.isObjectExpression(routeOptions)) {
|
|
406
|
+
const insertionPath = path.getStatementParent() ?? path;
|
|
385
407
|
if (opts.deleteNodes && opts.deleteNodes.size > 0) routeOptions.properties = routeOptions.properties.filter((prop) => {
|
|
386
408
|
if (t.isObjectProperty(prop)) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
409
|
+
const key = getObjectPropertyKeyName(prop);
|
|
410
|
+
if (key && opts.deleteNodes.has(key)) {
|
|
411
|
+
modified = true;
|
|
412
|
+
return false;
|
|
392
413
|
}
|
|
393
414
|
}
|
|
394
415
|
return true;
|
|
395
416
|
});
|
|
396
417
|
if (!splittableCreateRouteFns.includes(createRouteFn)) {
|
|
397
|
-
const insertionPath = path.getStatementParent() ?? path;
|
|
398
418
|
opts.compilerPlugins?.forEach((plugin) => {
|
|
399
419
|
if ((plugin.onUnsplittableRoute?.({
|
|
400
420
|
programPath,
|
|
@@ -405,17 +425,13 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
405
425
|
opts
|
|
406
426
|
}))?.modified) modified = true;
|
|
407
427
|
});
|
|
408
|
-
|
|
409
|
-
programPath.pushContainer("body", routeHmrStatement);
|
|
410
|
-
modified = true;
|
|
411
|
-
hmrAdded = true;
|
|
412
|
-
}
|
|
428
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
413
429
|
return programPath.stop();
|
|
414
430
|
}
|
|
415
431
|
routeOptions.properties.forEach((prop) => {
|
|
416
432
|
if (t.isObjectProperty(prop)) {
|
|
417
|
-
|
|
418
|
-
|
|
433
|
+
const key = getObjectPropertyKeyName(prop);
|
|
434
|
+
if (key) {
|
|
419
435
|
const codeSplitGroupingByKey = findIndexForSplitNode(key);
|
|
420
436
|
if (codeSplitGroupingByKey === -1) return;
|
|
421
437
|
const codeSplitGroup = [...new Set(opts.codeSplitGroupings[codeSplitGroupingByKey])];
|
|
@@ -438,12 +454,26 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
438
454
|
modified = true;
|
|
439
455
|
if (!hasImportedOrDefinedIdentifier(LAZY_ROUTE_COMPONENT_IDENT)) programPath.unshiftContainer("body", [template.statement(`import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`)()]);
|
|
440
456
|
if (!hasImportedOrDefinedIdentifier(splitNodeMeta.localImporterIdent)) programPath.unshiftContainer("body", [template.statement(`const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`)()]);
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
457
|
+
const insertionPath = path.getStatementParent() ?? path;
|
|
458
|
+
let splitPropValue;
|
|
459
|
+
for (const plugin of opts.compilerPlugins ?? []) {
|
|
460
|
+
const pluginPropValue = plugin.onSplitRouteProperty?.({
|
|
461
|
+
programPath,
|
|
462
|
+
callExpressionPath: path,
|
|
463
|
+
insertionPath,
|
|
464
|
+
routeOptions,
|
|
465
|
+
prop,
|
|
466
|
+
splitNodeMeta,
|
|
467
|
+
lazyRouteComponentIdent: LAZY_ROUTE_COMPONENT_IDENT
|
|
468
|
+
});
|
|
469
|
+
if (!pluginPropValue) continue;
|
|
444
470
|
modified = true;
|
|
445
|
-
|
|
471
|
+
splitPropValue = pluginPropValue;
|
|
472
|
+
break;
|
|
446
473
|
}
|
|
474
|
+
if (splitPropValue) prop.value = splitPropValue;
|
|
475
|
+
else prop.value = template.expression(`${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`)();
|
|
476
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
447
477
|
} else {
|
|
448
478
|
const value = prop.value;
|
|
449
479
|
let shouldSplit = true;
|
|
@@ -465,6 +495,7 @@ function compileCodeSplitReferenceRoute(opts) {
|
|
|
465
495
|
}
|
|
466
496
|
programPath.scope.crawl();
|
|
467
497
|
});
|
|
498
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
468
499
|
}
|
|
469
500
|
}
|
|
470
501
|
if (t.isCallExpression(path.parentPath.node)) babelHandleReference(resolveIdentifier(path, path.parentPath.node.arguments[0]));
|
|
@@ -540,7 +571,7 @@ function compileCodeSplitVirtualRoute(opts) {
|
|
|
540
571
|
if (t.isObjectExpression(options)) {
|
|
541
572
|
options.properties.forEach((prop) => {
|
|
542
573
|
if (t.isObjectProperty(prop)) KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
|
|
543
|
-
if (
|
|
574
|
+
if (getObjectPropertyKeyName(prop) !== splitType) return;
|
|
544
575
|
const value = prop.value;
|
|
545
576
|
if (t.isIdentifier(value) && value.name === "undefined") return;
|
|
546
577
|
let isExported = false;
|
|
@@ -734,18 +765,16 @@ function detectCodeSplitGroupingsFromRoute(opts) {
|
|
|
734
765
|
function babelHandleSplittingGroups(routeOptions) {
|
|
735
766
|
if (t.isObjectExpression(routeOptions)) routeOptions.properties.forEach((prop) => {
|
|
736
767
|
if (t.isObjectProperty(prop)) {
|
|
737
|
-
if (
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
if (t.isArrayExpression(
|
|
741
|
-
if (t.
|
|
742
|
-
|
|
743
|
-
return node.value;
|
|
744
|
-
});
|
|
745
|
-
throw new Error("You must provide arrays with codeSplitGroupings options.");
|
|
768
|
+
if (getObjectPropertyKeyName(prop) === "codeSplitGroupings") {
|
|
769
|
+
const value = prop.value;
|
|
770
|
+
if (t.isArrayExpression(value)) codeSplitGroupings = value.elements.map((group) => {
|
|
771
|
+
if (t.isArrayExpression(group)) return group.elements.map((node) => {
|
|
772
|
+
if (!t.isStringLiteral(node)) throw new Error("You must provide a string literal for the codeSplitGroupings");
|
|
773
|
+
return node.value;
|
|
746
774
|
});
|
|
747
|
-
|
|
748
|
-
}
|
|
775
|
+
throw new Error("You must provide arrays with codeSplitGroupings options.");
|
|
776
|
+
});
|
|
777
|
+
else throw new Error("You must provide an array of arrays for the codeSplitGroupings.");
|
|
749
778
|
}
|
|
750
779
|
}
|
|
751
780
|
});
|