Skip to content

Commit bd4f2bc

Browse files
committed
feat: implement css aware lazy and migrate routes, pt2
1 parent 8ef0ec8 commit bd4f2bc

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

packages/start/src/config/lazy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as t from "@babel/types";
44
import { sep as osSep } from "path";
55
import { basename, relative, sep } from "path/posix";
66
import { PluginOption } from "vite";
7+
import { VITE_ENVIRONMENTS } from "./constants";
78

89
const idTransform = (id: string): PluginItem => {
910
return {
@@ -33,7 +34,7 @@ const importTransform = (): PluginItem => {
3334
path.insertAfter(
3435
t.importDeclaration(
3536
[t.importSpecifier(t.identifier("lazy"), t.identifier("lazy"))],
36-
t.stringLiteral("@solidjs/start")
37+
t.stringLiteral("@solidjs/start/server")
3738
)
3839
);
3940
}
@@ -50,6 +51,9 @@ const lazy = (): PluginOption => {
5051
return {
5152
name: "solid-lazy-css",
5253
enforce: "pre",
54+
applyToEnvironment(env) {
55+
return env.name === VITE_ENVIRONMENTS.server;
56+
},
5357
async transform(src, id) {
5458
if (!id.match(fileEndingRegex)) return;
5559

packages/start/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ export type {
1515
export { default as clientOnly } from "./shared/clientOnly.ts";
1616
export { GET } from "./shared/GET.ts";
1717
export { HttpStatusCode } from "./shared/HttpStatusCode.ts";
18-
export { default as lazy } from "./shared/lazy.ts";
1918
export { getServerFunctionMeta } from "./shared/serverFunction.ts";

packages/start/src/router.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Component } from "solid-js";
1+
import { lazy, type Component } from "solid-js";
22
import { getRequestEvent, isServer } from "solid-js/web";
33

44
import { pageRoutes as routeConfigs } from "./server/routes.ts";
55
import type { PageEvent } from "./server/types.ts";
6-
import lazy from "./shared/lazy.ts";
76

87
const components: Record<string, Component> = {};
98

packages/start/src/server/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { default as lazy } from "../shared/lazy.ts";
12
export { getServerFunctionMeta } from "../shared/serverFunction.ts";
23
export { StartServer } from "./StartServer.tsx";
34
export { createHandler } from "./handler.ts";

packages/start/src/shared/lazy.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component, createComponent, lazy as solidLazy } from "solid-js";
2-
import { getRequestEvent } from "solid-js/web";
1+
import { Component, lazy as solidLazy } from "solid-js";
2+
import { getRequestEvent, isServer } from "solid-js/web";
33
import { getManifest } from "solid-start:get-manifest";
4-
import { Asset } from "../server/types.ts";
5-
import { useAssets } from "./assets.ts";
4+
import { useAssets } from "../server/assets/index.ts";
5+
import { Asset, PageEvent } from "../server/types.ts";
66

77
const assetsById: Record<string, Asset[]> = {};
88

@@ -16,12 +16,12 @@ const getAssets = async (id: string) => {
1616
return assets;
1717
};
1818

19-
const collectAssets = function <T extends () => Promise<{ default: Component<any> }>>(fn: T): T {
20-
const wrapper: any = async () => {
19+
const withAssets = function <T extends () => Promise<{ default: Component<any> }>>(fn: T): T {
20+
const wrapper = async () => {
2121
const mod = await fn();
2222

2323
// This id$$ export is generated by the lazy vite plugin
24-
const id = (mod as any).id$$;
24+
const id: string = (mod as any).id$$;
2525
if (!id) return mod;
2626

2727
if (!mod.default) {
@@ -34,19 +34,18 @@ const collectAssets = function <T extends () => Promise<{ default: Component<any
3434

3535
return {
3636
default: (props: any) => {
37-
// TODO: How to get nonce on the client
38-
const { nonce } = (getRequestEvent() as any) ?? {};
37+
const { nonce } = getRequestEvent() as PageEvent;
3938
useAssets(assets, nonce);
4039

41-
return createComponent(mod.default, props);
40+
return mod.default(props);
4241
}
4342
};
4443
};
4544

46-
return wrapper;
45+
return wrapper as T;
4746
};
4847

49-
const lazy = <T extends Component<any>>(fn: () => Promise<{ default: T }>) =>
50-
solidLazy(collectAssets(fn));
48+
const lazy = !isServer ? solidLazy: <T extends Component<any>>(fn: () => Promise<{ default: T }>) =>
49+
solidLazy(withAssets(fn));
5150

5251
export default lazy;

0 commit comments

Comments
 (0)