feat: prebundle code + restore from snapshot (#1604)

This commit is contained in:
Marvin Hagemeister
2023-08-16 16:47:51 +02:00
committed by GitHub
parent f67ec8520f
commit 477364c567
29 changed files with 500 additions and 28 deletions
+43
View File
@@ -0,0 +1,43 @@
import { ServerContext } from "../server/context.ts";
import { FreshOptions, Manifest } from "../server/mod.ts";
import { dirname, fromFileUrl, join, toFileUrl } from "../server/deps.ts";
import { fs } from "./deps.ts";
export async function build(
manifestPath: string,
opts: FreshOptions,
) {
const manifest = (await import(toFileUrl(manifestPath).href))
.default as Manifest;
const outDir = join(dirname(fromFileUrl(manifest.baseUrl)), "_fresh");
// Ensure that build dir is empty
await fs.emptyDir(outDir);
const ctx = await ServerContext.fromManifest(manifest, {
...opts,
skipSnapshot: false,
});
// Bundle assets
const snapshot = await ctx.buildSnapshot();
// Write output files to disk
await Promise.all(snapshot.paths.map((fileName) => {
const data = snapshot.read(fileName);
if (data === null) return;
return Deno.writeFile(join(outDir, fileName), data);
}));
// Write dependency snapshot file to disk
const deps: Record<string, string[]> = {};
for (const filePath of snapshot.paths) {
const dependencies = snapshot.dependencies(filePath);
deps[filePath] = dependencies;
}
const snapshotPath = join(outDir, "snapshot.json");
await Deno.writeTextFile(snapshotPath, JSON.stringify(deps, null, 2));
}
+1
View File
@@ -23,6 +23,7 @@ export { gte } from "https://deno.land/std@0.193.0/semver/mod.ts";
export { existsSync } from "https://deno.land/std@0.193.0/fs/mod.ts";
export * as semver from "https://deno.land/std@0.195.0/semver/mod.ts";
export * as JSONC from "https://deno.land/std@0.195.0/jsonc/mod.ts";
export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts";
// ts-morph
export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts";
+12 -2
View File
@@ -11,6 +11,8 @@ import {
WalkEntry,
} from "./deps.ts";
import { error } from "./error.ts";
import { FreshOptions } from "../server/mod.ts";
import { build } from "./build.ts";
const MIN_DENO_VERSION = "1.31.0";
@@ -183,7 +185,11 @@ export default manifest;
);
}
export async function dev(base: string, entrypoint: string) {
export async function dev(
base: string,
entrypoint: string,
options: FreshOptions = {},
) {
ensureMinDenoVersion();
// Run update check in background
@@ -209,7 +215,11 @@ export async function dev(base: string, entrypoint: string) {
if (manifestChanged) await generate(dir, newManifest);
await import(entrypoint);
if (Deno.args.includes("build")) {
await build(join(dir, "fresh.gen.ts"), options);
} else {
await import(entrypoint);
}
}
function arraysEqual<T>(a: T[], b: T[]): boolean {