Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit ee8dad4

Browse files
committed
[BREAKING] flat config field of server config
1 parent 817c5cb commit ee8dad4

File tree

6 files changed

+28
-34
lines changed

6 files changed

+28
-34
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { serve } from "aleph/server";
22

3-
serve({ port: 3000 });
3+
serve({
4+
port: 3000,
5+
});

examples/feature-apps/suspense-ssr/server.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { serve } from "aleph/server";
33
import { renderToReadableStream } from "react-dom/server";
44

55
serve({
6-
config: {
7-
routes: "./routes/**/*.tsx",
8-
},
6+
routes: "./routes/**/*.tsx",
97
ssr: {
108
suspense: true,
119
render: (ctx) => renderToReadableStream(<App ssrContext={ctx} />, ctx),

examples/feature-apps/unocss/server.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import { serve } from "aleph/server";
55
import { renderToString } from "react-dom/server";
66

77
serve({
8-
config: {
9-
routes: "./routes/**/*.tsx",
10-
unocss: {
11-
presets: [
12-
presetUno(),
13-
presetIcons({
14-
cdn: "https://esm.sh/",
15-
}),
16-
],
17-
},
8+
routes: "./routes/**/*.tsx",
9+
unocss: {
10+
presets: [
11+
presetUno(),
12+
presetIcons({
13+
cdn: "https://esm.sh/",
14+
}),
15+
],
1816
},
1917
ssr: (ctx) => renderToString(<App ssrContext={ctx} />),
2018
});

examples/react-app/server.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { serve } from "aleph/server";
33
import { renderToReadableStream } from "react-dom/server";
44

55
serve({
6-
config: {
7-
routes: "./routes/**/*.{tsx,ts}",
8-
unocss: {
9-
// to enable unocss, please add presets:
10-
// presets: [ unoPreset ],
11-
},
6+
routes: "./routes/**/*.{tsx,ts}",
7+
// to enable unocss, please add presets to unocss options
8+
// please check https://alephjs.org/docs/unocss
9+
unocss: {
10+
// presets: [ unoPreset ],
1211
},
1312
ssr: {
1413
// when set `suspense` to `true`, the router will loading data in suspense mode

server/mod.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,25 @@ export type ServerOptions = Omit<ServeInit, "onError"> & {
2121
keyFile?: string;
2222
logLevel?: LevelName;
2323
hmrWebSocketUrl?: string;
24-
config?: AlephConfig;
2524
middlewares?: Middleware[];
2625
fetch?: FetchHandler;
2726
ssr?: SSR;
2827
onError?: (
2928
error: unknown,
3029
cause: {
31-
by: "data-fetching" | "ssr" | "transplie" | "fs" | "middleware";
30+
by: "route-api" | "ssr" | "transplie" | "fs" | "middleware";
3231
url: string;
3332
},
3433
) => Response | void;
35-
};
34+
} & AlephConfig;
3635

3736
export const serve = (options: ServerOptions = {}) => {
38-
const { config, middlewares, fetch, ssr, logLevel, onError } = options;
37+
const { routes, build, unocss, middlewares, fetch, ssr, logLevel, onError } = options;
3938
const isDev = Deno.env.get("ALEPH_ENV") === "development";
4039
const importMapPromise = loadImportMap();
4140
const jsxConfigPromise = importMapPromise.then(loadJSXConfig);
4241
const moduleLoadersPromise = importMapPromise.then(initModuleLoaders);
43-
const routesPromise = config?.routes ? initRoutes(config.routes) : Promise.resolve({ routes: [] });
42+
const routesPromise = routes ? initRoutes(routes) : Promise.resolve({ routes: [] });
4443
const handler = async (req: Request, connInfo: ConnInfo): Promise<Response> => {
4544
const url = new URL(req.url);
4645
const { host, pathname, searchParams } = url;
@@ -67,7 +66,7 @@ export const serve = (options: ServerOptions = {}) => {
6766
return await clientModuleTransformer.fetch(req, {
6867
importMap,
6968
jsxConfig,
70-
buildTarget: config?.build?.target,
69+
buildTarget: build?.target,
7170
isDev,
7271
});
7372
} catch (err) {
@@ -96,7 +95,7 @@ export const serve = (options: ServerOptions = {}) => {
9695
loaded,
9796
importMap,
9897
jsxConfig,
99-
buildTarget: config?.build?.target,
98+
buildTarget: build?.target,
10099
isDev,
101100
});
102101
} catch (err) {
@@ -262,7 +261,7 @@ export const serve = (options: ServerOptions = {}) => {
262261
}
263262
}
264263

265-
// request data
264+
// request route api
266265
const routes: RouteRecord = Reflect.get(globalThis, "__ALEPH_ROUTES") || await routesPromise;
267266
if (routes.routes.length > 0) {
268267
for (const [pattern, { filename }] of routes.routes) {
@@ -309,7 +308,7 @@ export const serve = (options: ServerOptions = {}) => {
309308
return new Response("Method not allowed", { status: 405 });
310309
}
311310
} catch (err) {
312-
const res = onError?.(err, { by: "data-fetching", url: req.url });
311+
const res = onError?.(err, { by: "route-api", url: req.url });
313312
if (res instanceof Response) {
314313
return res;
315314
}
@@ -404,7 +403,7 @@ export const serve = (options: ServerOptions = {}) => {
404403
}
405404

406405
// inject global objects
407-
Reflect.set(globalThis, "__ALEPH_CONFIG", Object.assign({}, config));
406+
Reflect.set(globalThis, "__ALEPH_CONFIG", { build, routes, unocss });
408407
Reflect.set(globalThis, "clientDependencyGraph", new DependencyGraph());
409408

410409
const { hostname, port = 8080, certFile, keyFile, signal } = options;

server/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { UserConfig as UnoConfig } from "https://esm.sh/@unocss/[email protected]";
22

33
export type AlephConfig = {
4-
/** The basePath of the app. */
5-
basePath?: string;
6-
/** The build optioins for `build` command. */
4+
/** The build options for `build` command. */
75
build?: BuildOptions;
86
/** The config for file-system based routing. */
97
routes?: RoutesConfig | string;
@@ -14,7 +12,7 @@ export type AlephConfig = {
1412
/** The build platform. */
1513
export type BuildPlatform = "deno" | "cloudflare" | "vercel";
1614

17-
/** The build optioins for `build` command. */
15+
/** The build options for `build` command. */
1816
export type BuildOptions = {
1917
/** The supported platform. default is "deno" */
2018
platform?: BuildPlatform;

0 commit comments

Comments
 (0)