feat: prebundle code + restore from snapshot (#1604)
This commit is contained in:
+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 });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user