@tanstack/router-plugin 1.166.10 → 1.166.12
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/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/core/code-splitter/compilers.cjs +866 -1324
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/framework-options.cjs +39 -41
- package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/path-ids.cjs +26 -31
- package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs +11 -14
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs +31 -65
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs.map +1 -1
- package/dist/cjs/core/config.cjs +36 -46
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/constants.cjs +16 -15
- package/dist/cjs/core/constants.cjs.map +1 -1
- package/dist/cjs/core/route-autoimport-plugin.cjs +81 -115
- package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
- package/dist/cjs/core/route-hmr-statement.cjs +34 -56
- package/dist/cjs/core/route-hmr-statement.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +236 -306
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-composed-plugin.cjs +28 -34
- package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +101 -133
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +81 -81
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/utils.cjs +23 -30
- package/dist/cjs/core/utils.cjs.map +1 -1
- package/dist/cjs/esbuild.cjs +47 -18
- package/dist/cjs/esbuild.cjs.map +1 -1
- package/dist/cjs/index.cjs +11 -13
- package/dist/cjs/rspack.cjs +58 -19
- package/dist/cjs/rspack.cjs.map +1 -1
- package/dist/cjs/vite.cjs +52 -20
- package/dist/cjs/vite.cjs.map +1 -1
- package/dist/cjs/webpack.cjs +46 -19
- package/dist/cjs/webpack.cjs.map +1 -1
- package/dist/esm/core/code-splitter/compilers.js +859 -1307
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/framework-options.js +40 -42
- package/dist/esm/core/code-splitter/framework-options.js.map +1 -1
- package/dist/esm/core/code-splitter/path-ids.js +27 -33
- package/dist/esm/core/code-splitter/path-ids.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js +11 -14
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js +29 -48
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js.map +1 -1
- package/dist/esm/core/config.js +35 -48
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/constants.js +17 -19
- package/dist/esm/core/constants.js.map +1 -1
- package/dist/esm/core/route-autoimport-plugin.js +78 -98
- package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
- package/dist/esm/core/route-hmr-statement.js +32 -39
- package/dist/esm/core/route-hmr-statement.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +234 -305
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-composed-plugin.js +25 -32
- package/dist/esm/core/router-composed-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +100 -111
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +79 -80
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/core/utils.js +21 -15
- package/dist/esm/core/utils.js.map +1 -1
- package/dist/esm/esbuild.js +37 -18
- package/dist/esm/esbuild.js.map +1 -1
- package/dist/esm/index.js +2 -11
- package/dist/esm/rspack.js +49 -20
- package/dist/esm/rspack.js.map +1 -1
- package/dist/esm/vite.js +42 -21
- package/dist/esm/vite.js.map +1 -1
- package/dist/esm/webpack.js +37 -20
- package/dist/esm/webpack.js.map +1 -1
- package/package.json +6 -6
- package/src/core/code-splitter/compilers.ts +1 -1
- package/src/core/route-autoimport-plugin.ts +1 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.js.map +0 -1
|
@@ -1,37 +1,33 @@
|
|
|
1
1
|
import * as template from "@babel/template";
|
|
2
|
+
//#region src/core/route-hmr-statement.ts
|
|
2
3
|
function handleRouteUpdate(oldRoute, newRoute) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
node.dynamic?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
29
|
-
node.optional?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
30
|
-
node.wildcard?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
31
|
-
}
|
|
4
|
+
newRoute._path = oldRoute._path;
|
|
5
|
+
newRoute._id = oldRoute._id;
|
|
6
|
+
newRoute._fullPath = oldRoute._fullPath;
|
|
7
|
+
newRoute._to = oldRoute._to;
|
|
8
|
+
newRoute.children = oldRoute.children;
|
|
9
|
+
newRoute.parentRoute = oldRoute.parentRoute;
|
|
10
|
+
const router = window.__TSR_ROUTER__;
|
|
11
|
+
router.routesById[newRoute.id] = newRoute;
|
|
12
|
+
router.routesByPath[newRoute.fullPath] = newRoute;
|
|
13
|
+
router.processedTree.matchCache.clear();
|
|
14
|
+
router.processedTree.flatCache?.clear();
|
|
15
|
+
router.processedTree.singleCache.clear();
|
|
16
|
+
router.resolvePathCache.clear();
|
|
17
|
+
walkReplaceSegmentTree(newRoute, router.processedTree.segmentTree);
|
|
18
|
+
const filter = (m) => m.routeId === oldRoute.id;
|
|
19
|
+
if (router.state.matches.find(filter) || router.state.pendingMatches?.find(filter)) router.invalidate({ filter });
|
|
20
|
+
function walkReplaceSegmentTree(route, node) {
|
|
21
|
+
if (node.route?.id === route.id) node.route = route;
|
|
22
|
+
if (node.index) walkReplaceSegmentTree(route, node.index);
|
|
23
|
+
node.static?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
24
|
+
node.staticInsensitive?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
25
|
+
node.dynamic?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
26
|
+
node.optional?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
27
|
+
node.wildcard?.forEach((child) => walkReplaceSegmentTree(route, child));
|
|
28
|
+
}
|
|
32
29
|
}
|
|
33
|
-
|
|
34
|
-
`
|
|
30
|
+
var routeHmrStatement = template.statement(`
|
|
35
31
|
if (import.meta.hot) {
|
|
36
32
|
import.meta.hot.accept((newModule) => {
|
|
37
33
|
if (Route && newModule && newModule.Route) {
|
|
@@ -39,11 +35,8 @@ if (import.meta.hot) {
|
|
|
39
35
|
}
|
|
40
36
|
})
|
|
41
37
|
}
|
|
42
|
-
`,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
routeHmrStatement
|
|
48
|
-
};
|
|
49
|
-
//# sourceMappingURL=route-hmr-statement.js.map
|
|
38
|
+
`, { placeholderPattern: false })();
|
|
39
|
+
//#endregion
|
|
40
|
+
export { routeHmrStatement };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=route-hmr-statement.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route-hmr-statement.js","sources":["../../../src/core/route-hmr-statement.ts"],"sourcesContent":["import * as template from '@babel/template'\nimport type { AnyRoute, AnyRouteMatch, AnyRouter } from '@tanstack/router-core'\n\ntype AnyRouteWithPrivateProps = AnyRoute & {\n _path: string\n _id: string\n _fullPath: string\n _to: string\n}\n\nfunction handleRouteUpdate(\n oldRoute: AnyRouteWithPrivateProps,\n newRoute: AnyRouteWithPrivateProps,\n) {\n newRoute._path = oldRoute._path\n newRoute._id = oldRoute._id\n newRoute._fullPath = oldRoute._fullPath\n newRoute._to = oldRoute._to\n newRoute.children = oldRoute.children\n newRoute.parentRoute = oldRoute.parentRoute\n\n const router = window.__TSR_ROUTER__!\n router.routesById[newRoute.id] = newRoute\n router.routesByPath[newRoute.fullPath] = newRoute\n router.processedTree.matchCache.clear()\n router.processedTree.flatCache?.clear()\n router.processedTree.singleCache.clear()\n router.resolvePathCache.clear()\n // TODO: how to rebuild the tree if we add a new route?\n walkReplaceSegmentTree(newRoute, router.processedTree.segmentTree)\n const filter = (m: AnyRouteMatch) => m.routeId === oldRoute.id\n if (\n router.state.matches.find(filter) ||\n router.state.pendingMatches?.find(filter)\n ) {\n router.invalidate({ filter })\n }\n function walkReplaceSegmentTree(\n route: AnyRouteWithPrivateProps,\n node: AnyRouter['processedTree']['segmentTree'],\n ) {\n if (node.route?.id === route.id) node.route = route\n if (node.index) walkReplaceSegmentTree(route, node.index)\n node.static?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.staticInsensitive?.forEach((child) =>\n walkReplaceSegmentTree(route, child),\n )\n node.dynamic?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.optional?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.wildcard?.forEach((child) => walkReplaceSegmentTree(route, child))\n }\n}\n\nexport const routeHmrStatement = template.statement(\n `\nif (import.meta.hot) {\n import.meta.hot.accept((newModule) => {\n if (Route && newModule && newModule.Route) {\n (${handleRouteUpdate.toString()})(Route, newModule.Route)\n }\n })\n}\n`,\n // Disable placeholder parsing so identifiers like __TSR_ROUTER__ are treated as normal identifiers instead of template placeholders\n { placeholderPattern: false },\n)()\n"],"
|
|
1
|
+
{"version":3,"file":"route-hmr-statement.js","names":[],"sources":["../../../src/core/route-hmr-statement.ts"],"sourcesContent":["import * as template from '@babel/template'\nimport type { AnyRoute, AnyRouteMatch, AnyRouter } from '@tanstack/router-core'\n\ntype AnyRouteWithPrivateProps = AnyRoute & {\n _path: string\n _id: string\n _fullPath: string\n _to: string\n}\n\nfunction handleRouteUpdate(\n oldRoute: AnyRouteWithPrivateProps,\n newRoute: AnyRouteWithPrivateProps,\n) {\n newRoute._path = oldRoute._path\n newRoute._id = oldRoute._id\n newRoute._fullPath = oldRoute._fullPath\n newRoute._to = oldRoute._to\n newRoute.children = oldRoute.children\n newRoute.parentRoute = oldRoute.parentRoute\n\n const router = window.__TSR_ROUTER__!\n router.routesById[newRoute.id] = newRoute\n router.routesByPath[newRoute.fullPath] = newRoute\n router.processedTree.matchCache.clear()\n router.processedTree.flatCache?.clear()\n router.processedTree.singleCache.clear()\n router.resolvePathCache.clear()\n // TODO: how to rebuild the tree if we add a new route?\n walkReplaceSegmentTree(newRoute, router.processedTree.segmentTree)\n const filter = (m: AnyRouteMatch) => m.routeId === oldRoute.id\n if (\n router.state.matches.find(filter) ||\n router.state.pendingMatches?.find(filter)\n ) {\n router.invalidate({ filter })\n }\n function walkReplaceSegmentTree(\n route: AnyRouteWithPrivateProps,\n node: AnyRouter['processedTree']['segmentTree'],\n ) {\n if (node.route?.id === route.id) node.route = route\n if (node.index) walkReplaceSegmentTree(route, node.index)\n node.static?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.staticInsensitive?.forEach((child) =>\n walkReplaceSegmentTree(route, child),\n )\n node.dynamic?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.optional?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.wildcard?.forEach((child) => walkReplaceSegmentTree(route, child))\n }\n}\n\nexport const routeHmrStatement = template.statement(\n `\nif (import.meta.hot) {\n import.meta.hot.accept((newModule) => {\n if (Route && newModule && newModule.Route) {\n (${handleRouteUpdate.toString()})(Route, newModule.Route)\n }\n })\n}\n`,\n // Disable placeholder parsing so identifiers like __TSR_ROUTER__ are treated as normal identifiers instead of template placeholders\n { placeholderPattern: false },\n)()\n"],"mappings":";;AAUA,SAAS,kBACP,UACA,UACA;AACA,UAAS,QAAQ,SAAS;AAC1B,UAAS,MAAM,SAAS;AACxB,UAAS,YAAY,SAAS;AAC9B,UAAS,MAAM,SAAS;AACxB,UAAS,WAAW,SAAS;AAC7B,UAAS,cAAc,SAAS;CAEhC,MAAM,SAAS,OAAO;AACtB,QAAO,WAAW,SAAS,MAAM;AACjC,QAAO,aAAa,SAAS,YAAY;AACzC,QAAO,cAAc,WAAW,OAAO;AACvC,QAAO,cAAc,WAAW,OAAO;AACvC,QAAO,cAAc,YAAY,OAAO;AACxC,QAAO,iBAAiB,OAAO;AAE/B,wBAAuB,UAAU,OAAO,cAAc,YAAY;CAClE,MAAM,UAAU,MAAqB,EAAE,YAAY,SAAS;AAC5D,KACE,OAAO,MAAM,QAAQ,KAAK,OAAO,IACjC,OAAO,MAAM,gBAAgB,KAAK,OAAO,CAEzC,QAAO,WAAW,EAAE,QAAQ,CAAC;CAE/B,SAAS,uBACP,OACA,MACA;AACA,MAAI,KAAK,OAAO,OAAO,MAAM,GAAI,MAAK,QAAQ;AAC9C,MAAI,KAAK,MAAO,wBAAuB,OAAO,KAAK,MAAM;AACzD,OAAK,QAAQ,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACrE,OAAK,mBAAmB,SAAS,UAC/B,uBAAuB,OAAO,MAAM,CACrC;AACD,OAAK,SAAS,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACtE,OAAK,UAAU,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACvE,OAAK,UAAU,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;;;AAI3E,IAAa,oBAAoB,SAAS,UACxC;;;;SAIO,kBAAkB,UAAU,CAAC;;;;GAMpC,EAAE,oBAAoB,OAAO,CAC9B,EAAE"}
|
|
@@ -1,309 +1,238 @@
|
|
|
1
|
-
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
2
|
-
import { logDiff } from "@tanstack/router-utils";
|
|
3
1
|
import { getConfig, splitGroupingsSchema } from "./config.js";
|
|
4
|
-
import {
|
|
5
|
-
import { getReferenceRouteCompilerPlugins } from "./code-splitter/plugins/framework-plugins.js";
|
|
6
|
-
import { tsrSplit, tsrShared, splitRouteIdentNodes, defaultCodeSplitGroupings } from "./constants.js";
|
|
2
|
+
import { defaultCodeSplitGroupings, splitRouteIdentNodes, tsrShared, tsrSplit } from "./constants.js";
|
|
7
3
|
import { decodeIdentifier } from "./code-splitter/path-ids.js";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
userConfig = getConfig(options, ROOT);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
51
|
-
const sharedBindingsMap = /* @__PURE__ */ new Map();
|
|
52
|
-
const getGlobalCodeSplitGroupings = () => {
|
|
53
|
-
return userConfig.codeSplittingOptions?.defaultBehavior || defaultCodeSplitGroupings;
|
|
54
|
-
};
|
|
55
|
-
const getShouldSplitFn = () => {
|
|
56
|
-
return userConfig.codeSplittingOptions?.splitBehavior;
|
|
57
|
-
};
|
|
58
|
-
const handleCompilingReferenceFile = (code, id, generatorNodeInfo) => {
|
|
59
|
-
if (debug) console.info("Compiling Route: ", id);
|
|
60
|
-
const fromCode = detectCodeSplitGroupingsFromRoute({
|
|
61
|
-
code
|
|
62
|
-
});
|
|
63
|
-
if (fromCode.groupings) {
|
|
64
|
-
const res = splitGroupingsSchema.safeParse(fromCode.groupings);
|
|
65
|
-
if (!res.success) {
|
|
66
|
-
const message = res.error.errors.map((e) => e.message).join(". ");
|
|
67
|
-
throw new Error(
|
|
68
|
-
`The groupings for the route "${id}" are invalid.
|
|
69
|
-
${message}`
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const userShouldSplitFn = getShouldSplitFn();
|
|
74
|
-
const pluginSplitBehavior = userShouldSplitFn?.({
|
|
75
|
-
routeId: generatorNodeInfo.routePath
|
|
76
|
-
});
|
|
77
|
-
if (pluginSplitBehavior) {
|
|
78
|
-
const res = splitGroupingsSchema.safeParse(pluginSplitBehavior);
|
|
79
|
-
if (!res.success) {
|
|
80
|
-
const message = res.error.errors.map((e) => e.message).join(". ");
|
|
81
|
-
throw new Error(
|
|
82
|
-
`The groupings returned when using \`splitBehavior\` for the route "${id}" are invalid.
|
|
83
|
-
${message}`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const splitGroupings = fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings();
|
|
88
|
-
const sharedBindings = computeSharedBindings({
|
|
89
|
-
code,
|
|
90
|
-
codeSplitGroupings: splitGroupings
|
|
91
|
-
});
|
|
92
|
-
if (sharedBindings.size > 0) {
|
|
93
|
-
sharedBindingsMap.set(id, sharedBindings);
|
|
94
|
-
} else {
|
|
95
|
-
sharedBindingsMap.delete(id);
|
|
96
|
-
}
|
|
97
|
-
const addHmr = (userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction;
|
|
98
|
-
const compiledReferenceRoute = compileCodeSplitReferenceRoute({
|
|
99
|
-
code,
|
|
100
|
-
codeSplitGroupings: splitGroupings,
|
|
101
|
-
targetFramework: userConfig.target,
|
|
102
|
-
filename: id,
|
|
103
|
-
id,
|
|
104
|
-
deleteNodes: userConfig.codeSplittingOptions?.deleteNodes ? new Set(userConfig.codeSplittingOptions.deleteNodes) : void 0,
|
|
105
|
-
addHmr,
|
|
106
|
-
sharedBindings: sharedBindings.size > 0 ? sharedBindings : void 0,
|
|
107
|
-
compilerPlugins: getReferenceRouteCompilerPlugins({
|
|
108
|
-
targetFramework: userConfig.target,
|
|
109
|
-
addHmr
|
|
110
|
-
})
|
|
111
|
-
});
|
|
112
|
-
if (compiledReferenceRoute === null) {
|
|
113
|
-
if (debug) {
|
|
114
|
-
console.info(
|
|
115
|
-
`No changes made to route "${id}", skipping code-splitting.`
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
if (debug) {
|
|
121
|
-
logDiff(code, compiledReferenceRoute.code);
|
|
122
|
-
console.log("Output:\n", compiledReferenceRoute.code + "\n\n");
|
|
123
|
-
}
|
|
124
|
-
return compiledReferenceRoute;
|
|
125
|
-
};
|
|
126
|
-
const handleCompilingVirtualFile = (code, id) => {
|
|
127
|
-
if (debug) console.info("Splitting Route: ", id);
|
|
128
|
-
const [_, ...pathnameParts] = id.split("?");
|
|
129
|
-
const searchParams = new URLSearchParams(pathnameParts.join("?"));
|
|
130
|
-
const splitValue = searchParams.get(tsrSplit);
|
|
131
|
-
if (!splitValue) {
|
|
132
|
-
throw new Error(
|
|
133
|
-
`The split value for the virtual route "${id}" was not found.`
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
const rawGrouping = decodeIdentifier(splitValue);
|
|
137
|
-
const grouping = [...new Set(rawGrouping)].filter(
|
|
138
|
-
(p) => splitRouteIdentNodes.includes(p)
|
|
139
|
-
);
|
|
140
|
-
const baseId = id.split("?")[0];
|
|
141
|
-
const resolvedSharedBindings = sharedBindingsMap.get(baseId);
|
|
142
|
-
const result = compileCodeSplitVirtualRoute({
|
|
143
|
-
code,
|
|
144
|
-
filename: id,
|
|
145
|
-
splitTargets: grouping,
|
|
146
|
-
sharedBindings: resolvedSharedBindings
|
|
147
|
-
});
|
|
148
|
-
if (debug) {
|
|
149
|
-
logDiff(code, result.code);
|
|
150
|
-
console.log("Output:\n", result.code + "\n\n");
|
|
151
|
-
}
|
|
152
|
-
return result;
|
|
153
|
-
};
|
|
154
|
-
const includedCode = [
|
|
155
|
-
"createFileRoute(",
|
|
156
|
-
"createRootRoute(",
|
|
157
|
-
"createRootRouteWithContext("
|
|
158
|
-
];
|
|
159
|
-
return [
|
|
160
|
-
{
|
|
161
|
-
name: "tanstack-router:code-splitter:compile-reference-file",
|
|
162
|
-
enforce: "pre",
|
|
163
|
-
transform: {
|
|
164
|
-
filter: {
|
|
165
|
-
id: {
|
|
166
|
-
exclude: [tsrSplit, tsrShared],
|
|
167
|
-
// this is necessary for webpack / rspack to avoid matching .html files
|
|
168
|
-
include: /\.(m|c)?(j|t)sx?$/
|
|
169
|
-
},
|
|
170
|
-
code: {
|
|
171
|
-
include: includedCode
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
handler(code, id) {
|
|
175
|
-
const normalizedId = normalizePath(id);
|
|
176
|
-
const generatorFileInfo = globalThis.TSR_ROUTES_BY_ID_MAP?.get(normalizedId);
|
|
177
|
-
if (generatorFileInfo && includedCode.some((included) => code.includes(included))) {
|
|
178
|
-
return handleCompilingReferenceFile(
|
|
179
|
-
code,
|
|
180
|
-
normalizedId,
|
|
181
|
-
generatorFileInfo
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
},
|
|
187
|
-
vite: {
|
|
188
|
-
configResolved(config) {
|
|
189
|
-
ROOT = config.root;
|
|
190
|
-
initUserConfig();
|
|
191
|
-
const routerPluginIndex = config.plugins.findIndex(
|
|
192
|
-
(p) => p.name === CODE_SPLITTER_PLUGIN_NAME
|
|
193
|
-
);
|
|
194
|
-
if (routerPluginIndex === -1) return;
|
|
195
|
-
const frameworkPlugins = TRANSFORMATION_PLUGINS_BY_FRAMEWORK[userConfig.target];
|
|
196
|
-
if (!frameworkPlugins) return;
|
|
197
|
-
for (const transformPlugin of frameworkPlugins) {
|
|
198
|
-
const transformPluginIndex = config.plugins.findIndex(
|
|
199
|
-
(p) => transformPlugin.pluginNames.includes(p.name)
|
|
200
|
-
);
|
|
201
|
-
if (transformPluginIndex !== -1 && transformPluginIndex < routerPluginIndex) {
|
|
202
|
-
throw new Error(
|
|
203
|
-
`Plugin order error: '${transformPlugin.pkg}' is placed before '@tanstack/router-plugin'.
|
|
204
|
-
|
|
205
|
-
The TanStack Router plugin must come BEFORE JSX transformation plugins.
|
|
206
|
-
|
|
207
|
-
Please update your Vite config:
|
|
208
|
-
|
|
209
|
-
plugins: [
|
|
210
|
-
tanstackRouter(),
|
|
211
|
-
${transformPlugin.usage},
|
|
212
|
-
]
|
|
213
|
-
`
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
},
|
|
218
|
-
applyToEnvironment(environment) {
|
|
219
|
-
if (userConfig.plugin?.vite?.environmentName) {
|
|
220
|
-
return userConfig.plugin.vite.environmentName === environment.name;
|
|
221
|
-
}
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
},
|
|
225
|
-
rspack(compiler) {
|
|
226
|
-
ROOT = process.cwd();
|
|
227
|
-
initUserConfig();
|
|
228
|
-
if (compiler.options.mode === "production") {
|
|
229
|
-
compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
230
|
-
console.info("✅ " + PLUGIN_NAME + ": code-splitting done!");
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
webpack(compiler) {
|
|
235
|
-
ROOT = process.cwd();
|
|
236
|
-
initUserConfig();
|
|
237
|
-
if (compiler.options.mode === "production") {
|
|
238
|
-
compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
239
|
-
console.info("✅ " + PLUGIN_NAME + ": code-splitting done!");
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
{
|
|
245
|
-
name: "tanstack-router:code-splitter:compile-virtual-file",
|
|
246
|
-
enforce: "pre",
|
|
247
|
-
transform: {
|
|
248
|
-
filter: {
|
|
249
|
-
id: /tsr-split/
|
|
250
|
-
},
|
|
251
|
-
handler(code, id) {
|
|
252
|
-
const url = pathToFileURL(id);
|
|
253
|
-
url.searchParams.delete("v");
|
|
254
|
-
const normalizedId = normalizePath(fileURLToPath(url));
|
|
255
|
-
return handleCompilingVirtualFile(code, normalizedId);
|
|
256
|
-
}
|
|
257
|
-
},
|
|
258
|
-
vite: {
|
|
259
|
-
applyToEnvironment(environment) {
|
|
260
|
-
if (userConfig.plugin?.vite?.environmentName) {
|
|
261
|
-
return userConfig.plugin.vite.environmentName === environment.name;
|
|
262
|
-
}
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
},
|
|
267
|
-
{
|
|
268
|
-
name: "tanstack-router:code-splitter:compile-shared-file",
|
|
269
|
-
enforce: "pre",
|
|
270
|
-
transform: {
|
|
271
|
-
filter: {
|
|
272
|
-
id: /tsr-shared/
|
|
273
|
-
},
|
|
274
|
-
handler(code, id) {
|
|
275
|
-
const url = pathToFileURL(id);
|
|
276
|
-
url.searchParams.delete("v");
|
|
277
|
-
const normalizedId = normalizePath(fileURLToPath(url));
|
|
278
|
-
const [baseId] = normalizedId.split("?");
|
|
279
|
-
if (!baseId) return null;
|
|
280
|
-
const sharedBindings = sharedBindingsMap.get(baseId);
|
|
281
|
-
if (!sharedBindings || sharedBindings.size === 0) return null;
|
|
282
|
-
if (debug) console.info("Compiling Shared Module: ", id);
|
|
283
|
-
const result = compileCodeSplitSharedRoute({
|
|
284
|
-
code,
|
|
285
|
-
sharedBindings,
|
|
286
|
-
filename: normalizedId
|
|
287
|
-
});
|
|
288
|
-
if (debug) {
|
|
289
|
-
logDiff(code, result.code);
|
|
290
|
-
console.log("Output:\n", result.code + "\n\n");
|
|
291
|
-
}
|
|
292
|
-
return result;
|
|
293
|
-
}
|
|
294
|
-
},
|
|
295
|
-
vite: {
|
|
296
|
-
applyToEnvironment(environment) {
|
|
297
|
-
if (userConfig.plugin?.vite?.environmentName) {
|
|
298
|
-
return userConfig.plugin.vite.environmentName === environment.name;
|
|
299
|
-
}
|
|
300
|
-
return true;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
];
|
|
4
|
+
import { compileCodeSplitReferenceRoute, compileCodeSplitSharedRoute, compileCodeSplitVirtualRoute, computeSharedBindings, detectCodeSplitGroupingsFromRoute } from "./code-splitter/compilers.js";
|
|
5
|
+
import { debug, normalizePath } from "./utils.js";
|
|
6
|
+
import { getReferenceRouteCompilerPlugins } from "./code-splitter/plugins/framework-plugins.js";
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
|
+
import { logDiff } from "@tanstack/router-utils";
|
|
9
|
+
//#region src/core/router-code-splitter-plugin.ts
|
|
10
|
+
/**
|
|
11
|
+
* It is important to familiarize yourself with how the code-splitting works in this plugin.
|
|
12
|
+
* https://github.com/TanStack/router/pull/3355
|
|
13
|
+
*/
|
|
14
|
+
var PLUGIN_NAME = "unplugin:router-code-splitter";
|
|
15
|
+
var CODE_SPLITTER_PLUGIN_NAME = "tanstack-router:code-splitter:compile-reference-file";
|
|
16
|
+
/**
|
|
17
|
+
* JSX transformation plugins grouped by framework.
|
|
18
|
+
* These plugins must come AFTER the TanStack Router plugin in the Vite config.
|
|
19
|
+
*/
|
|
20
|
+
var TRANSFORMATION_PLUGINS_BY_FRAMEWORK = {
|
|
21
|
+
react: [
|
|
22
|
+
{
|
|
23
|
+
pluginNames: ["vite:react-babel", "vite:react-refresh"],
|
|
24
|
+
pkg: "@vitejs/plugin-react",
|
|
25
|
+
usage: "react()"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
pluginNames: ["vite:react-swc", "vite:react-swc:resolve-runtime"],
|
|
29
|
+
pkg: "@vitejs/plugin-react-swc",
|
|
30
|
+
usage: "reactSwc()"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
pluginNames: ["vite:react-oxc:config", "vite:react-oxc:refresh-runtime"],
|
|
34
|
+
pkg: "@vitejs/plugin-react-oxc",
|
|
35
|
+
usage: "reactOxc()"
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
solid: [{
|
|
39
|
+
pluginNames: ["solid"],
|
|
40
|
+
pkg: "vite-plugin-solid",
|
|
41
|
+
usage: "solid()"
|
|
42
|
+
}]
|
|
305
43
|
};
|
|
306
|
-
|
|
307
|
-
|
|
44
|
+
var unpluginRouterCodeSplitterFactory = (options = {}, { framework: _framework }) => {
|
|
45
|
+
let ROOT = process.cwd();
|
|
46
|
+
let userConfig;
|
|
47
|
+
function initUserConfig() {
|
|
48
|
+
if (typeof options === "function") userConfig = options();
|
|
49
|
+
else userConfig = getConfig(options, ROOT);
|
|
50
|
+
}
|
|
51
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
52
|
+
const sharedBindingsMap = /* @__PURE__ */ new Map();
|
|
53
|
+
const getGlobalCodeSplitGroupings = () => {
|
|
54
|
+
return userConfig.codeSplittingOptions?.defaultBehavior || defaultCodeSplitGroupings;
|
|
55
|
+
};
|
|
56
|
+
const getShouldSplitFn = () => {
|
|
57
|
+
return userConfig.codeSplittingOptions?.splitBehavior;
|
|
58
|
+
};
|
|
59
|
+
const handleCompilingReferenceFile = (code, id, generatorNodeInfo) => {
|
|
60
|
+
if (debug) console.info("Compiling Route: ", id);
|
|
61
|
+
const fromCode = detectCodeSplitGroupingsFromRoute({ code });
|
|
62
|
+
if (fromCode.groupings) {
|
|
63
|
+
const res = splitGroupingsSchema.safeParse(fromCode.groupings);
|
|
64
|
+
if (!res.success) {
|
|
65
|
+
const message = res.error.errors.map((e) => e.message).join(". ");
|
|
66
|
+
throw new Error(`The groupings for the route "${id}" are invalid.\n${message}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const pluginSplitBehavior = getShouldSplitFn()?.({ routeId: generatorNodeInfo.routePath });
|
|
70
|
+
if (pluginSplitBehavior) {
|
|
71
|
+
const res = splitGroupingsSchema.safeParse(pluginSplitBehavior);
|
|
72
|
+
if (!res.success) {
|
|
73
|
+
const message = res.error.errors.map((e) => e.message).join(". ");
|
|
74
|
+
throw new Error(`The groupings returned when using \`splitBehavior\` for the route "${id}" are invalid.\n${message}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const splitGroupings = fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings();
|
|
78
|
+
const sharedBindings = computeSharedBindings({
|
|
79
|
+
code,
|
|
80
|
+
codeSplitGroupings: splitGroupings
|
|
81
|
+
});
|
|
82
|
+
if (sharedBindings.size > 0) sharedBindingsMap.set(id, sharedBindings);
|
|
83
|
+
else sharedBindingsMap.delete(id);
|
|
84
|
+
const addHmr = (userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction;
|
|
85
|
+
const compiledReferenceRoute = compileCodeSplitReferenceRoute({
|
|
86
|
+
code,
|
|
87
|
+
codeSplitGroupings: splitGroupings,
|
|
88
|
+
targetFramework: userConfig.target,
|
|
89
|
+
filename: id,
|
|
90
|
+
id,
|
|
91
|
+
deleteNodes: userConfig.codeSplittingOptions?.deleteNodes ? new Set(userConfig.codeSplittingOptions.deleteNodes) : void 0,
|
|
92
|
+
addHmr,
|
|
93
|
+
sharedBindings: sharedBindings.size > 0 ? sharedBindings : void 0,
|
|
94
|
+
compilerPlugins: getReferenceRouteCompilerPlugins({
|
|
95
|
+
targetFramework: userConfig.target,
|
|
96
|
+
addHmr
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
if (compiledReferenceRoute === null) {
|
|
100
|
+
if (debug) console.info(`No changes made to route "${id}", skipping code-splitting.`);
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
if (debug) {
|
|
104
|
+
logDiff(code, compiledReferenceRoute.code);
|
|
105
|
+
console.log("Output:\n", compiledReferenceRoute.code + "\n\n");
|
|
106
|
+
}
|
|
107
|
+
return compiledReferenceRoute;
|
|
108
|
+
};
|
|
109
|
+
const handleCompilingVirtualFile = (code, id) => {
|
|
110
|
+
if (debug) console.info("Splitting Route: ", id);
|
|
111
|
+
const [_, ...pathnameParts] = id.split("?");
|
|
112
|
+
const splitValue = new URLSearchParams(pathnameParts.join("?")).get(tsrSplit);
|
|
113
|
+
if (!splitValue) throw new Error(`The split value for the virtual route "${id}" was not found.`);
|
|
114
|
+
const rawGrouping = decodeIdentifier(splitValue);
|
|
115
|
+
const grouping = [...new Set(rawGrouping)].filter((p) => splitRouteIdentNodes.includes(p));
|
|
116
|
+
const baseId = id.split("?")[0];
|
|
117
|
+
const result = compileCodeSplitVirtualRoute({
|
|
118
|
+
code,
|
|
119
|
+
filename: id,
|
|
120
|
+
splitTargets: grouping,
|
|
121
|
+
sharedBindings: sharedBindingsMap.get(baseId)
|
|
122
|
+
});
|
|
123
|
+
if (debug) {
|
|
124
|
+
logDiff(code, result.code);
|
|
125
|
+
console.log("Output:\n", result.code + "\n\n");
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
};
|
|
129
|
+
const includedCode = [
|
|
130
|
+
"createFileRoute(",
|
|
131
|
+
"createRootRoute(",
|
|
132
|
+
"createRootRouteWithContext("
|
|
133
|
+
];
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
name: "tanstack-router:code-splitter:compile-reference-file",
|
|
137
|
+
enforce: "pre",
|
|
138
|
+
transform: {
|
|
139
|
+
filter: {
|
|
140
|
+
id: {
|
|
141
|
+
exclude: [tsrSplit, tsrShared],
|
|
142
|
+
include: /\.(m|c)?(j|t)sx?$/
|
|
143
|
+
},
|
|
144
|
+
code: { include: includedCode }
|
|
145
|
+
},
|
|
146
|
+
handler(code, id) {
|
|
147
|
+
const normalizedId = normalizePath(id);
|
|
148
|
+
const generatorFileInfo = globalThis.TSR_ROUTES_BY_ID_MAP?.get(normalizedId);
|
|
149
|
+
if (generatorFileInfo && includedCode.some((included) => code.includes(included))) return handleCompilingReferenceFile(code, normalizedId, generatorFileInfo);
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
vite: {
|
|
154
|
+
configResolved(config) {
|
|
155
|
+
ROOT = config.root;
|
|
156
|
+
initUserConfig();
|
|
157
|
+
const routerPluginIndex = config.plugins.findIndex((p) => p.name === CODE_SPLITTER_PLUGIN_NAME);
|
|
158
|
+
if (routerPluginIndex === -1) return;
|
|
159
|
+
const frameworkPlugins = TRANSFORMATION_PLUGINS_BY_FRAMEWORK[userConfig.target];
|
|
160
|
+
if (!frameworkPlugins) return;
|
|
161
|
+
for (const transformPlugin of frameworkPlugins) {
|
|
162
|
+
const transformPluginIndex = config.plugins.findIndex((p) => transformPlugin.pluginNames.includes(p.name));
|
|
163
|
+
if (transformPluginIndex !== -1 && transformPluginIndex < routerPluginIndex) throw new Error(`Plugin order error: '${transformPlugin.pkg}' is placed before '@tanstack/router-plugin'.\n\nThe TanStack Router plugin must come BEFORE JSX transformation plugins.\n\nPlease update your Vite config:\n\n plugins: [\n tanstackRouter(),\n ${transformPlugin.usage},\n ]\n`);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
applyToEnvironment(environment) {
|
|
167
|
+
if (userConfig.plugin?.vite?.environmentName) return userConfig.plugin.vite.environmentName === environment.name;
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
rspack(compiler) {
|
|
172
|
+
ROOT = process.cwd();
|
|
173
|
+
initUserConfig();
|
|
174
|
+
if (compiler.options.mode === "production") compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
175
|
+
console.info("✅ " + PLUGIN_NAME + ": code-splitting done!");
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
webpack(compiler) {
|
|
179
|
+
ROOT = process.cwd();
|
|
180
|
+
initUserConfig();
|
|
181
|
+
if (compiler.options.mode === "production") compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
182
|
+
console.info("✅ " + PLUGIN_NAME + ": code-splitting done!");
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
name: "tanstack-router:code-splitter:compile-virtual-file",
|
|
188
|
+
enforce: "pre",
|
|
189
|
+
transform: {
|
|
190
|
+
filter: { id: /tsr-split/ },
|
|
191
|
+
handler(code, id) {
|
|
192
|
+
const url = pathToFileURL(id);
|
|
193
|
+
url.searchParams.delete("v");
|
|
194
|
+
return handleCompilingVirtualFile(code, normalizePath(fileURLToPath(url)));
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
vite: { applyToEnvironment(environment) {
|
|
198
|
+
if (userConfig.plugin?.vite?.environmentName) return userConfig.plugin.vite.environmentName === environment.name;
|
|
199
|
+
return true;
|
|
200
|
+
} }
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: "tanstack-router:code-splitter:compile-shared-file",
|
|
204
|
+
enforce: "pre",
|
|
205
|
+
transform: {
|
|
206
|
+
filter: { id: /tsr-shared/ },
|
|
207
|
+
handler(code, id) {
|
|
208
|
+
const url = pathToFileURL(id);
|
|
209
|
+
url.searchParams.delete("v");
|
|
210
|
+
const normalizedId = normalizePath(fileURLToPath(url));
|
|
211
|
+
const [baseId] = normalizedId.split("?");
|
|
212
|
+
if (!baseId) return null;
|
|
213
|
+
const sharedBindings = sharedBindingsMap.get(baseId);
|
|
214
|
+
if (!sharedBindings || sharedBindings.size === 0) return null;
|
|
215
|
+
if (debug) console.info("Compiling Shared Module: ", id);
|
|
216
|
+
const result = compileCodeSplitSharedRoute({
|
|
217
|
+
code,
|
|
218
|
+
sharedBindings,
|
|
219
|
+
filename: normalizedId
|
|
220
|
+
});
|
|
221
|
+
if (debug) {
|
|
222
|
+
logDiff(code, result.code);
|
|
223
|
+
console.log("Output:\n", result.code + "\n\n");
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
vite: { applyToEnvironment(environment) {
|
|
229
|
+
if (userConfig.plugin?.vite?.environmentName) return userConfig.plugin.vite.environmentName === environment.name;
|
|
230
|
+
return true;
|
|
231
|
+
} }
|
|
232
|
+
}
|
|
233
|
+
];
|
|
308
234
|
};
|
|
309
|
-
//#
|
|
235
|
+
//#endregion
|
|
236
|
+
export { unpluginRouterCodeSplitterFactory };
|
|
237
|
+
|
|
238
|
+
//# sourceMappingURL=router-code-splitter-plugin.js.map
|