@tanstack/start-plugin-core 1.121.0-alpha.25 → 1.121.0-alpha.27
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/compilers.cjs +24 -10
- package/dist/cjs/compilers.cjs.map +1 -1
- package/dist/cjs/constants.cjs +7 -1
- package/dist/cjs/constants.cjs.map +1 -1
- package/dist/cjs/constants.d.cts +1 -1
- package/dist/cjs/debug.cjs +5 -0
- package/dist/cjs/debug.cjs.map +1 -0
- package/dist/cjs/debug.d.cts +1 -0
- package/dist/cjs/nitro-plugin/build-sitemap.cjs +3 -3
- package/dist/cjs/nitro-plugin/build-sitemap.cjs.map +1 -1
- package/dist/cjs/nitro-plugin/plugin.cjs +2 -2
- package/dist/cjs/nitro-plugin/plugin.cjs.map +1 -1
- package/dist/cjs/nitro-plugin/prerender.cjs +7 -4
- package/dist/cjs/nitro-plugin/prerender.cjs.map +1 -1
- package/dist/cjs/plugin.cjs +26 -31
- package/dist/cjs/plugin.cjs.map +1 -1
- package/dist/cjs/plugin.d.cts +2 -4
- package/dist/cjs/schema.cjs +5 -5
- package/dist/cjs/schema.cjs.map +1 -1
- package/dist/cjs/schema.d.cts +32 -16
- package/dist/cjs/start-compiler-plugin.cjs +19 -17
- package/dist/cjs/start-compiler-plugin.cjs.map +1 -1
- package/dist/cjs/start-manifest-plugin/plugin.cjs +182 -0
- package/dist/cjs/start-manifest-plugin/plugin.cjs.map +1 -0
- package/dist/cjs/start-manifest-plugin/plugin.d.cts +3 -0
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs +39 -0
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs.map +1 -0
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.d.cts +6 -0
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.cjs +121 -0
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.cjs.map +1 -0
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.d.cts +2 -0
- package/dist/cjs/start-router-plugin/plugin.cjs +21 -0
- package/dist/cjs/start-router-plugin/plugin.cjs.map +1 -0
- package/dist/cjs/start-router-plugin/plugin.d.cts +3 -0
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.cjs +72 -0
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.cjs.map +1 -0
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.d.cts +6 -0
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.cjs +30 -0
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.cjs.map +1 -0
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.d.cts +4 -0
- package/dist/esm/compilers.js +24 -10
- package/dist/esm/compilers.js.map +1 -1
- package/dist/esm/constants.d.ts +1 -1
- package/dist/esm/constants.js +7 -1
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/debug.d.ts +1 -0
- package/dist/esm/debug.js +5 -0
- package/dist/esm/debug.js.map +1 -0
- package/dist/esm/nitro-plugin/prerender.js +4 -1
- package/dist/esm/nitro-plugin/prerender.js.map +1 -1
- package/dist/esm/plugin.d.ts +2 -4
- package/dist/esm/plugin.js +17 -22
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/schema.d.ts +32 -16
- package/dist/esm/start-compiler-plugin.js +19 -17
- package/dist/esm/start-compiler-plugin.js.map +1 -1
- package/dist/esm/start-manifest-plugin/plugin.d.ts +3 -0
- package/dist/esm/start-manifest-plugin/plugin.js +182 -0
- package/dist/esm/start-manifest-plugin/plugin.js.map +1 -0
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.d.ts +6 -0
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +39 -0
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -0
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.d.ts +2 -0
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js +121 -0
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js.map +1 -0
- package/dist/esm/start-router-plugin/plugin.d.ts +3 -0
- package/dist/esm/start-router-plugin/plugin.js +21 -0
- package/dist/esm/start-router-plugin/plugin.js.map +1 -0
- package/dist/esm/start-router-plugin/route-tree-client-plugin.d.ts +6 -0
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js +55 -0
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js.map +1 -0
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.d.ts +4 -0
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js +30 -0
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js.map +1 -0
- package/package.json +7 -6
- package/src/compilers.ts +31 -131
- package/src/constants.ts +8 -1
- package/src/debug.ts +3 -0
- package/src/global.d.ts +8 -0
- package/src/nitro-plugin/prerender.ts +4 -1
- package/src/plugin.ts +15 -26
- package/src/start-compiler-plugin.ts +24 -24
- package/src/start-manifest-plugin/plugin.ts +249 -0
- package/src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts +43 -0
- package/src/start-router-plugin/generator-plugins/server-routes-plugin.ts +138 -0
- package/src/start-router-plugin/plugin.ts +35 -0
- package/src/start-router-plugin/route-tree-client-plugin.ts +76 -0
- package/src/start-router-plugin/virtual-route-tree-plugin.ts +30 -0
- package/dist/cjs/start-routes-manifest-plugin/plugin.cjs +0 -207
- package/dist/cjs/start-routes-manifest-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/start-routes-manifest-plugin/plugin.d.cts +0 -3
- package/dist/cjs/start-server-routes-plugin/config.d.cts +0 -49
- package/dist/cjs/start-server-routes-plugin/plugin.cjs +0 -614
- package/dist/cjs/start-server-routes-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/start-server-routes-plugin/plugin.d.cts +0 -3
- package/dist/cjs/start-server-routes-plugin/template.cjs +0 -111
- package/dist/cjs/start-server-routes-plugin/template.cjs.map +0 -1
- package/dist/cjs/start-server-routes-plugin/template.d.cts +0 -34
- package/dist/esm/start-routes-manifest-plugin/plugin.d.ts +0 -3
- package/dist/esm/start-routes-manifest-plugin/plugin.js +0 -207
- package/dist/esm/start-routes-manifest-plugin/plugin.js.map +0 -1
- package/dist/esm/start-server-routes-plugin/config.d.ts +0 -49
- package/dist/esm/start-server-routes-plugin/plugin.d.ts +0 -3
- package/dist/esm/start-server-routes-plugin/plugin.js +0 -614
- package/dist/esm/start-server-routes-plugin/plugin.js.map +0 -1
- package/dist/esm/start-server-routes-plugin/template.d.ts +0 -34
- package/dist/esm/start-server-routes-plugin/template.js +0 -111
- package/dist/esm/start-server-routes-plugin/template.js.map +0 -1
- package/src/start-routes-manifest-plugin/plugin.ts +0 -282
- package/src/start-server-routes-plugin/config.ts +0 -8
- package/src/start-server-routes-plugin/plugin.ts +0 -900
- package/src/start-server-routes-plugin/template.ts +0 -164
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import * as t from '@babel/types'
|
|
3
|
+
import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'
|
|
4
|
+
import { normalizePath } from 'vite'
|
|
5
|
+
import { deadCodeElimination } from 'babel-dead-code-elimination'
|
|
6
|
+
import { debug } from '../debug'
|
|
7
|
+
import type { Plugin } from 'vite'
|
|
8
|
+
import type { Config } from '@tanstack/router-generator'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This removes the server part from the generated route tree so that it can be used on the client.
|
|
12
|
+
*/
|
|
13
|
+
export function routeTreeClientPlugin(config: Config): Plugin {
|
|
14
|
+
const generatedRouteTreePath = normalizePath(
|
|
15
|
+
path.resolve(config.generatedRouteTree),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
name: 'tanstack-start:route-tree-client-plugin',
|
|
20
|
+
enforce: 'pre',
|
|
21
|
+
// only run this plugin in the client environment
|
|
22
|
+
applyToEnvironment: (env) => env.config.consumer === 'client',
|
|
23
|
+
transform: {
|
|
24
|
+
filter: { id: generatedRouteTreePath },
|
|
25
|
+
handler(code, id) {
|
|
26
|
+
if (id !== generatedRouteTreePath) {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
if (debug) console.info(`Compiling route tree for the client`, id)
|
|
30
|
+
const ast = parseAst({ code, sourceFilename: id })
|
|
31
|
+
|
|
32
|
+
// only keep `export const routeTree = ... `
|
|
33
|
+
const filteredBody = ast.program.body.filter((node) => {
|
|
34
|
+
if (t.isExportNamedDeclaration(node)) {
|
|
35
|
+
if (
|
|
36
|
+
node.declaration &&
|
|
37
|
+
t.isVariableDeclaration(node.declaration) &&
|
|
38
|
+
node.declaration.declarations.length === 1 &&
|
|
39
|
+
node.declaration.declarations[0] &&
|
|
40
|
+
t.isVariableDeclarator(node.declaration.declarations[0]) &&
|
|
41
|
+
t.isIdentifier(node.declaration.declarations[0].id) &&
|
|
42
|
+
node.declaration.declarations[0].id.name === 'routeTree'
|
|
43
|
+
) {
|
|
44
|
+
return true
|
|
45
|
+
}
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
// strip off the typescript interface & module declarations since they also reference the server routes
|
|
49
|
+
if (
|
|
50
|
+
t.isTSInterfaceDeclaration(node) ||
|
|
51
|
+
t.isTSModuleDeclaration(node)
|
|
52
|
+
) {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
return true
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
ast.program.body = filteredBody
|
|
59
|
+
|
|
60
|
+
deadCodeElimination(ast)
|
|
61
|
+
|
|
62
|
+
const compiled = generateFromAst(ast, {
|
|
63
|
+
sourceMaps: true,
|
|
64
|
+
sourceFileName: id,
|
|
65
|
+
filename: id,
|
|
66
|
+
})
|
|
67
|
+
if (debug) {
|
|
68
|
+
logDiff(code, compiled.code)
|
|
69
|
+
console.log('Output:\n', compiled.code, '\n\n')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return compiled
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { normalizePath } from 'vite'
|
|
3
|
+
import { debug } from '../debug'
|
|
4
|
+
import type { Config } from '@tanstack/router-generator'
|
|
5
|
+
import type { Plugin } from 'vite'
|
|
6
|
+
|
|
7
|
+
export const moduleId = 'tanstack-start-route-tree:v'
|
|
8
|
+
|
|
9
|
+
export function virtualRouteTreePlugin(config: Config): Plugin {
|
|
10
|
+
const generatedRouteTreePath = normalizePath(
|
|
11
|
+
path.resolve(config.generatedRouteTree),
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
name: 'tanstack-start:virtual-route-tree',
|
|
16
|
+
enforce: 'pre',
|
|
17
|
+
sharedDuringBuild: true,
|
|
18
|
+
resolveId: {
|
|
19
|
+
filter: { id: new RegExp(moduleId) },
|
|
20
|
+
handler(id) {
|
|
21
|
+
let resolvedId: string | null = null
|
|
22
|
+
if (id === moduleId) {
|
|
23
|
+
if (debug) console.info('resolving id', id, generatedRouteTreePath)
|
|
24
|
+
resolvedId = generatedRouteTreePath
|
|
25
|
+
}
|
|
26
|
+
return resolvedId
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const fs = require("node:fs");
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const ufo = require("ufo");
|
|
6
|
-
const routerCore = require("@tanstack/router-core");
|
|
7
|
-
const utils = require("../utils.cjs");
|
|
8
|
-
const getCSSRecursively = (file, filesByRouteFilePath, basePath) => {
|
|
9
|
-
const result = [];
|
|
10
|
-
for (const cssFile of file.css ?? []) {
|
|
11
|
-
result.push({
|
|
12
|
-
tag: "link",
|
|
13
|
-
attrs: {
|
|
14
|
-
rel: "stylesheet",
|
|
15
|
-
href: ufo.joinURL(basePath, cssFile),
|
|
16
|
-
type: "text/css"
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
for (const imp of file.imports ?? []) {
|
|
21
|
-
const importInfo = filesByRouteFilePath[imp];
|
|
22
|
-
if (importInfo) {
|
|
23
|
-
result.push(
|
|
24
|
-
...getCSSRecursively(importInfo, filesByRouteFilePath, basePath)
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return result;
|
|
29
|
-
};
|
|
30
|
-
function startRoutesManifestPlugin(opts) {
|
|
31
|
-
let config;
|
|
32
|
-
const moduleId = "tanstack-start-router-manifest:v";
|
|
33
|
-
const resolvedModuleId = utils.resolveViteId(moduleId);
|
|
34
|
-
return {
|
|
35
|
-
name: "tsr-routes-manifest",
|
|
36
|
-
enforce: "pre",
|
|
37
|
-
configResolved(resolvedConfig) {
|
|
38
|
-
config = resolvedConfig;
|
|
39
|
-
},
|
|
40
|
-
// configEnvironment(env, envConfig) {
|
|
41
|
-
// config = envConfig.
|
|
42
|
-
// },
|
|
43
|
-
resolveId(id) {
|
|
44
|
-
if (id === moduleId) {
|
|
45
|
-
return resolvedModuleId;
|
|
46
|
-
}
|
|
47
|
-
return;
|
|
48
|
-
},
|
|
49
|
-
load(id) {
|
|
50
|
-
var _a, _b;
|
|
51
|
-
if (id === resolvedModuleId) {
|
|
52
|
-
if (this.environment.config.consumer !== "server") {
|
|
53
|
-
return `export default {}`;
|
|
54
|
-
}
|
|
55
|
-
if (config.command === "serve") {
|
|
56
|
-
return `export const tsrStartManifest = () => ({
|
|
57
|
-
routes: {}
|
|
58
|
-
})`;
|
|
59
|
-
}
|
|
60
|
-
const APP_BASE = globalThis.TSS_APP_BASE;
|
|
61
|
-
const clientViteManifestPath = path.resolve(
|
|
62
|
-
opts.root,
|
|
63
|
-
".tanstack-start/build/client-dist/.vite/manifest.json"
|
|
64
|
-
);
|
|
65
|
-
let viteManifest;
|
|
66
|
-
try {
|
|
67
|
-
viteManifest = JSON.parse(
|
|
68
|
-
fs.readFileSync(clientViteManifestPath, "utf-8")
|
|
69
|
-
);
|
|
70
|
-
} catch (err) {
|
|
71
|
-
console.error(err);
|
|
72
|
-
throw new Error(
|
|
73
|
-
`Could not find the production client vite manifest at '${clientViteManifestPath}'!`
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
const routeTreePath = path.resolve(opts.tsr.generatedRouteTree);
|
|
77
|
-
let routeTreeContent;
|
|
78
|
-
try {
|
|
79
|
-
routeTreeContent = fs.readFileSync(routeTreePath, "utf-8");
|
|
80
|
-
} catch (err) {
|
|
81
|
-
console.error(err);
|
|
82
|
-
throw new Error(
|
|
83
|
-
`Could not find the generated route tree at '${routeTreePath}'!`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
const routerManifest = JSON.parse(
|
|
87
|
-
((_a = routeTreeContent.match(
|
|
88
|
-
/\/\* ROUTE_MANIFEST_START([\s\S]*?)ROUTE_MANIFEST_END \*\//
|
|
89
|
-
)) == null ? void 0 : _a[1]) || "{ routes: {} }"
|
|
90
|
-
);
|
|
91
|
-
const routeTreeRoutes = routerManifest.routes;
|
|
92
|
-
let entryFile;
|
|
93
|
-
const filesByRouteFilePath = Object.fromEntries(
|
|
94
|
-
Object.entries(viteManifest).map(([k, v]) => {
|
|
95
|
-
if (v.isEntry) {
|
|
96
|
-
entryFile = v;
|
|
97
|
-
}
|
|
98
|
-
const rPath = k.split("?")[0];
|
|
99
|
-
return [rPath, v];
|
|
100
|
-
}, {})
|
|
101
|
-
);
|
|
102
|
-
const routesDirectoryFromRoot = path.relative(
|
|
103
|
-
opts.root,
|
|
104
|
-
opts.tsr.routesDirectory
|
|
105
|
-
);
|
|
106
|
-
Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {
|
|
107
|
-
const file = filesByRouteFilePath[path.posix.join(routesDirectoryFromRoot, v.filePath)];
|
|
108
|
-
if (file) {
|
|
109
|
-
const preloads = (file.imports ?? []).map((d) => {
|
|
110
|
-
const assetPath = ufo.joinURL(APP_BASE, viteManifest[d].file);
|
|
111
|
-
return assetPath;
|
|
112
|
-
});
|
|
113
|
-
if (file.file) {
|
|
114
|
-
preloads.unshift(path.join(APP_BASE, file.file));
|
|
115
|
-
}
|
|
116
|
-
const cssAssetsList = getCSSRecursively(
|
|
117
|
-
file,
|
|
118
|
-
filesByRouteFilePath,
|
|
119
|
-
APP_BASE
|
|
120
|
-
);
|
|
121
|
-
routeTreeRoutes[routeId] = {
|
|
122
|
-
...v,
|
|
123
|
-
assets: [...v.assets || [], ...cssAssetsList],
|
|
124
|
-
preloads
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
if (entryFile) {
|
|
129
|
-
routeTreeRoutes[routerCore.rootRouteId].preloads = [
|
|
130
|
-
ufo.joinURL(APP_BASE, entryFile.file),
|
|
131
|
-
...((_b = entryFile.imports) == null ? void 0 : _b.map(
|
|
132
|
-
(d) => ufo.joinURL(APP_BASE, viteManifest[d].file)
|
|
133
|
-
)) || []
|
|
134
|
-
];
|
|
135
|
-
const entryCssAssetsList = getCSSRecursively(
|
|
136
|
-
entryFile,
|
|
137
|
-
filesByRouteFilePath,
|
|
138
|
-
APP_BASE
|
|
139
|
-
);
|
|
140
|
-
routeTreeRoutes[routerCore.rootRouteId].assets = [
|
|
141
|
-
...routeTreeRoutes[routerCore.rootRouteId].assets || [],
|
|
142
|
-
...entryCssAssetsList,
|
|
143
|
-
{
|
|
144
|
-
tag: "script",
|
|
145
|
-
attrs: {
|
|
146
|
-
src: ufo.joinURL(APP_BASE, entryFile.file),
|
|
147
|
-
type: "module"
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
];
|
|
151
|
-
}
|
|
152
|
-
const recurseRoute = (route, seenPreloads = {}) => {
|
|
153
|
-
var _a2;
|
|
154
|
-
route.preloads = (_a2 = route.preloads) == null ? void 0 : _a2.filter((preload) => {
|
|
155
|
-
if (seenPreloads[preload]) {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
seenPreloads[preload] = true;
|
|
159
|
-
return true;
|
|
160
|
-
});
|
|
161
|
-
if (route.children) {
|
|
162
|
-
route.children.forEach((child) => {
|
|
163
|
-
const childRoute = routeTreeRoutes[child];
|
|
164
|
-
recurseRoute(childRoute, { ...seenPreloads });
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
recurseRoute(routeTreeRoutes[routerCore.rootRouteId]);
|
|
169
|
-
const routesManifest = {
|
|
170
|
-
routes: routeTreeRoutes
|
|
171
|
-
};
|
|
172
|
-
try {
|
|
173
|
-
const routesManifestOutputDirPath = path.resolve(
|
|
174
|
-
opts.root,
|
|
175
|
-
".tanstack-start/build/route-assets-manifest"
|
|
176
|
-
);
|
|
177
|
-
fs.rmSync(routesManifestOutputDirPath, {
|
|
178
|
-
recursive: true,
|
|
179
|
-
force: true
|
|
180
|
-
});
|
|
181
|
-
fs.mkdirSync(routesManifestOutputDirPath, { recursive: true });
|
|
182
|
-
fs.writeFile(
|
|
183
|
-
path.join(routesManifestOutputDirPath, "manifest.json"),
|
|
184
|
-
JSON.stringify(routesManifest),
|
|
185
|
-
(err) => {
|
|
186
|
-
if (err) {
|
|
187
|
-
console.error(
|
|
188
|
-
"There was an error writing the routes manifest to disk.\nYou can ignore this error. It does not affect the runtime of your application."
|
|
189
|
-
);
|
|
190
|
-
console.error(err);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
);
|
|
194
|
-
} catch (err) {
|
|
195
|
-
console.error(
|
|
196
|
-
"There was an error writing the routes manifest to disk.\nYou can ignore this error. It does not affect the runtime of your application."
|
|
197
|
-
);
|
|
198
|
-
console.error(err);
|
|
199
|
-
}
|
|
200
|
-
return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`;
|
|
201
|
-
}
|
|
202
|
-
return void 0;
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
exports.startRoutesManifestPlugin = startRoutesManifestPlugin;
|
|
207
|
-
//# sourceMappingURL=plugin.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","sources":["../../../src/start-routes-manifest-plugin/plugin.ts"],"sourcesContent":["import { mkdirSync, readFileSync, rmSync, writeFile } from 'node:fs'\nimport path from 'node:path'\nimport { joinURL } from 'ufo'\nimport { rootRouteId } from '@tanstack/router-core'\nimport { resolveViteId } from '../utils'\nimport type {\n PluginOption,\n ResolvedConfig,\n Manifest as ViteManifest,\n ManifestChunk as ViteManifestChunk,\n} from 'vite'\nimport type { Manifest, RouterManagedTag } from '@tanstack/router-core'\nimport type { TanStackStartOutputConfig } from '../plugin'\n\nconst getCSSRecursively = (\n file: ViteManifestChunk,\n filesByRouteFilePath: ViteManifest,\n basePath: string,\n) => {\n const result: Array<RouterManagedTag> = []\n\n // Get all css imports from the file\n for (const cssFile of file.css ?? []) {\n result.push({\n tag: 'link',\n attrs: {\n rel: 'stylesheet',\n href: joinURL(basePath, cssFile),\n type: 'text/css',\n },\n })\n }\n\n // Recursively get CSS from imports\n for (const imp of file.imports ?? []) {\n const importInfo = filesByRouteFilePath[imp]\n if (importInfo) {\n result.push(\n ...getCSSRecursively(importInfo, filesByRouteFilePath, basePath),\n )\n }\n }\n\n return result\n}\n\nexport function startRoutesManifestPlugin(\n opts: TanStackStartOutputConfig,\n): PluginOption {\n let config: ResolvedConfig\n\n const moduleId = 'tanstack-start-router-manifest:v'\n const resolvedModuleId = resolveViteId(moduleId)\n\n return {\n name: 'tsr-routes-manifest',\n enforce: 'pre',\n\n configResolved(resolvedConfig) {\n config = resolvedConfig\n },\n // configEnvironment(env, envConfig) {\n // config = envConfig.\n // },\n resolveId(id) {\n if (id === moduleId) {\n return resolvedModuleId\n }\n return\n },\n load(id) {\n if (id === resolvedModuleId) {\n if (this.environment.config.consumer !== 'server') {\n // this will ultimately fail the build if the plugin is used outside the server environment\n // TODO: do we need special handling for `serve`?\n return `export default {}`\n }\n\n // If we're in development, return a dummy manifest\n if (config.command === 'serve') {\n return `export const tsrStartManifest = () => ({\n routes: {}\n })`\n }\n\n // This is the basepath for the application\n const APP_BASE = globalThis.TSS_APP_BASE\n\n const clientViteManifestPath = path.resolve(\n opts.root,\n '.tanstack-start/build/client-dist/.vite/manifest.json',\n )\n\n let viteManifest: ViteManifest\n try {\n viteManifest = JSON.parse(\n readFileSync(clientViteManifestPath, 'utf-8'),\n )\n } catch (err) {\n console.error(err)\n throw new Error(\n `Could not find the production client vite manifest at '${clientViteManifestPath}'!`,\n )\n }\n\n const routeTreePath = path.resolve(opts.tsr.generatedRouteTree)\n\n let routeTreeContent: string\n try {\n routeTreeContent = readFileSync(routeTreePath, 'utf-8')\n } catch (err) {\n console.error(err)\n throw new Error(\n `Could not find the generated route tree at '${routeTreePath}'!`,\n )\n }\n\n // Extract the routesManifest JSON from the route tree file.\n // It's located between the /* ROUTE_MANIFEST_START and ROUTE_MANIFEST_END */ comment block.\n\n const routerManifest = JSON.parse(\n routeTreeContent.match(\n /\\/\\* ROUTE_MANIFEST_START([\\s\\S]*?)ROUTE_MANIFEST_END \\*\\//,\n )?.[1] || '{ routes: {} }',\n ) as Manifest\n\n // This the manifest pulled from the generated route tree and later used by the Router.\n // i.e what's located in `src/generatedRouteTree.gen.ts`\n const routeTreeRoutes = routerManifest.routes\n\n // This is where hydration will start, from when the SSR'd page reaches the browser.\n // By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.\n let entryFile: ViteManifestChunk | undefined\n\n const filesByRouteFilePath: ViteManifest = Object.fromEntries(\n Object.entries(viteManifest).map(([k, v]) => {\n if (v.isEntry) {\n entryFile = v\n }\n\n const rPath = k.split('?')[0]\n\n return [rPath, v]\n }, {}),\n )\n\n const routesDirectoryFromRoot = path.relative(\n opts.root,\n opts.tsr.routesDirectory,\n )\n\n // Add preloads to the routes from the vite manifest\n Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {\n const file =\n filesByRouteFilePath[\n path.posix.join(routesDirectoryFromRoot, v.filePath as string)\n ]\n\n if (file) {\n // Map the relevant imports to their route paths,\n // so that it can be imported in the browser.\n const preloads = (file.imports ?? []).map((d) => {\n const assetPath = joinURL(APP_BASE, viteManifest[d]!.file)\n return assetPath\n })\n\n // Since this is the most important JS entry for the route,\n // it should be moved to the front of the preloads so that\n // it has the best chance of being loaded first.\n if (file.file) {\n preloads.unshift(path.join(APP_BASE, file.file))\n }\n\n const cssAssetsList = getCSSRecursively(\n file,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[routeId] = {\n ...v,\n assets: [...(v.assets || []), ...cssAssetsList],\n preloads,\n }\n }\n })\n\n if (entryFile) {\n routeTreeRoutes[rootRouteId]!.preloads = [\n joinURL(APP_BASE, entryFile.file),\n ...(entryFile.imports?.map((d) =>\n joinURL(APP_BASE, viteManifest[d]!.file),\n ) || []),\n ]\n\n // Gather all the CSS files from the entry file in\n // the `css` key and add them to the root route\n const entryCssAssetsList = getCSSRecursively(\n entryFile,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[rootRouteId]!.assets = [\n ...(routeTreeRoutes[rootRouteId]!.assets || []),\n ...entryCssAssetsList,\n {\n tag: 'script',\n attrs: {\n src: joinURL(APP_BASE, entryFile.file),\n type: 'module',\n },\n },\n ]\n }\n\n const recurseRoute = (\n route: {\n preloads?: Array<string>\n children?: Array<any>\n },\n seenPreloads = {} as Record<string, true>,\n ) => {\n route.preloads = route.preloads?.filter((preload) => {\n if (seenPreloads[preload]) {\n return false\n }\n seenPreloads[preload] = true\n return true\n })\n\n if (route.children) {\n route.children.forEach((child) => {\n const childRoute = routeTreeRoutes[child]!\n recurseRoute(childRoute, { ...seenPreloads })\n })\n }\n }\n\n // @ts-expect-error\n recurseRoute(routeTreeRoutes[rootRouteId])\n\n const routesManifest = {\n routes: routeTreeRoutes,\n }\n\n try {\n const routesManifestOutputDirPath = path.resolve(\n opts.root,\n '.tanstack-start/build/route-assets-manifest',\n )\n rmSync(routesManifestOutputDirPath, {\n recursive: true,\n force: true,\n })\n mkdirSync(routesManifestOutputDirPath, { recursive: true })\n writeFile(\n path.join(routesManifestOutputDirPath, 'manifest.json'),\n JSON.stringify(routesManifest),\n (err) => {\n if (err) {\n console.error(\n 'There was an error writing the routes manifest to disk.\\nYou can ignore this error. It does not affect the runtime of your application.',\n )\n console.error(err)\n }\n },\n )\n } catch (err) {\n console.error(\n 'There was an error writing the routes manifest to disk.\\nYou can ignore this error. It does not affect the runtime of your application.',\n )\n console.error(err)\n }\n\n return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`\n }\n\n return undefined\n },\n }\n}\n"],"names":["joinURL","resolveViteId","readFileSync","rootRouteId","_a","rmSync","mkdirSync","writeFile"],"mappings":";;;;;;;AAcA,MAAM,oBAAoB,CACxB,MACA,sBACA,aACG;AACH,QAAM,SAAkC,CAAC;AAGzC,aAAW,WAAW,KAAK,OAAO,CAAA,GAAI;AACpC,WAAO,KAAK;AAAA,MACV,KAAK;AAAA,MACL,OAAO;AAAA,QACL,KAAK;AAAA,QACL,MAAMA,IAAAA,QAAQ,UAAU,OAAO;AAAA,QAC/B,MAAM;AAAA,MAAA;AAAA,IACR,CACD;AAAA,EAAA;AAIH,aAAW,OAAO,KAAK,WAAW,CAAA,GAAI;AAC9B,UAAA,aAAa,qBAAqB,GAAG;AAC3C,QAAI,YAAY;AACP,aAAA;AAAA,QACL,GAAG,kBAAkB,YAAY,sBAAsB,QAAQ;AAAA,MACjE;AAAA,IAAA;AAAA,EACF;AAGK,SAAA;AACT;AAEO,SAAS,0BACd,MACc;AACV,MAAA;AAEJ,QAAM,WAAW;AACX,QAAA,mBAAmBC,oBAAc,QAAQ;AAExC,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,eAAe,gBAAgB;AACpB,eAAA;AAAA,IACX;AAAA;AAAA;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z } from 'zod';
|
|
2
|
-
export declare const configSchema: z.ZodObject<z.objectUtil.extendShape<{
|
|
3
|
-
target: z.ZodDefault<z.ZodOptional<z.ZodEnum<["react", "solid"]>>>;
|
|
4
|
-
virtualRouteConfig: z.ZodOptional<z.ZodUnion<[z.ZodType<import('@tanstack/virtual-file-routes').VirtualRootRoute, z.ZodTypeDef, import('@tanstack/virtual-file-routes').VirtualRootRoute>, z.ZodString]>>;
|
|
5
|
-
routeFilePrefix: z.ZodOptional<z.ZodString>;
|
|
6
|
-
routeFileIgnorePrefix: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
7
|
-
routeFileIgnorePattern: z.ZodOptional<z.ZodString>;
|
|
8
|
-
routesDirectory: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
9
|
-
quoteStyle: z.ZodDefault<z.ZodOptional<z.ZodEnum<["single", "double"]>>>;
|
|
10
|
-
semicolons: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
11
|
-
disableLogging: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
12
|
-
routeTreeFileHeader: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
|
|
13
|
-
indexToken: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
14
|
-
routeToken: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
15
|
-
pathParamsAllowedCharacters: z.ZodOptional<z.ZodArray<z.ZodEnum<[";", ":", "@", "&", "=", "+", "$", ","]>, "many">>;
|
|
16
|
-
}, {
|
|
17
|
-
srcDirectory: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
18
|
-
}>, "strip", z.ZodTypeAny, {
|
|
19
|
-
target: "react" | "solid";
|
|
20
|
-
routeFileIgnorePrefix: string;
|
|
21
|
-
routesDirectory: string;
|
|
22
|
-
quoteStyle: "single" | "double";
|
|
23
|
-
semicolons: boolean;
|
|
24
|
-
disableLogging: boolean;
|
|
25
|
-
routeTreeFileHeader: string[];
|
|
26
|
-
indexToken: string;
|
|
27
|
-
routeToken: string;
|
|
28
|
-
srcDirectory: string;
|
|
29
|
-
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined;
|
|
30
|
-
routeFilePrefix?: string | undefined;
|
|
31
|
-
routeFileIgnorePattern?: string | undefined;
|
|
32
|
-
pathParamsAllowedCharacters?: (";" | ":" | "@" | "&" | "=" | "+" | "$" | ",")[] | undefined;
|
|
33
|
-
}, {
|
|
34
|
-
target?: "react" | "solid" | undefined;
|
|
35
|
-
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined;
|
|
36
|
-
routeFilePrefix?: string | undefined;
|
|
37
|
-
routeFileIgnorePrefix?: string | undefined;
|
|
38
|
-
routeFileIgnorePattern?: string | undefined;
|
|
39
|
-
routesDirectory?: string | undefined;
|
|
40
|
-
quoteStyle?: "single" | "double" | undefined;
|
|
41
|
-
semicolons?: boolean | undefined;
|
|
42
|
-
disableLogging?: boolean | undefined;
|
|
43
|
-
routeTreeFileHeader?: string[] | undefined;
|
|
44
|
-
indexToken?: string | undefined;
|
|
45
|
-
routeToken?: string | undefined;
|
|
46
|
-
pathParamsAllowedCharacters?: (";" | ":" | "@" | "&" | "=" | "+" | "$" | ",")[] | undefined;
|
|
47
|
-
srcDirectory?: string | undefined;
|
|
48
|
-
}>;
|
|
49
|
-
export type Config = z.infer<typeof configSchema>;
|