feat: prebundle code + restore from snapshot (#1604)
This commit is contained in:
@@ -163,7 +163,7 @@ export class EsbuildSnapshot implements BuildSnapshot {
|
||||
}
|
||||
|
||||
get paths(): string[] {
|
||||
return Object.keys(this.#files);
|
||||
return Array.from(this.#files.keys());
|
||||
}
|
||||
|
||||
read(path: string): Uint8Array | null {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
+42
-4
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
colors,
|
||||
dirname,
|
||||
extname,
|
||||
fromFileUrl,
|
||||
@@ -51,6 +52,7 @@ import {
|
||||
Builder,
|
||||
BuildSnapshot,
|
||||
EsbuildBuilder,
|
||||
EsbuildSnapshot,
|
||||
JSXConfig,
|
||||
} from "../build/mod.ts";
|
||||
import { InternalRoute } from "./router.ts";
|
||||
@@ -117,6 +119,7 @@ export class ServerContext {
|
||||
jsxConfig: JSXConfig,
|
||||
dev: boolean = isDevMode(),
|
||||
routerOptions: RouterOptions,
|
||||
snapshot: BuildSnapshot | null = null,
|
||||
) {
|
||||
this.#routes = routes;
|
||||
this.#islands = islands;
|
||||
@@ -129,7 +132,7 @@ export class ServerContext {
|
||||
this.#error = error;
|
||||
this.#plugins = plugins;
|
||||
this.#dev = dev;
|
||||
this.#builder = new EsbuildBuilder({
|
||||
this.#builder = snapshot ?? new EsbuildBuilder({
|
||||
buildID: BUILD_ID,
|
||||
entrypoints: collectEntrypoints(this.#dev, this.#islands, this.#plugins),
|
||||
configPath,
|
||||
@@ -144,7 +147,7 @@ export class ServerContext {
|
||||
*/
|
||||
static async fromManifest(
|
||||
manifest: Manifest,
|
||||
opts: FreshOptions,
|
||||
opts: FreshOptions & { skipSnapshot?: boolean },
|
||||
): Promise<ServerContext> {
|
||||
// Get the manifest' base URL.
|
||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||
@@ -158,6 +161,40 @@ export class ServerContext {
|
||||
);
|
||||
}
|
||||
|
||||
// Restore snapshot if available
|
||||
let snapshot: BuildSnapshot | null = null;
|
||||
// Load from snapshot if not explicitly requested not to
|
||||
const loadFromSnapshot = !opts.skipSnapshot;
|
||||
if (loadFromSnapshot) {
|
||||
const snapshotDirPath = join(dirname(configPath), "_fresh");
|
||||
try {
|
||||
if ((await Deno.stat(snapshotDirPath)).isDirectory) {
|
||||
console.log(
|
||||
`Using snapshot found at ${colors.cyan(snapshotDirPath)}`,
|
||||
);
|
||||
|
||||
const snapshotPath = join(snapshotDirPath, "snapshot.json");
|
||||
const json = JSON.parse(await Deno.readTextFile(snapshotPath));
|
||||
const dependencies = new Map<string, string[]>(
|
||||
Object.entries(json),
|
||||
);
|
||||
|
||||
const files = new Map();
|
||||
const names = Object.keys(json);
|
||||
await Promise.all(names.map(async (name) => {
|
||||
const filePath = join(snapshotDirPath, name);
|
||||
files.set(name, await Deno.readFile(filePath));
|
||||
}));
|
||||
|
||||
snapshot = new EsbuildSnapshot(files, dependencies);
|
||||
}
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config.compilerOptions ??= {};
|
||||
|
||||
let jsx: "react" | "react-jsx";
|
||||
@@ -415,6 +452,7 @@ export class ServerContext {
|
||||
jsxConfig,
|
||||
dev,
|
||||
opts.router ?? DEFAULT_ROUTER_OPTIONS,
|
||||
snapshot,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -474,7 +512,7 @@ export class ServerContext {
|
||||
return this.#builder;
|
||||
}
|
||||
|
||||
async #buildSnapshot() {
|
||||
async buildSnapshot() {
|
||||
if ("build" in this.#builder) {
|
||||
const builder = this.#builder;
|
||||
this.#builder = builder.build();
|
||||
@@ -950,7 +988,7 @@ export class ServerContext {
|
||||
*/
|
||||
#bundleAssetRoute = (): router.MatchHandler => {
|
||||
return async (_req, _ctx, params) => {
|
||||
const snapshot = await this.#buildSnapshot();
|
||||
const snapshot = await this.buildSnapshot();
|
||||
const contents = snapshot.read(params.path);
|
||||
if (!contents) return new Response(null, { status: 404 });
|
||||
|
||||
|
||||
@@ -4,10 +4,15 @@ import {
|
||||
AsyncRoute,
|
||||
LayoutContext,
|
||||
RouteContext,
|
||||
StartOptions,
|
||||
} from "./types.ts";
|
||||
import { checkAsyncComponent } from "./render.ts";
|
||||
import { AppContext } from "$fresh/server.ts";
|
||||
|
||||
export function defineConfig(config: StartOptions): StartOptions {
|
||||
return config;
|
||||
}
|
||||
|
||||
// Route creation helpers
|
||||
export function defineRoute<
|
||||
T,
|
||||
|
||||
+7
-1
@@ -15,7 +15,12 @@ import {
|
||||
StartOptions,
|
||||
UnknownHandler,
|
||||
} from "./types.ts";
|
||||
export { defineApp, defineLayout, defineRoute } from "./defines.ts";
|
||||
export {
|
||||
defineApp,
|
||||
defineConfig,
|
||||
defineLayout,
|
||||
defineRoute,
|
||||
} from "./defines.ts";
|
||||
export type {
|
||||
AppContext,
|
||||
AppProps,
|
||||
@@ -78,6 +83,7 @@ export interface Manifest {
|
||||
export interface DenoConfig {
|
||||
imports?: Record<string, string>;
|
||||
importMap?: string;
|
||||
tasks?: Record<string, string>;
|
||||
compilerOptions?: {
|
||||
jsx?: string;
|
||||
jsxImportSource?: string;
|
||||
|
||||
Reference in New Issue
Block a user