/
build.ts
450 lines (421 loc) · 15.7 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import { basename, dirname, extname, join } from "https://deno.land/std@0.136.0/path/mod.ts";
import { ensureDir } from "https://deno.land/std@0.136.0/fs/ensure_dir.ts";
import { build as esbuild, type Loader, stop } from "https://deno.land/x/esbuild@v0.14.38/mod.js";
import { parseExportNames } from "https://deno.land/x/aleph_compiler@0.1.0/mod.ts";
import cache from "../lib/cache.ts";
import { existsDir, existsFile } from "../lib/fs.ts";
import { parseHtmlLinks } from "./html.ts";
import log from "../lib/log.ts";
import util from "../lib/util.ts";
import type { DependencyGraph } from "./graph.ts";
import {
builtinModuleExts,
getAlephPkgUri,
initModuleLoaders,
loadImportMap,
loadJSXConfig,
restoreUrl,
toLocalPath,
} from "./helpers.ts";
import { initRoutes } from "./routing.ts";
import type { AlephConfig, BuildPlatform, FetchHandler } from "./types.ts";
const supportedPlatforms: Record<BuildPlatform, string> = {
"deno": "Deno",
"cloudflare": "Cloudflare",
"vercel": "Vercel",
};
export async function build(serverEntry?: string) {
const workingDir = Deno.cwd();
const alephPkgUri = getAlephPkgUri();
const importMap = await loadImportMap();
const jsxCofig = await loadJSXConfig(importMap);
const moduleLoaders = await initModuleLoaders(importMap);
const config: AlephConfig | undefined = Reflect.get(globalThis, "__ALEPH_CONFIG");
const platform = config?.build?.platform ?? "deno";
const target = config?.build?.target ?? "es2020";
const outputDir = join(workingDir, config?.build?.outputDir ?? "dist");
if (platform === "cloudflare" || platform === "vercel") {
log.fatal(`Deploy to ${supportedPlatforms[platform]} is not supported yet`);
}
// clean previous build
if (await existsDir(outputDir)) {
for await (const entry of Deno.readDir(outputDir)) {
await Deno.remove(join(outputDir, entry.name), { recursive: entry.isDirectory });
}
} else {
await Deno.mkdir(outputDir, { recursive: true });
}
let routeFiles: [filename: string, exportNames: string[]][] = [];
if (config?.routes) {
const { routes } = await initRoutes(config?.routes);
routeFiles = await Promise.all(routes.map(async ([_, { filename }]) => {
const code = await Deno.readTextFile(filename);
const exportNames = await parseExportNames(filename, code);
return [filename, exportNames];
}));
}
const modulesProxyPort = Deno.env.get("ALEPH_MODULES_PROXY_PORT");
const serverEntryCode = [
`import { DependencyGraph } from "${alephPkgUri}/server/graph.ts";`,
`import graph from "./server_dependency_graph.js";`,
`globalThis.serverDependencyGraph = new DependencyGraph(graph.modules);`,
routeFiles.length > 0 && `import { revive } from "${alephPkgUri}/server/routing.ts";`,
moduleLoaders.length > 0 &&
`import { globToRegExp } from "https://deno.land/std@0.136.0/path/mod.ts";const moduleLoaders = []; globalThis["__ALEPH_MODULE_LOADERS"] = moduleLoaders;`,
moduleLoaders.length > 0 &&
moduleLoaders.map((loader) => {
const meta = Reflect.get(loader, "meta");
return `
{
const reg = globToRegExp(${JSON.stringify(meta.glob)});
moduleLoaders.push({
meta: ${JSON.stringify(meta)},
test: (pathname) => reg.test(pathname),
load: (pathname, env) => ({ code: '' }),
})
}
`;
}).join("\n"),
...routeFiles.map(([filename, exportNames], idx) => {
const hasDefaultExport = exportNames.includes("default");
const hasDataExport = exportNames.includes("data");
if (!hasDefaultExport && !hasDataExport) {
return [];
}
const url = `http://localhost:${modulesProxyPort}${filename.slice(1)}`;
return [
`import { ${
[
hasDefaultExport && `default as $${idx}`,
hasDataExport && `data as $$${idx}`,
].filter(Boolean).join(", ")
} } from ${JSON.stringify(url)};`,
`revive(${JSON.stringify(filename)}, { ${
[
hasDefaultExport && `default: $${idx}`,
hasDataExport && `data: $$${idx}`,
].filter(Boolean).join(", ")
} });`,
];
}),
serverEntry && `import "http://localhost:${modulesProxyPort}/${basename(serverEntry)}";`,
!serverEntry && `import { serve } from "${alephPkgUri}/server/mode.ts";`,
!serverEntry && `serve();`,
].flat().filter(Boolean).join("\n");
// since esbuild doesn't support jsx automic transform, we need to manually import jsx runtime
let jsxShimFile: string | null = null;
if (serverEntry && util.endsWithAny(serverEntry, ".jsx", ".tsx") && jsxCofig.jsxImportSource) {
jsxShimFile = join(outputDir, "jsx-shim.js");
await Deno.writeTextFile(
jsxShimFile,
(jsxCofig.jsxRuntime === "preact"
? [
`import { h, Fragment } from ${JSON.stringify(jsxCofig.jsxImportSource)};`,
`export { h, Fragment }`,
]
: [
`import React from ${JSON.stringify(jsxCofig.jsxImportSource)};`,
`export { React }`,
]).join("\n"),
);
}
const shouldBundle = (importUrl: string) => {
return (
// to bundle `server/transformer.ts`, need to bundle `server/mod.ts` first
importUrl === alephPkgUri + "/server/mod.ts" ||
// bundle `server/transformer.ts` with `server/server_dist.ts` content
importUrl === alephPkgUri + "/server/transformer.ts" ||
// since deno deploy doesn't support importMap, we need to bundle the framework and resolve the 'react' import
importUrl.startsWith(alephPkgUri + "/framework/react/") ||
// bundle app modules
importUrl.startsWith(`http://localhost:${modulesProxyPort}/`)
);
};
const isEsmPkg = (url: string) => {
return url.startsWith("https://esm.sh/") && !url.endsWith(".js") && !url.endsWith(".css");
};
// build the server entry
await esbuild({
stdin: {
contents: serverEntryCode,
sourcefile: "server.tsx",
},
outfile: join(outputDir, "server.js"),
platform: "browser",
format: "esm",
target: ["esnext"],
bundle: true,
minify: !Deno.env.get("ALEPH_BUILD_DEBUG"),
treeShaking: true,
sourcemap: true,
jsxFactory: jsxCofig.jsxRuntime === "preact" ? "h" : "React.createElement",
jsxFragment: jsxCofig.jsxRuntime === "preact" ? "Fragment" : "React.Fragment",
inject: [jsxShimFile as string].filter(Boolean),
plugins: [{
name: "aleph-esbuild-plugin",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
let importUrl = args.path;
if (importUrl in importMap.imports) {
// since deno deploy doesn't support importMap yet, we need to resolve the 'react' import
importUrl = importMap.imports[importUrl];
}
const isRemote = util.isLikelyHttpURL(importUrl);
const [path] = util.splitBy(isRemote ? importUrl : util.trimPrefix(importUrl, "file://"), "#");
if (args.kind === "dynamic-import") {
return { path, external: true };
}
if (args.namespace === "http") {
const { href } = new URL(path, args.importer);
if (!shouldBundle(href)) {
return { path: href, external: true };
}
return { path: href, namespace: "http" };
}
if (isRemote && shouldBundle(path)) {
return { path, namespace: "http" };
}
if (isRemote || path === "./server_dependency_graph.js") {
return { path, external: true };
}
if (path.startsWith("./") || path.startsWith("../")) {
return {
path: join(dirname(args.importer), path),
};
}
return { path };
});
build.onLoad({ filter: /.*/, namespace: "http" }, async (args) => {
const url = new URL(args.path);
if (url.href === `${alephPkgUri}/server/transformer.ts`) {
// bundle `server/transformer.ts` with `server/server_dist.ts` content
url.pathname = util.trimSuffix(url.pathname, "transformer.ts") + "serve_dist.ts";
}
const res = await cache(url.href);
const contents = await res.text();
let ext = extname(url.pathname).slice(1);
if (ext === "mjs") {
ext = "js";
} else if (ext === "mts") {
ext = "ts";
} else if (!builtinModuleExts.includes(ext)) {
const ctype = res.headers.get("Content-Type");
if (ctype?.startsWith("application/javascript")) {
ext = "js";
} else if (ctype?.startsWith("application/typescript")) {
ext = "ts";
} else if (ctype?.startsWith("text/jsx")) {
ext = "jsx";
} else if (ctype?.startsWith("text/tsx")) {
ext = "tsx";
}
}
return {
contents,
loader: ext as unknown as Loader,
};
});
},
}],
});
const serverDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "serverDependencyGraph");
const clientDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "clientDependencyGraph");
// create server_dependency_graph.js
if (serverDependencyGraph) {
// deno-lint-ignore no-unused-vars
const modules = serverDependencyGraph.modules.map(({ sourceCode, ...ret }) => ret);
await Deno.writeTextFile(
join(outputDir, "server_dependency_graph.js"),
"export default " + JSON.stringify({ modules }),
);
}
// look up client modules
let tasks = routeFiles.map(([filename]) => filename);
if (await existsFile(join(workingDir, "index.html"))) {
const html = await Deno.readFile(join(workingDir, "index.html"));
const links = await parseHtmlLinks(html);
for (const src of links) {
const url = new URL(src, "http://localhost/");
const ext = extname(url.pathname).slice(1);
if (ext === "css" || builtinModuleExts.includes(ext)) {
const specifier = util.isLikelyHttpURL(src) ? src : "." + util.cleanPath(src);
tasks.push(specifier);
}
}
}
tasks.push(`${alephPkgUri}/framework/core/nomodule.ts`);
const entryModules = new Set(tasks);
const allModules = new Set<string>();
// transform client modules
const serverHandler: FetchHandler | undefined = Reflect.get(globalThis, "__ALEPH_SERVER")?.handler;
if (serverHandler) {
while (tasks.length > 0) {
const deps = new Set<string>();
await Promise.all(tasks.map(async (specifier) => {
const url = new URL(util.isLikelyHttpURL(specifier) ? toLocalPath(specifier) : specifier, "http://localhost");
const isCSS = url.pathname.endsWith(".css");
const req = new Request(url.toString());
let savePath = join(outputDir, url.pathname);
if (isEsmPkg(specifier)) {
savePath += ".js";
} else if (isCSS && url.searchParams.has("module")) {
savePath += ".js";
}
await ensureDir(dirname(savePath));
const [res, file] = await Promise.all([
serverHandler(req),
Deno.open(savePath, { write: true, create: true }),
]);
await res.body?.pipeTo(file.writable);
if (!isCSS) {
clientDependencyGraph?.get(specifier)?.deps?.forEach(({ specifier, dynamic }) => {
if (dynamic) {
entryModules.add(specifier);
}
if (specifier.endsWith(".css")) {
deps.add(specifier + "?module");
} else {
deps.add(specifier);
}
});
} else if (url.searchParams.has("module")) {
deps.add(`${alephPkgUri}/framework/core/style.ts`);
}
allModules.add(specifier);
}));
tasks = Array.from(deps).filter((specifier) => !allModules.has(specifier));
}
}
// count client module refs
const refs = new Map<string, Set<string>>();
for (const name of entryModules) {
clientDependencyGraph?.walk(name, ({ specifier }, importer) => {
if (importer) {
let set = refs.get(specifier);
if (!set) {
set = new Set<string>();
refs.set(specifier, set);
}
set.add(importer.specifier);
}
});
}
// hygiene 1
/*
B(1) <-
A <- <- <- D(1+) :: A <- D(1)
C(1) <-
*/
refs.forEach((counter, specifier) => {
if (counter.size > 1) {
const a = Array.from(counter).filter((specifier) => {
const set = refs.get(specifier);
if (set?.size === 1) {
const name = set.values().next().value;
if (name && counter.has(name)) {
return false;
}
}
return true;
});
refs.set(specifier, new Set(a));
}
});
// hygiene 2 (twice)
/*
B(1) <-
A <- C(1) <- E(1+) :: A <- E(1)
D(1) <-
*/
for (let i = 0; i < 2; i++) {
refs.forEach((counter, specifier) => {
if (counter.size > 0) {
const a = Array.from(counter);
if (a.every((specifier) => refs.get(specifier)?.size === 1)) {
const set = new Set(a.map((specifier) => refs.get(specifier)?.values().next().value));
if (set.size === 1) {
refs.set(specifier, set);
}
}
}
});
}
// find client modules
const clientModules = new Set<string>(entryModules);
refs.forEach((counter, specifier) => {
if (counter.size > 1) {
clientModules.add(specifier);
}
// console.log(`[${specifier}] \n - ${Array.from(counter).join("\n - ")}`);
});
// bundle client modules
const bundling = new Set<string>();
clientModules.forEach((specifier) => {
if (
clientDependencyGraph?.get(specifier)?.deps?.some(({ specifier }) => !clientModules.has(specifier)) &&
!util.splitBy(specifier, "?")[0].endsWith(".css")
) {
bundling.add(specifier);
}
});
await Promise.all(
Array.from(bundling).map(async (entryPoint) => {
const url = new URL(util.isLikelyHttpURL(entryPoint) ? toLocalPath(entryPoint) : entryPoint, "http://localhost");
let jsFile = join(outputDir, url.pathname);
if (isEsmPkg(entryPoint)) {
jsFile += ".js";
}
await esbuild({
entryPoints: [jsFile],
outfile: jsFile,
allowOverwrite: true,
platform: "browser",
format: "esm",
target: [target],
bundle: true,
minify: !Deno.env.get("ALEPH_BUILD_DEBUG"),
treeShaking: true,
sourcemap: false,
loader: {
".vue": "js",
},
plugins: [{
name: "aleph-esbuild-plugin",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
let argsPath = args.path;
if (argsPath.startsWith("./") || argsPath.startsWith("../")) {
argsPath = join(args.resolveDir, argsPath);
}
const [fp, q] = util.splitBy(argsPath, "?");
const path = util.trimPrefix(fp, outputDir);
let specifier = "." + path;
if (args.path.startsWith("/-/")) {
specifier = restoreUrl(path);
}
if (clientModules.has(specifier) && specifier !== entryPoint) {
return {
path: [path, q].filter(Boolean).join("?"),
external: true,
};
}
let jsFile = join(outputDir, path);
if (isEsmPkg(specifier)) {
jsFile += ".js";
} else if (specifier.endsWith(".css") && new URLSearchParams(q).has("module")) {
jsFile += ".js";
}
return { path: jsFile };
});
},
}],
});
}),
);
// clean up then exit
if (jsxShimFile) {
await Deno.remove(jsxShimFile);
}
stop();
return { clientModules, routeFiles };
}