Skip to content

Commit eda4a1b

Browse files
committed
cleanup virtual modules and manifests
1 parent facc538 commit eda4a1b

16 files changed

Lines changed: 92 additions & 170 deletions

packages/start/src/config/index.ts

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { createTanStackServerFnPlugin } from "@tanstack/server-functions-plugin";
2-
import { defu } from "defu";
31
import { existsSync } from "node:fs";
42
import path, { isAbsolute, join, normalize } from "node:path";
53
import { fileURLToPath } from "node:url";
6-
import type { StartServerManifest } from "solid-start:server-manifest";
4+
import { createTanStackServerFnPlugin } from "@tanstack/server-functions-plugin";
5+
import { defu } from "defu";
76
import { normalizePath, type PluginOption, type Rollup, type ViteDevServer } from "vite";
87
import solid, { type Options as SolidOptions } from "vite-plugin-solid";
98

109
import { isCssModulesFile } from "../server/collect-styles.js";
11-
import { getSsrDevManifest } from "../server/manifest/dev-server-manifest.js";
10+
import { getSsrDevManifest } from "../server/manifest/dev-ssr-manifest.js";
1211
import { SolidStartClientFileRouter, SolidStartServerFileRouter } from "./fs-router.js";
1312
import { fsRoutes } from "./fs-routes/index.js";
13+
import type { BaseFileSystemRouter } from "./fs-routes/router.js";
1414
import {
1515
clientDistDir,
1616
nitroPlugin,
1717
serverDistDir,
1818
ssrEntryFile,
1919
type UserNitroConfig
2020
} from "./nitroPlugin.js";
21-
import { BaseFileSystemRouter } from "./fs-routes/router.js";
21+
import { CLIENT_BASE_PATH } from "../constants.js";
2222

2323
const DEFAULT_EXTENSIONS = ["js", "jsx", "ts", "tsx"];
2424

@@ -70,15 +70,12 @@ const absolute = (path: string, root: string) =>
7070
let ssrBundle: Rollup.OutputBundle;
7171

7272
const VIRTUAL_MODULES = {
73-
serverManifest: "solid-start:server-manifest",
73+
clientViteManifest: "solid-start:client-vite-manifest",
7474
getClientManifest: "solid-start:get-client-manifest",
75-
getSsrManifest: "solid-start:get-ssr-manifest",
7675
getManifest: "solid-start:get-manifest",
7776
middleware: "solid-start:middleware"
7877
} as const;
7978

80-
export const CLIENT_BASE_PATH = "_build";
81-
8279
function solidStartVitePlugin(options?: SolidStartOptions): Array<PluginOption> {
8380
const start = defu(options ?? {}, {
8481
appRoot: "./src",
@@ -127,7 +124,7 @@ function solidStartVitePlugin(options?: SolidStartOptions): Array<PluginOption>
127124
};
128125
},
129126
async config(_, env) {
130-
let clientInput = [handlers.client];
127+
const clientInput = [handlers.client];
131128

132129
if (env.command === "build") {
133130
const clientRouter: BaseFileSystemRouter = (globalThis as any).ROUTERS.client
@@ -168,8 +165,6 @@ function solidStartVitePlugin(options?: SolidStartOptions): Array<PluginOption>
168165
consumer: "server",
169166
build: {
170167
ssr: true,
171-
// we don't write to the file system as the below 'capture-output' plugin will
172-
// capture the output and write it to the virtual file system
173168
write: true,
174169
manifest: true,
175170
copyPublicDir: false,
@@ -244,46 +239,41 @@ function solidStartVitePlugin(options?: SolidStartOptions): Array<PluginOption>
244239
name: "solid-start:manifest-plugin",
245240
enforce: "pre",
246241
async resolveId(id) {
247-
if (id === VIRTUAL_MODULES.serverManifest) return `\0${VIRTUAL_MODULES.serverManifest}`;
242+
if (id === VIRTUAL_MODULES.clientViteManifest) return `\0${VIRTUAL_MODULES.clientViteManifest}`;
248243
if (id === VIRTUAL_MODULES.getClientManifest)
249244
return new URL("../server/manifest/client-manifest.js", import.meta.url).pathname;
250-
if (id === VIRTUAL_MODULES.getSsrManifest)
251-
return new URL("../server/manifest/ssr-manifest.js", import.meta.url).pathname;
252-
if (id === VIRTUAL_MODULES.getManifest)
253-
return this.environment.config.consumer === "server"
254-
? new URL("../server/manifest/ssr-manifest.js", import.meta.url).pathname
255-
: new URL("../server/manifest/client-manifest.js", import.meta.url).pathname;
245+
if (id === VIRTUAL_MODULES.getManifest) {
246+
return this.environment.config.consumer === "client" ?
247+
new URL("../server/manifest/client-manifest.js", import.meta.url).pathname :
248+
new URL("../server/manifest/ssr-manifest.js", import.meta.url).pathname;
249+
}
256250
if (id === VIRTUAL_MODULES.middleware) {
257251
if (start.middleware) return await this.resolve(start.middleware);
258252

259253
return `\0${VIRTUAL_MODULES.middleware}`;
260254
}
261255
},
262256
async load(id) {
263-
if (id === `\0${VIRTUAL_MODULES.serverManifest}`) {
257+
if (id === `\0${VIRTUAL_MODULES.clientViteManifest}`) {
258+
let clientViteManifest: Record<string, Record<string, any>>;
259+
264260
if (this.environment.config.command === "serve") {
265-
const manifest: StartServerManifest = {
266-
clientViteManifest: {},
267-
};
261+
clientViteManifest = {};
262+
} else {
263+
const entry = Object.values(globalThis.START_CLIENT_BUNDLE).find(
264+
v => "isEntry" in v && v.isEntry
265+
);
266+
if (!entry) throw new Error("No client entry found");
268267

269-
return `export const manifest = ${JSON.stringify(manifest)}`;
268+
clientViteManifest = JSON.parse(
269+
(globalThis.START_CLIENT_BUNDLE[".vite/manifest.json"] as any).source
270+
);
270271
}
271272

272-
const entry = Object.values(globalThis.START_CLIENT_BUNDLE).find(
273-
v => "isEntry" in v && v.isEntry
274-
);
275-
if (!entry) throw new Error("No client entry found");
276-
277-
const clientManifest: Record<string, Record<string, any>> = JSON.parse(
278-
(globalThis.START_CLIENT_BUNDLE[".vite/manifest.json"] as any).source
279-
);
280-
281-
const manifest: StartServerManifest = {
282-
clientViteManifest: clientManifest as any,
283-
};
284-
285-
return `export const manifest = ${JSON.stringify(manifest)};`;
286-
} else if (id.startsWith("/@manifest")) {
273+
return `export const clientViteManifest = ${JSON.stringify(clientViteManifest)};`;
274+
}
275+
else if (id === `\0${VIRTUAL_MODULES.middleware}`) return "export default {};"
276+
else if (id.startsWith("/@manifest")) {
287277
const [path, query] = id.split("?");
288278
const params = new URLSearchParams(query);
289279
if (!path || !query) return;
@@ -296,7 +286,7 @@ function solidStartVitePlugin(options?: SolidStartOptions): Array<PluginOption>
296286
await getSsrDevManifest("server").getAssets(id)
297287
)}`;
298288
}
299-
} else if (id === `\0${VIRTUAL_MODULES.middleware}`) return "export default {};"
289+
}
300290
}
301291
},
302292
nitroPlugin({ root: process.cwd() }, () => ssrBundle, start.server),

packages/start/src/config/nitroPlugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export function nitroPlugin(
180180
logLevel: 3,
181181
preset: "node-server",
182182
typescript: {
183-
generateTsConfig: false
183+
generateTsConfig: false,
184+
generateRuntimeConfigTypes: false
184185
},
185186
...nitroConfig,
186187
dev: false,

packages/start/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CLIENT_BASE_PATH = "_build";

packages/start/src/router.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { getRequestEvent, isServer } from "solid-js/web";
21
import { getManifest } from "solid-start:get-manifest";
2+
import { getRequestEvent, isServer } from "solid-js/web";
33

44
import lazyRoute from "./server/lazyRoute.jsx";
5-
import type { PageEvent } from "./server/types.js";
65
import { pageRoutes as routeConfigs } from "./server/routes.js";
6+
import type { PageEvent } from "./server/types.js";
77

88
export function createRoutes() {
99
function createRoute(route: any) {
@@ -16,7 +16,11 @@ export function createRoutes() {
1616
},
1717
component:
1818
route.$component &&
19-
lazyRoute(route.$component, getManifest(import.meta.env.START_ISLANDS), getManifest(true)),
19+
lazyRoute(
20+
route.$component,
21+
getManifest(import.meta.env.START_ISLANDS ? "server" : "client"),
22+
getManifest("server")
23+
),
2024
children: route.children ? route.children.map(createRoute) : undefined
2125
};
2226
}

packages/start/src/server/StartServer.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import {
99
ssr,
1010
useAssets
1111
} from "solid-js/web";
12-
import { getManifest } from "solid-start:get-manifest";
13-
import { manifest } from "solid-start:server-manifest";
1412

1513
import { ErrorBoundary, TopErrorBoundary } from "../shared/ErrorBoundary.jsx";
1614
import { renderAsset } from "./renderAsset.jsx";
17-
import { getClientEntryPath } from "./server-manifest.js";
1815
import type { Asset, DocumentComponentProps, PageEvent } from "./types.js";
16+
import { getSsrManifest } from "./manifest/ssr-manifest.js";
1917

2018
const docType = ssr("<!DOCTYPE html>");
2119

@@ -46,7 +44,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
4644
let assets: Asset[] = [];
4745
Promise.resolve()
4846
.then(async () => {
49-
const manifest = getManifest(import.meta.env.START_ISLANDS);
47+
const manifest = getSsrManifest(import.meta.env.START_ISLANDS);
5048

5149
let assetPromises: Promise<Asset[]>[] = [];
5250
// @ts-ignore
@@ -93,7 +91,12 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
9391
nonce={nonce}
9492
innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`}
9593
/>
96-
<script type="module" nonce={nonce} async src={getClientEntryPath()} />
94+
<script
95+
type="module"
96+
nonce={nonce}
97+
async
98+
src={getSsrManifest("client").path(import.meta.env.START_CLIENT_ENTRY)}
99+
/>
97100
</>
98101
}
99102
>

packages/start/src/server/collect-styles.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import path from "node:path";
22
import { join, resolve } from "pathe";
33
import type { ModuleNode, ViteDevServer } from "vite";
4-
5-
import { CLIENT_BASE_PATH } from "../config/index.js";
4+
import { CLIENT_BASE_PATH } from "../constants";
65

76
async function getViteModuleNode(vite: ViteDevServer, file: string, ssr = false) {
87
let nodePath = file;
@@ -162,34 +161,3 @@ export async function findStylesInModuleGraph(vite: ViteDevServer, id: string, s
162161

163162
return styles
164163
}
165-
166-
export async function getManifestEntryCssTags(id: string) {
167-
if (import.meta.env.DEV) {
168-
const styles = await findStylesInModuleGraph((globalThis as any).VITE_DEV_SERVER, id, false);
169-
170-
return Object.entries(
171-
styles
172-
).map(([key, value]) => ({
173-
tag: "style",
174-
attrs: {
175-
type: "text/css",
176-
key,
177-
"data-vite-dev-id": key,
178-
"data-vite-ref": "0",
179-
},
180-
children: value,
181-
}))
182-
} else {
183-
const { manifest } = await import("solid-start:server-manifest");
184-
185-
const entry = manifest.clientViteManifest[id];
186-
if (!entry) throw new Error(`No entry '${id}' found in vite manifest`);
187-
188-
return (
189-
entry.css?.map(css => ({
190-
tag: "link",
191-
attrs: { href: `/${"_build"}/${css}`, rel: "stylesheet" }
192-
})) ?? []
193-
);
194-
}
195-
}

packages/start/src/server/handler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { createRoutes } from "../router.jsx";
1010
import { getFetchEvent } from "./fetchEvent.js";
1111
import { matchAPIRoute } from "./routes.js";
1212
import { handleServerFunction } from "./server-functions-handler.js";
13-
import { getClientEntryCssTags } from "./server-manifest.js";
1413
import type { APIEvent, FetchEvent, HandlerOptions, PageEvent } from "./types.js";
1514
import { getSsrManifest } from "./manifest/ssr-manifest.js";
1615

packages/start/src/server/lazyRoute.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { type Component, createComponent, JSX, lazy, onCleanup } from "solid-js";
1+
import { type Component, createComponent, type JSX, lazy, onCleanup } from "solid-js";
22

3-
import { Asset, renderAsset } from "./renderAsset.jsx";
3+
import { type Asset, renderAsset } from "./renderAsset.jsx";
44

55
export default function lazyRoute<T extends Record<string, any>>(
66
component: { src: string; import(): Promise<Record<string, Component>> },

packages/start/src/server/manifest/dev-client-manifest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export function getClientDevManifest() {
1414

1515
return (await import(/* @vite-ignore */ assetsPath)).default;
1616
},
17-
} satisfies StartManifest;
17+
} satisfies StartManifest & { import(id: string): Promise<any> };
1818
}

packages/start/src/server/manifest/dev-server-manifest.ts renamed to packages/start/src/server/manifest/dev-ssr-manifest.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { isAbsolute, join } from "pathe";
2-
import type { ViteDevServer } from "vite";
1+
import { join, resolve } from "pathe";
2+
import { normalizePath, type ViteDevServer } from "vite";
33

44
import { findStylesInModuleGraph } from "../collect-styles.js";
55

66
export function getSsrDevManifest(target: "client" | "server") {
77
const vite: ViteDevServer = (globalThis as any).VITE_DEV_SERVER;
88

99
return {
10-
import(id: string) {
11-
const absolutePath = isAbsolute(id) ? id : join(process.cwd(), id);
12-
return vite.ssrLoadModule(join(absolutePath));
13-
},
10+
path: (id: string) => normalizePath(join("/@fs", resolve(process.cwd(), id))),
1411
async getAssets(id: string) {
1512
const styles = await findStylesInModuleGraph(vite, id, target === "server");
1613

@@ -25,7 +22,11 @@ export function getSsrDevManifest(target: "client" | "server") {
2522
children: value,
2623
}));
2724
},
28-
} satisfies StartManifest;
25+
} satisfies StartManifest & {
26+
path(
27+
id: string,
28+
): string
29+
}
2930
}
3031

3132
export { getSsrDevManifest as getSsrManifest }

0 commit comments

Comments
 (0)