@solidjs/start 1.1.7 → 2.0.0-alpha.0
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/README.md +88 -8
- package/dist/client/StartClient.d.ts +0 -1
- package/dist/client/StartClient.jsx +2 -4
- package/dist/client/index.d.ts +2 -3
- package/dist/client/index.jsx +2 -4
- package/dist/client/mount.js +10 -2
- package/dist/client/spa/index.d.ts +1 -2
- package/dist/client/spa/index.jsx +1 -2
- package/dist/config/constants.d.ts +16 -0
- package/dist/config/constants.js +16 -0
- package/dist/config/dev-server.d.ts +2 -0
- package/dist/config/dev-server.js +108 -0
- package/dist/config/fs-router.d.ts +37 -0
- package/dist/config/fs-router.js +153 -0
- package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
- package/dist/config/fs-routes/fs-watcher.js +49 -0
- package/dist/config/fs-routes/index.d.ts +7 -0
- package/dist/config/fs-routes/index.js +109 -0
- package/dist/config/fs-routes/router.d.ts +37 -0
- package/dist/config/fs-routes/router.js +136 -0
- package/dist/config/fs-routes/tree-shake.d.ts +2 -0
- package/dist/config/fs-routes/tree-shake.js +357 -0
- package/dist/config/index.d.ts +10 -0
- package/dist/config/index.js +217 -0
- package/dist/config/lazy.d.ts +3 -0
- package/dist/config/lazy.js +82 -0
- package/dist/config/manifest.d.ts +3 -0
- package/dist/config/manifest.js +74 -0
- package/dist/config/utils.d.ts +4 -0
- package/dist/config/utils.js +6 -0
- package/dist/config/vite-utils.d.ts +16 -0
- package/dist/config/vite-utils.js +81 -0
- package/dist/http/index.d.ts +84 -0
- package/dist/http/index.js +131 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.jsx +5 -5
- package/dist/middleware/index.d.ts +6 -6
- package/dist/middleware/index.js +39 -0
- package/dist/{router/FileRoutes.js → router.jsx} +5 -6
- package/dist/server/StartServer.d.ts +3 -3
- package/dist/server/StartServer.jsx +9 -63
- package/dist/server/assets/index.d.ts +2 -0
- package/dist/server/assets/index.js +52 -0
- package/dist/server/assets/render.d.ts +19 -0
- package/dist/server/{renderAsset.jsx → assets/render.jsx} +1 -1
- package/dist/server/collect-styles.d.ts +3 -0
- package/dist/server/collect-styles.js +69 -0
- package/dist/server/fetchEvent.d.ts +4 -3
- package/dist/server/fetchEvent.js +12 -74
- package/dist/server/fetchEvent.spec.d.ts +1 -0
- package/dist/server/fetchEvent.spec.js +65 -0
- package/dist/server/handler.d.ts +6 -12
- package/dist/server/handler.js +180 -95
- package/dist/server/index.d.ts +8 -6
- package/dist/server/index.jsx +14 -6
- package/dist/server/manifest/client-manifest.d.ts +9 -0
- package/dist/server/manifest/client-manifest.js +6 -0
- package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
- package/dist/server/manifest/dev-client-manifest.js +24 -0
- package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
- package/dist/server/manifest/dev-ssr-manifest.js +23 -0
- package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
- package/dist/server/manifest/prod-client-manifest.js +26 -0
- package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
- package/dist/server/manifest/prod-ssr-manifest.js +90 -0
- package/dist/server/manifest/ssr-manifest.d.ts +4 -0
- package/dist/server/manifest/ssr-manifest.js +6 -0
- package/dist/{router → server}/routes.d.ts +6 -10
- package/dist/{router → server}/routes.js +35 -27
- package/dist/{runtime → server}/server-fns-runtime.d.ts +1 -1
- package/dist/{runtime → server}/server-fns-runtime.js +7 -6
- package/dist/server/server-functions-handler.d.ts +2 -0
- package/dist/{runtime/server-handler.js → server/server-functions-handler.js} +76 -103
- package/dist/server/server-runtime.d.ts +3 -0
- package/dist/{runtime → server}/server-runtime.js +9 -16
- package/dist/server/spa/StartServer.d.ts +1 -1
- package/dist/server/spa/StartServer.jsx +7 -9
- package/dist/server/spa/handler.d.ts +3 -3
- package/dist/server/spa/handler.js +9 -11
- package/dist/server/spa/index.d.ts +4 -3
- package/dist/server/spa/index.jsx +4 -3
- package/dist/server/types.d.ts +7 -9
- package/dist/server/util.d.ts +2 -0
- package/dist/server/util.js +8 -0
- package/dist/shared/ErrorBoundary.jsx +2 -2
- package/dist/shared/HttpHeader.jsx +4 -2
- package/dist/shared/{HttpStatusCode.jsx → HttpStatusCode.js} +3 -1
- package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +2 -2
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +75 -66
- package/dist/shared/dev-overlay/createStackFrame.js +3 -3
- package/dist/shared/dev-overlay/index.jsx +5 -8
- package/dist/shared/lazy.d.ts +3 -0
- package/dist/shared/lazy.js +40 -0
- package/dist/shared/serverFunction.d.ts +1 -1
- package/package.json +47 -65
- package/scripts/build.js +15 -0
- package/scripts/validate-imports.js +316 -0
- package/src/client/StartClient.tsx +34 -0
- package/src/client/index.tsx +2 -0
- package/src/client/mount.ts +114 -0
- package/src/client/spa/index.tsx +9 -0
- package/src/config/constants.ts +19 -0
- package/src/config/dev-server.ts +149 -0
- package/src/config/fs-router.ts +177 -0
- package/src/config/fs-routes/fs-watcher.ts +81 -0
- package/src/config/fs-routes/index.ts +138 -0
- package/src/config/fs-routes/router.ts +169 -0
- package/src/config/fs-routes/tree-shake.ts +392 -0
- package/src/config/index.ts +236 -0
- package/src/config/lazy.ts +100 -0
- package/src/config/manifest.ts +84 -0
- package/src/config/utils.ts +5 -0
- package/src/config/vite-utils.ts +113 -0
- package/src/env.d.ts +35 -0
- package/src/http/index.ts +253 -0
- package/src/index.tsx +19 -0
- package/src/internal.d.ts +14 -0
- package/src/middleware/index.ts +82 -0
- package/src/router.tsx +36 -0
- package/src/server/StartServer.tsx +62 -0
- package/src/server/assets/index.ts +70 -0
- package/src/server/assets/render.tsx +42 -0
- package/src/server/collect-styles.ts +94 -0
- package/src/server/fetchEvent.spec.ts +89 -0
- package/src/server/fetchEvent.ts +36 -0
- package/src/server/handler.ts +250 -0
- package/src/server/index.tsx +16 -0
- package/src/server/manifest/client-manifest.ts +8 -0
- package/src/server/manifest/dev-client-manifest.ts +19 -0
- package/src/server/manifest/dev-ssr-manifest.ts +21 -0
- package/src/server/manifest/prod-client-manifest.ts +18 -0
- package/src/server/manifest/prod-ssr-manifest.ts +125 -0
- package/src/server/manifest/ssr-manifest.ts +8 -0
- package/src/server/routes.ts +138 -0
- package/src/server/server-fns-runtime.ts +31 -0
- package/src/server/server-functions-handler.ts +364 -0
- package/src/server/server-runtime.ts +240 -0
- package/src/server/spa/StartServer.tsx +40 -0
- package/src/server/spa/handler.ts +29 -0
- package/src/server/spa/index.tsx +5 -0
- package/src/server/types.ts +82 -0
- package/src/server/util.ts +12 -0
- package/src/shared/ErrorBoundary.tsx +52 -0
- package/src/shared/GET.ts +8 -0
- package/src/shared/HttpHeader.tsx +42 -0
- package/src/shared/HttpStatusCode.ts +27 -0
- package/src/shared/clientOnly.ts +44 -0
- package/src/shared/dev-overlay/CodeView.tsx +95 -0
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +316 -0
- package/src/shared/dev-overlay/createStackFrame.ts +77 -0
- package/src/shared/dev-overlay/download.ts +6 -0
- package/src/shared/dev-overlay/env.d.ts +4 -0
- package/src/shared/dev-overlay/get-source-map.ts +35 -0
- package/src/shared/dev-overlay/icons.tsx +288 -0
- package/src/shared/dev-overlay/index.tsx +63 -0
- package/src/shared/dev-overlay/styles.css +292 -0
- package/src/shared/lazy.ts +51 -0
- package/src/shared/serverFunction.ts +10 -0
- package/src/virtual.d.ts +27 -0
- package/tsconfig.json +23 -0
- package/vitest.config.ts +9 -0
- package/config/fs-router.js +0 -142
- package/config/index.d.ts +0 -39
- package/config/index.js +0 -303
- package/config/server-components.js +0 -106
- package/dist/client/islands.d.ts +0 -7
- package/dist/client/islands.jsx +0 -10
- package/dist/middleware/index.jsx +0 -35
- package/dist/router/index.d.ts +0 -1
- package/dist/router/index.jsx +0 -2
- package/dist/router/lazyRoute.d.ts +0 -6
- package/dist/router/lazyRoute.js +0 -53
- package/dist/runtime/server-handler.d.ts +0 -2
- package/dist/runtime/server-runtime.d.ts +0 -3
- package/dist/server/islands/index.d.ts +0 -19
- package/dist/server/islands/index.jsx +0 -62
- package/dist/server/islands/utils.d.ts +0 -1
- package/dist/server/islands/utils.jsx +0 -85
- package/dist/server/pageEvent.d.ts +0 -2
- package/dist/server/pageEvent.js +0 -56
- package/dist/server/renderAsset.d.ts +0 -2
- package/env.d.ts +0 -26
- /package/dist/{router/FileRoutes.d.ts → router.d.ts} +0 -0
- /package/dist/shared/{clientOnly.jsx → clientOnly.js} +0 -0
- /package/dist/shared/{serverFunction.jsx → serverFunction.js} +0 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// @refresh skip
|
|
2
|
+
|
|
3
|
+
import type { H3Event, Middleware } from "h3";
|
|
4
|
+
import { getFetchEvent } from "../server/fetchEvent.ts";
|
|
5
|
+
import type { FetchEvent } from "../server/types.ts";
|
|
6
|
+
|
|
7
|
+
/** Function responsible for receiving an observable [operation]{@link Operation} and returning a [result]{@link OperationResult}. */
|
|
8
|
+
|
|
9
|
+
export type MiddlewareFn = (event: FetchEvent) => Promise<unknown> | unknown;
|
|
10
|
+
/** This composes an array of Exchanges into a single ExchangeIO function */
|
|
11
|
+
|
|
12
|
+
export type RequestMiddleware = (
|
|
13
|
+
event: FetchEvent,
|
|
14
|
+
) =>
|
|
15
|
+
| Response
|
|
16
|
+
| Promise<Response>
|
|
17
|
+
| void
|
|
18
|
+
| Promise<void>
|
|
19
|
+
| Promise<void | Response>;
|
|
20
|
+
|
|
21
|
+
// copy-pasted from h3/dist/index.d.ts
|
|
22
|
+
type EventHandlerResponse<T = any> = T | Promise<T>;
|
|
23
|
+
type ResponseMiddlewareResponseParam = { body?: Awaited<EventHandlerResponse> };
|
|
24
|
+
|
|
25
|
+
export type ResponseMiddleware = (
|
|
26
|
+
event: FetchEvent,
|
|
27
|
+
response: ResponseMiddlewareResponseParam,
|
|
28
|
+
) => Response | Promise<Response> | void | Promise<void>;
|
|
29
|
+
|
|
30
|
+
function wrapRequestMiddleware(onRequest: RequestMiddleware) {
|
|
31
|
+
return async (h3Event: H3Event) => {
|
|
32
|
+
const fetchEvent = getFetchEvent(h3Event);
|
|
33
|
+
const response = await onRequest(fetchEvent);
|
|
34
|
+
if (response) return response;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function wrapResponseMiddleware(
|
|
39
|
+
onBeforeResponse: ResponseMiddleware,
|
|
40
|
+
): Middleware {
|
|
41
|
+
return async (h3Event, next) => {
|
|
42
|
+
const resp = await next();
|
|
43
|
+
|
|
44
|
+
const fetchEvent = getFetchEvent(h3Event);
|
|
45
|
+
const mwResponse = await onBeforeResponse(fetchEvent, {
|
|
46
|
+
body: (resp as any)?.body,
|
|
47
|
+
});
|
|
48
|
+
if (mwResponse) return mwResponse;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createMiddleware(
|
|
53
|
+
args:
|
|
54
|
+
| {
|
|
55
|
+
/** @deprecated Use H3 `Middleware` */
|
|
56
|
+
onRequest?: RequestMiddleware | RequestMiddleware[] | undefined;
|
|
57
|
+
/** @deprecated Use H3 `Middleware` */
|
|
58
|
+
onBeforeResponse?:
|
|
59
|
+
| ResponseMiddleware
|
|
60
|
+
| ResponseMiddleware[]
|
|
61
|
+
| undefined;
|
|
62
|
+
}
|
|
63
|
+
| Middleware[],
|
|
64
|
+
): Middleware[] {
|
|
65
|
+
if (Array.isArray(args)) return args;
|
|
66
|
+
|
|
67
|
+
const mw: Middleware[] = [];
|
|
68
|
+
|
|
69
|
+
if (typeof args.onRequest === "function") {
|
|
70
|
+
mw.push(wrapRequestMiddleware(args.onRequest));
|
|
71
|
+
} else if (Array.isArray(args.onRequest)) {
|
|
72
|
+
mw.push(...args.onRequest.map(wrapRequestMiddleware));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof args.onBeforeResponse === "function") {
|
|
76
|
+
mw.push(wrapResponseMiddleware(args.onBeforeResponse));
|
|
77
|
+
} else if (Array.isArray(args.onBeforeResponse)) {
|
|
78
|
+
mw.push(...args.onBeforeResponse.map(wrapResponseMiddleware));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return mw;
|
|
82
|
+
}
|
package/src/router.tsx
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { lazy, type Component } from "solid-js";
|
|
2
|
+
import { getRequestEvent, isServer } from "solid-js/web";
|
|
3
|
+
|
|
4
|
+
import { pageRoutes as routeConfigs } from "./server/routes.ts";
|
|
5
|
+
import type { PageEvent } from "./server/types.ts";
|
|
6
|
+
|
|
7
|
+
const components: Record<string, Component> = {};
|
|
8
|
+
|
|
9
|
+
export function createRoutes() {
|
|
10
|
+
function createRoute(route: any) {
|
|
11
|
+
const component = route.$component && (components[route.$component.src] ??= lazy(route.$component.import));
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
...route,
|
|
15
|
+
...(route.$$route ? route.$$route.require().route : undefined),
|
|
16
|
+
info: {
|
|
17
|
+
...(route.$$route ? route.$$route.require().route.info : {}),
|
|
18
|
+
filesystem: true
|
|
19
|
+
},
|
|
20
|
+
component,
|
|
21
|
+
children: route.children ? route.children.map(createRoute) : undefined
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const routes = routeConfigs.map(createRoute);
|
|
25
|
+
return routes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let routes: any[];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* Read more: https://docs.solidjs.com/solid-start/reference/routing/file-routes
|
|
33
|
+
*/
|
|
34
|
+
export const FileRoutes = isServer
|
|
35
|
+
? () => (getRequestEvent() as PageEvent).routes
|
|
36
|
+
: () => routes || (routes = createRoutes());
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// @refresh skip
|
|
2
|
+
import type { Component } from "solid-js";
|
|
3
|
+
import {
|
|
4
|
+
Hydration,
|
|
5
|
+
HydrationScript,
|
|
6
|
+
NoHydration,
|
|
7
|
+
getRequestEvent,
|
|
8
|
+
ssr
|
|
9
|
+
} from "solid-js/web";
|
|
10
|
+
import App from "solid-start:app";
|
|
11
|
+
|
|
12
|
+
import { ErrorBoundary, TopErrorBoundary } from "../shared/ErrorBoundary.tsx";
|
|
13
|
+
import { useAssets } from "./assets/index.ts";
|
|
14
|
+
import { getSsrManifest } from "./manifest/ssr-manifest.ts";
|
|
15
|
+
import type { DocumentComponentProps, PageEvent } from "./types.ts";
|
|
16
|
+
|
|
17
|
+
const docType = ssr("<!DOCTYPE html>");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* Read more: https://docs.solidjs.com/solid-start/reference/server/start-server
|
|
22
|
+
*/
|
|
23
|
+
export function StartServer(props: { document: Component<DocumentComponentProps> }) {
|
|
24
|
+
const context = getRequestEvent() as PageEvent;
|
|
25
|
+
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
const nonce = context.nonce;
|
|
28
|
+
useAssets(context.assets, nonce);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<NoHydration>
|
|
32
|
+
{docType as unknown as any}
|
|
33
|
+
<TopErrorBoundary>
|
|
34
|
+
<props.document
|
|
35
|
+
assets={<HydrationScript />}
|
|
36
|
+
scripts={
|
|
37
|
+
<>
|
|
38
|
+
<script
|
|
39
|
+
type="module"
|
|
40
|
+
nonce={nonce}
|
|
41
|
+
async
|
|
42
|
+
src={getSsrManifest("client").path(import.meta.env.START_CLIENT_ENTRY)}
|
|
43
|
+
/>
|
|
44
|
+
</>
|
|
45
|
+
}
|
|
46
|
+
>
|
|
47
|
+
{!import.meta.env.START_ISLANDS ? (
|
|
48
|
+
<Hydration>
|
|
49
|
+
<ErrorBoundary>
|
|
50
|
+
<App />
|
|
51
|
+
</ErrorBoundary>
|
|
52
|
+
</Hydration>
|
|
53
|
+
) : (
|
|
54
|
+
<ErrorBoundary>
|
|
55
|
+
<App />
|
|
56
|
+
</ErrorBoundary>
|
|
57
|
+
)}
|
|
58
|
+
</props.document>
|
|
59
|
+
</TopErrorBoundary>
|
|
60
|
+
</NoHydration>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { onCleanup, sharedConfig } from "solid-js";
|
|
2
|
+
import { getRequestEvent, useAssets as useAssets_ } from "solid-js/web";
|
|
3
|
+
import { renderAsset, type Asset } from "./render.tsx";
|
|
4
|
+
|
|
5
|
+
const REGISTRY = Symbol("assetRegistry");
|
|
6
|
+
const NOOP = () => "";
|
|
7
|
+
|
|
8
|
+
type AssetEntity = {
|
|
9
|
+
key: string;
|
|
10
|
+
consumers: number;
|
|
11
|
+
preloadEl?: HTMLLinkElement;
|
|
12
|
+
ssrIdx?: number;
|
|
13
|
+
};
|
|
14
|
+
type Registry = Record<string, AssetEntity>;
|
|
15
|
+
|
|
16
|
+
const keyAttrs = ["href", "rel", "data-vite-dev-id"] as const;
|
|
17
|
+
|
|
18
|
+
const getEntity = (registry: Registry, asset: Asset) => {
|
|
19
|
+
let key = asset.tag;
|
|
20
|
+
for (const k of keyAttrs) {
|
|
21
|
+
if (!(k in asset.attrs)) continue;
|
|
22
|
+
key += `[${k}='${asset.attrs[k as keyof Asset["attrs"]]}']`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const entity = (registry[key] ??= {
|
|
26
|
+
key,
|
|
27
|
+
consumers: 0
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return entity;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const useAssets = (assets: Asset[], nonce?: string) => {
|
|
34
|
+
if (!assets.length) return;
|
|
35
|
+
|
|
36
|
+
const registry: Registry = (getRequestEvent()!.locals[REGISTRY] ??= {});
|
|
37
|
+
const ssrRequestAssets: Function[] = (sharedConfig.context as any)?.assets;
|
|
38
|
+
const cssKeys: string[] = [];
|
|
39
|
+
|
|
40
|
+
for (const asset of assets) {
|
|
41
|
+
const entity = getEntity(registry, asset);
|
|
42
|
+
const isCSSLink = asset.tag === "link" && asset.attrs.rel === "stylesheet";
|
|
43
|
+
const isCSS = isCSSLink || asset.tag === "style";
|
|
44
|
+
if (isCSS) {
|
|
45
|
+
cssKeys.push(entity.key);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
entity.consumers++;
|
|
49
|
+
if (entity.consumers > 1) continue;
|
|
50
|
+
|
|
51
|
+
// Mounting logic
|
|
52
|
+
useAssets_(() => renderAsset(asset, nonce));
|
|
53
|
+
entity.ssrIdx = ssrRequestAssets.length - 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
onCleanup(() => {
|
|
57
|
+
for (const key of cssKeys) {
|
|
58
|
+
const entity = registry[key]!;
|
|
59
|
+
entity.consumers--;
|
|
60
|
+
if (entity.consumers != 0) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Ideally this logic should be implemented directly in dom-expressions
|
|
65
|
+
ssrRequestAssets.splice(entity.ssrIdx!, 1, NOOP);
|
|
66
|
+
|
|
67
|
+
delete registry[key];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @refresh skip
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
|
|
4
|
+
const assetMap = {
|
|
5
|
+
style: (props: { attrs: JSX.StyleHTMLAttributes<HTMLStyleElement>; children?: JSX.Element }) => (
|
|
6
|
+
<style {...props.attrs}>{props.children}</style>
|
|
7
|
+
),
|
|
8
|
+
link: (props: { attrs: JSX.LinkHTMLAttributes<HTMLLinkElement> }) => <link {...props.attrs} />,
|
|
9
|
+
script: (props: {
|
|
10
|
+
attrs: JSX.ScriptHTMLAttributes<HTMLScriptElement>;
|
|
11
|
+
key: string | undefined;
|
|
12
|
+
}) => {
|
|
13
|
+
return props.attrs.src ? (
|
|
14
|
+
<script {...props.attrs} id={props.key}>
|
|
15
|
+
{" "}
|
|
16
|
+
</script>
|
|
17
|
+
) : null;
|
|
18
|
+
},
|
|
19
|
+
noscript: (props: { attrs: JSX.HTMLAttributes<HTMLElement>; children: JSX.Element }) => (
|
|
20
|
+
<noscript {...props.attrs}>{props.children}</noscript>
|
|
21
|
+
)
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function renderAsset(asset: Asset, nonce?: string) {
|
|
25
|
+
let { tag, attrs: { key, ...attrs } = { key: undefined }, children } = asset as any;
|
|
26
|
+
return (assetMap as any)[tag]({ attrs: { ...attrs, nonce }, key, children });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Asset =
|
|
30
|
+
| {
|
|
31
|
+
tag: "style";
|
|
32
|
+
attrs: JSX.StyleHTMLAttributes<HTMLStyleElement> & { key?: string };
|
|
33
|
+
children?: JSX.Element;
|
|
34
|
+
}
|
|
35
|
+
| {
|
|
36
|
+
tag: "script";
|
|
37
|
+
attrs: JSX.ScriptHTMLAttributes<HTMLScriptElement> & { key?: string };
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
tag: "link";
|
|
41
|
+
attrs: JSX.LinkHTMLAttributes<HTMLLinkElement> & { key?: string };
|
|
42
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { resolve } from "pathe";
|
|
3
|
+
import type { DevEnvironment, EnvironmentModuleNode } from "vite";
|
|
4
|
+
|
|
5
|
+
async function getViteModuleNode(
|
|
6
|
+
vite: DevEnvironment,
|
|
7
|
+
file: string,
|
|
8
|
+
) {
|
|
9
|
+
let nodePath = file;
|
|
10
|
+
let node = vite.moduleGraph.getModuleById(file);
|
|
11
|
+
|
|
12
|
+
if (!node) {
|
|
13
|
+
const resolvedId = await vite.pluginContainer.resolveId(file, undefined);
|
|
14
|
+
if (!resolvedId) return;
|
|
15
|
+
|
|
16
|
+
nodePath = resolvedId.id;
|
|
17
|
+
node = vite.moduleGraph.getModuleById(file);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!node) {
|
|
21
|
+
nodePath = resolve(nodePath);
|
|
22
|
+
node = await vite.moduleGraph.getModuleByUrl(file);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!node) {
|
|
26
|
+
await vite.moduleGraph.ensureEntryFromUrl(nodePath, false);
|
|
27
|
+
node = vite.moduleGraph.getModuleById(nodePath);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return node;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function findModuleDependencies(
|
|
34
|
+
vite: DevEnvironment,
|
|
35
|
+
file: string,
|
|
36
|
+
deps: Set<EnvironmentModuleNode>,
|
|
37
|
+
crawledFiles = new Set<string>()
|
|
38
|
+
) {
|
|
39
|
+
crawledFiles.add(file);
|
|
40
|
+
const module = await getViteModuleNode(vite, file);
|
|
41
|
+
if (!module?.id || deps.has(module)) return;
|
|
42
|
+
|
|
43
|
+
deps.add(module);
|
|
44
|
+
|
|
45
|
+
if (module.url.endsWith(".css") || module.url.includes("node_modules")) return;
|
|
46
|
+
|
|
47
|
+
if (!module.transformResult) {
|
|
48
|
+
await vite.transformRequest(module.id).catch(() => {});
|
|
49
|
+
}
|
|
50
|
+
if (!module.transformResult?.deps) return;
|
|
51
|
+
|
|
52
|
+
// Relying on module.transformResult.deps instead of module.importedModules because:
|
|
53
|
+
// transformResult properly separates imports into deps and dynamicDeps, importedModules doesn't
|
|
54
|
+
// Style crawling has to skip dynamic imports as such modules load their styles themselves
|
|
55
|
+
for (const dep of module.transformResult.deps) {
|
|
56
|
+
if (crawledFiles.has(dep)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
await findModuleDependencies(vite, dep, deps, crawledFiles);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Vite doesn't expose these so we just copy the list for now
|
|
64
|
+
// https://github.com/vitejs/vite/blob/d6bde8b03d433778aaed62afc2be0630c8131908/packages/vite/src/node/constants.ts#L49C23-L50
|
|
65
|
+
const cssFileRegExp =
|
|
66
|
+
/\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)$/;
|
|
67
|
+
// https://github.com/vitejs/vite/blob/d6bde8b03d433778aaed62afc2be0630c8131908/packages/vite/src/node/plugins/css.ts#L160
|
|
68
|
+
const cssModulesRegExp = new RegExp(`\\.module${cssFileRegExp.source}`);
|
|
69
|
+
|
|
70
|
+
const isCssFile = (file: string) => cssFileRegExp.test(file);
|
|
71
|
+
export const isCssModulesFile = (file: string) => cssModulesRegExp.test(file);
|
|
72
|
+
|
|
73
|
+
export async function findStylesInModuleGraph(
|
|
74
|
+
vite: DevEnvironment,
|
|
75
|
+
id: string,
|
|
76
|
+
) {
|
|
77
|
+
const absolute = path.resolve(process.cwd(), id);
|
|
78
|
+
const dependencies = new Set<EnvironmentModuleNode>();
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
await findModuleDependencies(vite, absolute, dependencies);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.error(e);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const styles: Record<string, any> = {};
|
|
87
|
+
for (const dep of dependencies) {
|
|
88
|
+
if (dep.id && isCssFile(dep.url)) {
|
|
89
|
+
styles[dep.id] = dep.url;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return styles;
|
|
94
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as h3 from "h3";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
createFetchEvent,
|
|
6
|
+
getFetchEvent,
|
|
7
|
+
mergeResponseHeaders,
|
|
8
|
+
} from "./fetchEvent.ts";
|
|
9
|
+
|
|
10
|
+
vi.mock(import("h3"), async (mod) => {
|
|
11
|
+
return {
|
|
12
|
+
...(await mod()),
|
|
13
|
+
getRequestIP: vi.fn(),
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const mockedH3 = vi.mocked(h3);
|
|
18
|
+
|
|
19
|
+
const createMockH3Event = (): h3.H3Event => {
|
|
20
|
+
const event = new h3.H3Event(new Request("http://localhost/test"));
|
|
21
|
+
|
|
22
|
+
event.res.status = 200;
|
|
23
|
+
event.res.statusText = "OK";
|
|
24
|
+
|
|
25
|
+
return event;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
describe("fetchEvent", () => {
|
|
29
|
+
let mockH3Event: h3.H3Event;
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
mockH3Event = createMockH3Event();
|
|
33
|
+
vi.clearAllMocks();
|
|
34
|
+
|
|
35
|
+
mockedH3.getRequestIP.mockReturnValue("127.0.0.1");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("createFetchEvent", () => {
|
|
39
|
+
it("should create a FetchEvent from H3Event", () => {
|
|
40
|
+
const fetchEvent = createFetchEvent(mockH3Event);
|
|
41
|
+
|
|
42
|
+
expect(fetchEvent).toEqual({
|
|
43
|
+
request: mockH3Event.req,
|
|
44
|
+
response: expect.any(Object),
|
|
45
|
+
clientAddress: "127.0.0.1",
|
|
46
|
+
locals: {},
|
|
47
|
+
nativeEvent: mockH3Event,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should create response stub with correct properties", () => {
|
|
52
|
+
const fetchEvent = createFetchEvent(mockH3Event);
|
|
53
|
+
|
|
54
|
+
expect(fetchEvent.response).toHaveProperty("status");
|
|
55
|
+
expect(fetchEvent.response).toHaveProperty("statusText");
|
|
56
|
+
expect(fetchEvent.response).toHaveProperty("headers");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("getFetchEvent", () => {
|
|
61
|
+
it("should create and cache FetchEvent on first call", () => {
|
|
62
|
+
const fetchEvent = getFetchEvent(mockH3Event);
|
|
63
|
+
|
|
64
|
+
expect(mockH3Event.context.solidFetchEvent).toBe(fetchEvent);
|
|
65
|
+
expect(fetchEvent.nativeEvent).toBe(mockH3Event);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should return cached FetchEvent on subsequent calls", () => {
|
|
69
|
+
const firstCall = getFetchEvent(mockH3Event);
|
|
70
|
+
const secondCall = getFetchEvent(mockH3Event);
|
|
71
|
+
|
|
72
|
+
expect(firstCall).toBe(secondCall);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("mergeResponseHeaders", () => {
|
|
77
|
+
it("should merge headers from Headers object to H3Event", () => {
|
|
78
|
+
const headers = new Headers({
|
|
79
|
+
"content-type": "application/json",
|
|
80
|
+
"x-custom": "value",
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
mergeResponseHeaders(mockH3Event, headers);
|
|
84
|
+
|
|
85
|
+
expect(headers.get("content-type")).toBe("application/json");
|
|
86
|
+
expect(headers.get("x-custom")).toBe("value");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type EventHandler, getRequestIP, type H3Event, type Middleware } from "h3";
|
|
2
|
+
import { provideRequestEvent } from "solid-js/web/storage";
|
|
3
|
+
import type { FetchEvent } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
const FETCH_EVENT_CONTEXT = "solidFetchEvent";
|
|
6
|
+
|
|
7
|
+
export function createFetchEvent(event: H3Event): FetchEvent {
|
|
8
|
+
return {
|
|
9
|
+
request: event.req,
|
|
10
|
+
response: event.res,
|
|
11
|
+
clientAddress: getRequestIP(event),
|
|
12
|
+
locals: {},
|
|
13
|
+
nativeEvent: event,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getFetchEvent(h3Event: H3Event): FetchEvent {
|
|
18
|
+
if (!h3Event.context[FETCH_EVENT_CONTEXT]) {
|
|
19
|
+
const fetchEvent = createFetchEvent(h3Event);
|
|
20
|
+
h3Event.context[FETCH_EVENT_CONTEXT] = fetchEvent;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return h3Event.context[FETCH_EVENT_CONTEXT] as any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function mergeResponseHeaders(h3Event: H3Event, headers: Headers) {
|
|
27
|
+
for (const [key, value] of headers.entries()) {
|
|
28
|
+
h3Event.res.headers.append(key, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const decorateHandler = <T extends EventHandler>(fn: T) =>
|
|
33
|
+
(event => provideRequestEvent(getFetchEvent(event), () => fn(event))) as T;
|
|
34
|
+
|
|
35
|
+
export const decorateMiddleware = <T extends Middleware>(fn: T) =>
|
|
36
|
+
((event, next) => provideRequestEvent(getFetchEvent(event), () => fn(event, next))) as T;
|