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

Commit 88801c8

Browse files
committed
Add preBuild option
1 parent 5384c4d commit 88801c8

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

commands/dev.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ if (import.meta.main) {
126126
});
127127
}
128128
});
129-
} else {
130-
emitters.forEach((e) => {
131-
e.emit(kind, { specifier });
132-
});
133129
}
134130
});
135131

@@ -142,6 +138,9 @@ if (import.meta.main) {
142138

143139
let ac: AbortController | null = null;
144140
const bs = async () => {
141+
if (Deno.env.get("PREVENT_SERVER_RESTART") === "true") {
142+
return;
143+
}
145144
if (ac) {
146145
ac.abort();
147146
log.info(`Restart server...`);
@@ -188,7 +187,7 @@ if (import.meta.main) {
188187
await bs();
189188
}
190189

191-
async function bootstrap(signal: AbortSignal, entry: string | undefined, forcePort?: number): Promise<void> {
190+
async function bootstrap(signal: AbortSignal, entry: string | undefined, fixedPort?: number): Promise<void> {
192191
// clean globally cached objects
193192
Reflect.deleteProperty(globalThis, "__ALEPH_SERVER");
194193
Reflect.deleteProperty(globalThis, "__ALEPH_INDEX_HTML");
@@ -211,14 +210,28 @@ async function bootstrap(signal: AbortSignal, entry: string | undefined, forcePo
211210
serve();
212211
}
213212

214-
const {
215-
port: userPort,
216-
hostname,
217-
certFile,
218-
keyFile,
219-
handler,
220-
} = Reflect.get(globalThis, "__ALEPH_SERVER") || {};
221-
const port = forcePort || userPort || 8080;
213+
const { devServer, build }: AlephConfig = Reflect.get(globalThis, "__ALEPH_CONFIG") || {};
214+
const { port: userPort, hostname, certFile, keyFile, handler } = Reflect.get(globalThis, "__ALEPH_SERVER") || {};
215+
const port = fixedPort || userPort || 8080;
216+
217+
if (typeof build?.preBuild === "function") {
218+
log.info("Pre-build...");
219+
await build.preBuild();
220+
}
221+
if (typeof devServer?.watchFS === "function") {
222+
const { watchFS } = devServer;
223+
const e = createEmitter();
224+
signal.addEventListener("abort", () => {
225+
removeEmitter(e);
226+
});
227+
e.on("*", (kind, { specifier }) => {
228+
if (kind.startsWith("modify:")) {
229+
watchFS("modify", specifier);
230+
} else if (kind === "create" || kind === "remove") {
231+
watchFS(kind, specifier);
232+
}
233+
});
234+
}
222235

223236
try {
224237
await httpServe({

server/build.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function build(serverEntry?: string) {
4141
const jsxCofig = await loadJSXConfig(importMap);
4242
const moduleLoaders = await initModuleLoaders(importMap);
4343
const config: AlephConfig | undefined = Reflect.get(globalThis, "__ALEPH_CONFIG");
44+
const preBuild = config?.build?.preBuild;
4445
const platform = config?.build?.platform ?? "deno";
4546
const target = config?.build?.target ?? "es2020";
4647
const outputDir = join(workingDir, config?.build?.outputDir ?? "dist");
@@ -59,6 +60,11 @@ export async function build(serverEntry?: string) {
5960
await Deno.mkdir(outputDir, { recursive: true });
6061
}
6162

63+
if (typeof preBuild === "function") {
64+
log.info("Pre-build...");
65+
await preBuild();
66+
}
67+
6268
// find route files by the `routes` config
6369
let routeFiles: [filename: string, exportNames: string[]][] = [];
6470
if (config?.routes) {

server/mod.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ export type ServerOptions = Omit<ServeInit, "onError"> & {
2020
certFile?: string;
2121
keyFile?: string;
2222
logLevel?: LevelName;
23-
hmrWebSocketUrl?: string;
2423
middlewares?: Middleware[];
2524
fetch?: FetchHandler;
2625
ssr?: SSR;
2726
onError?: ErrorCallback;
2827
} & AlephConfig;
2928

3029
export const serve = (options: ServerOptions = {}) => {
31-
const { routes, build, middlewares, fetch, ssr, logLevel, onError } = options;
30+
const { routes, build, devServer, middlewares, fetch, ssr, logLevel, onError } = options;
3231
const isDev = Deno.env.get("ALEPH_ENV") === "development";
3332
const importMapPromise = loadImportMap();
3433
const jsxConfigPromise = importMapPromise.then(loadJSXConfig);
@@ -353,7 +352,7 @@ export const serve = (options: ServerOptions = {}) => {
353352
isDev,
354353
importMap: await importMapPromise,
355354
ssr: typeof ssr === "function" ? {} : ssr,
356-
hmrWebSocketUrl: options.hmrWebSocketUrl,
355+
hmrWebSocketUrl: options.devServer?.hmrWebSocketUrl,
357356
});
358357
} catch (err) {
359358
if (err instanceof Deno.errors.NotFound) {
@@ -387,31 +386,13 @@ export const serve = (options: ServerOptions = {}) => {
387386
});
388387
};
389388

390-
// inject navigator browser polyfill to fix some ssr errors
391-
Object.assign(globalThis.navigator, {
392-
connection: {
393-
downlink: 10,
394-
effectiveType: "4g",
395-
onchange: null,
396-
rtt: 50,
397-
saveData: false,
398-
},
399-
cookieEnabled: false,
400-
language: "en",
401-
languages: ["en"],
402-
onLine: true,
403-
vendor: "Deno Land Inc.",
404-
});
405-
// deno-lint-ignore no-explicit-any
406-
(globalThis.navigator as any).userAgent ??= `Deno/${Deno.version?.deno || "deploy"}`;
407-
408389
// set log level if specified
409390
if (logLevel) {
410391
log.setLevel(logLevel);
411392
}
412393

413394
// inject global objects
414-
Reflect.set(globalThis, "__ALEPH_CONFIG", { build, routes });
395+
Reflect.set(globalThis, "__ALEPH_CONFIG", { build, routes, devServer });
415396
Reflect.set(globalThis, "clientDependencyGraph", new DependencyGraph());
416397

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

server/types.ts

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

33
export type AlephConfig = {
4-
/** The build options for `build` command. */
5-
build?: BuildOptions;
64
/** The config for file-system based routing. */
75
routes?: RoutesConfig | string;
6+
/** The build options for `build` command. */
7+
build?: BuildOptions;
8+
/** The config for dev server. */
9+
devServer?: {
10+
watchFS?: (kind: "create" | "remove" | "modify", specifier: string) => void;
11+
/** The url for HMR web socket. This is useful for dev server proxy env. */
12+
hmrWebSocketUrl?: string;
13+
};
814
};
915

1016
/** The build platform. */
@@ -13,7 +19,7 @@ export type BuildPlatform = "deno" | "cloudflare" | "vercel";
1319
/** The build options for `build` command. */
1420
export type BuildOptions = {
1521
/** The Pre-build task. */
16-
preBuild?: () => Promise<Record<string, unknown>>;
22+
preBuild?: () => Promise<void> | void;
1723
/** The supported platform. default is "deno" */
1824
platform?: BuildPlatform;
1925
/** The directory for build output files. default is "dist" */

0 commit comments

Comments
 (0)