feat: add build.outDir option (#1797)

This commit is contained in:
Marvin Hagemeister
2023-09-20 10:40:09 +02:00
committed by GitHub
parent ff6ae7eb7f
commit 3fd8fe89d3
30 changed files with 846 additions and 448 deletions
+17 -19
View File
@@ -1,28 +1,20 @@
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";
import { getServerContext } from "../server/context.ts";
import { join } from "../server/deps.ts";
import { colors, fs } from "./deps.ts";
import { BuildSnapshotJson } from "../build/mod.ts";
import { BUILD_ID } from "$fresh/src/server/build_id.ts";
import { BUILD_ID } from "../server/build_id.ts";
import { InternalFreshOptions } from "../server/types.ts";
export async function build(
manifestPath: string,
opts: FreshOptions,
config: InternalFreshOptions,
) {
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);
await fs.emptyDir(config.build.outDir);
const ctx = await ServerContext.fromManifest(manifest, {
...opts,
skipSnapshot: true,
});
await Promise.all(config.plugins.map((plugin) => plugin.buildStart?.()));
// Bundle assets
const ctx = await getServerContext(config);
const snapshot = await ctx.buildSnapshot();
// Write output files to disk
@@ -30,7 +22,7 @@ export async function build(
const data = await snapshot.read(fileName);
if (data === null) return;
return Deno.writeFile(join(outDir, fileName), data);
return Deno.writeFile(join(config.build.outDir, fileName), data);
}));
// Write dependency snapshot file to disk
@@ -43,6 +35,12 @@ export async function build(
jsonSnapshot.files[filePath] = dependencies;
}
const snapshotPath = join(outDir, "snapshot.json");
const snapshotPath = join(config.build.outDir, "snapshot.json");
await Deno.writeTextFile(snapshotPath, JSON.stringify(jsonSnapshot, null, 2));
console.log(
`Assets written to: ${colors.green(config.build.outDir)}`,
);
await Promise.all(config.plugins.map((plugin) => plugin.buildEnd?.()));
}
+19 -24
View File
@@ -1,13 +1,11 @@
import { updateCheck } from "./update_check.ts";
import { DAY, dirname, fromFileUrl, fs, join, toFileUrl } from "./deps.ts";
import {
FreshOptions,
Manifest as ServerManifest,
ServerContext,
} from "../server/mod.ts";
import { DAY, dirname, fromFileUrl, join, toFileUrl } from "./deps.ts";
import { FreshOptions, Manifest as ServerManifest } from "../server/mod.ts";
import { build } from "./build.ts";
import { collect, ensureMinDenoVersion, generate, Manifest } from "./mod.ts";
import { startFromContext } from "../server/boot.ts";
import { getFreshConfigWithDefaults } from "../server/config.ts";
import { getServerContext } from "$fresh/src/server/context.ts";
export async function dev(
base: string,
@@ -40,25 +38,22 @@ export async function dev(
const manifest = (await import(toFileUrl(join(dir, "fresh.gen.ts")).href))
.default as ServerManifest;
const outDir = join(dir, "_fresh");
const isBuild = Deno.args.includes("build");
if (isBuild) {
// Ensure that build dir is empty
await fs.emptyDir(outDir);
options = options ?? {};
const plugins = options.plugins ?? [];
await Promise.all(plugins.map((plugin) => plugin.buildStart?.()));
await build(join(dir, "fresh.gen.ts"), options);
await Promise.all(plugins.map((plugin) => plugin.buildEnd?.()));
if (Deno.args.includes("build")) {
const config = await getFreshConfigWithDefaults(
manifest,
options ?? {},
);
config.dev = false;
config.loadSnapshot = false;
await build(config);
} else if (options) {
const ctx = await ServerContext.fromManifest(manifest, {
...options,
skipSnapshot: true,
dev: true,
});
const config = await getFreshConfigWithDefaults(
manifest,
options,
);
config.dev = true;
config.loadSnapshot = false;
const ctx = await getServerContext(config);
await startFromContext(ctx, options);
} else {
// Legacy entry point: Back then `dev.ts` would call `main.ts` but