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

Commit d2c73fd

Browse files
committed
Fix build command
1 parent 9f6c37d commit d2c73fd

File tree

5 files changed

+45
-31
lines changed

5 files changed

+45
-31
lines changed

server/build.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { parseExportNames } from "../compiler/mod.ts";
55
import cache from "../lib/cache.ts";
66
import { existsDir, existsFile } from "../lib/fs.ts";
77
import { builtinModuleExts, toLocalPath } from "../lib/helpers.ts";
8-
import { parseHtmlLinks } from "../lib/html.ts";
8+
import { parseHtmlLinks } from "./html.ts";
99
import log from "../lib/log.ts";
1010
import util from "../lib/util.ts";
1111
import { getAlephPkgUri, loadImportMap, loadJSXConfig } from "../server/config.ts";
@@ -55,7 +55,7 @@ export async function build(serverEntry?: string) {
5555
`import { DependencyGraph } from "${alephPkgUri}/server/graph.ts";`,
5656
`import graph from "./server_dependency_graph.js";`,
5757
`globalThis.serverDependencyGraph = new DependencyGraph(graph.modules);`,
58-
routeFiles.length > 0 && `import { register } from "${alephPkgUri}/server/routing.ts";`,
58+
routeFiles.length > 0 && `import { revive } from "${alephPkgUri}/server/routing.ts";`,
5959
...routeFiles.map(([filename, exportNames], idx) => {
6060
const hasDefaultExport = exportNames.includes("default");
6161
const hasDataExport = exportNames.includes("data");
@@ -64,11 +64,18 @@ export async function build(serverEntry?: string) {
6464
}
6565
const url = `http://localhost:${port}${filename.slice(1)}`;
6666
return [
67-
hasDefaultExport && `import _${idx} from ${JSON.stringify(url)};`,
68-
!hasDefaultExport && `const _${idx} = undefined;`,
69-
hasDataExport && `import { data as $${idx} } from ${JSON.stringify(url)};`,
70-
!hasDataExport && `const $${idx} = undefined;`,
71-
`register(${JSON.stringify(filename)}, { default: _${idx}, data: $${idx} });`,
67+
`import { ${
68+
[
69+
hasDefaultExport && `default as $${idx}`,
70+
hasDataExport && `data as $$${idx}`,
71+
].filter(Boolean).join(", ")
72+
} } from ${JSON.stringify(url)};`,
73+
`revive(${JSON.stringify(filename)}, { ${
74+
[
75+
hasDefaultExport && `default: $${idx}`,
76+
hasDataExport && `data: $$${idx}`,
77+
].filter(Boolean).join(", ")
78+
} });`,
7279
];
7380
}).flat().filter(Boolean),
7481
serverEntry && `import "http://localhost:${port}/${basename(serverEntry)}";`,
@@ -189,9 +196,11 @@ export async function build(serverEntry?: string) {
189196
// create server_dependency_graph.js
190197
const serverDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "serverDependencyGraph");
191198
if (serverDependencyGraph) {
199+
// deno-lint-ignore no-unused-vars
200+
const modules = serverDependencyGraph.modules.map(({ sourceCode, ...ret }) => ret);
192201
await Deno.writeTextFile(
193202
join(outputDir, "server_dependency_graph.js"),
194-
"export default " + JSON.stringify({ modules: serverDependencyGraph.modules }),
203+
"export default " + JSON.stringify({ modules }),
195204
);
196205
}
197206

server/graph.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type Module = {
22
readonly specifier: string;
3-
readonly sourceCode: string;
43
readonly version: number;
4+
readonly sourceCode?: string;
55
readonly deps?: ReadonlyArray<DependencyDescriptor>;
66
readonly inlineCSS?: string;
77
readonly atomicCSS?: boolean;
@@ -108,7 +108,7 @@ export class DependencyGraph {
108108
#walk(specifier: string, callback: (mod: Module) => void, __tracing = new Set<string>()) {
109109
if (this.#modules.has(specifier)) {
110110
const mod = this.#modules.get(specifier)!;
111-
callback({ ...mod, deps: mod.deps?.map((dep) => ({ ...dep })) });
111+
callback(mod);
112112
__tracing.add(specifier);
113113
mod.deps?.forEach((dep) => {
114114
if (!__tracing.has(dep.specifier)) {

server/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export const serve = (options: ServerOptions = {}) => {
294294
indexHtml,
295295
routes,
296296
customHTMLRewriter,
297+
isDev,
297298
ssr,
298299
});
299300
};

server/renderer.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import util from "../lib/util.ts";
44
import type { RouteModule, Routes } from "../lib/route.ts";
55
import { matchRoutes } from "../lib/route.ts";
66
import { getUnoGenerator } from "./config.ts";
7-
import type { DependencyGraph } from "./graph.ts";
7+
import type { DependencyGraph, Module } from "./graph.ts";
88
import type { Comment, Element } from "./html.ts";
99
import { HTMLRewriter } from "./html.ts";
1010
import { importRouteModule } from "./routing.ts";
@@ -36,6 +36,7 @@ export type RenderOptions = {
3636
indexHtml: Uint8Array;
3737
routes: Routes;
3838
customHTMLRewriter: Map<string, HTMLRewriterHandlers>;
39+
isDev: boolean;
3940
ssr?: SSR;
4041
};
4142

@@ -51,7 +52,7 @@ const bootstrapScript = `data:text/javascript;charset=utf-8;base64,${btoa("/* hy
5152

5253
export default {
5354
async fetch(req: Request, ctx: Record<string, unknown>, options: RenderOptions): Promise<Response> {
54-
const { indexHtml, routes, customHTMLRewriter, ssr } = options;
55+
const { indexHtml, routes, customHTMLRewriter, isDev, ssr } = options;
5556
const headers = new Headers(ctx.headers as Headers);
5657
let ssrRes: SSRResult | null = null;
5758
if (ssr) {
@@ -72,27 +73,27 @@ export default {
7273
const body = await render(ssrContext);
7374
const serverDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "serverDependencyGraph");
7475
if (serverDependencyGraph) {
75-
const atomicCSSSource: string[] = [];
76+
const atomicCSSSource: Promise<string>[] = [];
77+
const lookupModuleStyle = (mod: Module) => {
78+
const { specifier, sourceCode, atomicCSS, inlineCSS } = mod;
79+
if (atomicCSS) {
80+
atomicCSSSource.push(
81+
sourceCode ? Promise.resolve(sourceCode) : Deno.readTextFile(specifier).then((text) => {
82+
Object.assign(mod, { sourceCode: text });
83+
return text;
84+
}),
85+
);
86+
}
87+
if (inlineCSS) {
88+
headCollection.push(`<style data-module-id="${specifier}">${inlineCSS}</style>`);
89+
}
90+
};
7691
for (const { filename } of routeModules) {
77-
serverDependencyGraph.walk(filename, (mod) => {
78-
if (mod.atomicCSS) {
79-
atomicCSSSource.push(mod.sourceCode);
80-
}
81-
if (mod.inlineCSS) {
82-
headCollection.push(`<style data-module-id="${mod.specifier}">${mod.inlineCSS}</style>`);
83-
}
84-
});
92+
serverDependencyGraph.walk(filename, lookupModuleStyle);
8593
}
8694
for (const serverEntry of builtinModuleExts.map((ext) => `./server.${ext}`)) {
8795
if (serverDependencyGraph.get(serverEntry)) {
88-
serverDependencyGraph.walk(serverEntry, (mod) => {
89-
if (mod.atomicCSS) {
90-
atomicCSSSource.push(mod.sourceCode);
91-
}
92-
if (mod.inlineCSS) {
93-
headCollection.push(`<style data-module-id="${mod.specifier}">${mod.inlineCSS}</style>`);
94-
}
95-
});
96+
serverDependencyGraph.walk(serverEntry, lookupModuleStyle);
9697
break;
9798
}
9899
}
@@ -101,7 +102,10 @@ export default {
101102
const unoGenerator = getUnoGenerator();
102103
if (unoGenerator) {
103104
const start = performance.now();
104-
const { css } = await unoGenerator.generate(atomicCSSSource.join("\n"));
105+
const input = (await Promise.all(atomicCSSSource)).join("\n");
106+
const { css } = await unoGenerator.generate(input, {
107+
minify: !isDev,
108+
});
105109
if (css) {
106110
headCollection.push(
107111
`<style data-unocss="${unoGenerator.version}" data-build-time="${

server/routing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const revivedModules: Map<string, Record<string, unknown>> = new Map();
1212

1313
/** revive a route module. */
1414
export function revive(filename: string, module: Record<string, unknown>) {
15-
if (!Deno.env.get("ALEPH_CLI")) {
15+
if (Deno.env.get("ALEPH_ENV") === "production") {
1616
revivedModules.set(filename, module);
1717
}
1818
}

0 commit comments

Comments
 (0)