feat: pass resolved config to plugin.buildStart() (#1961)

This commit is contained in:
Marvin Hagemeister
2023-10-19 17:25:16 +02:00
committed by GitHub
parent c41cfc9227
commit cbd5b842b0
7 changed files with 85 additions and 61 deletions
+13 -10
View File
@@ -3,18 +3,21 @@ import { join } from "../server/deps.ts";
import { colors, fs } from "./deps.ts";
import { BuildSnapshotJson } from "../build/mod.ts";
import { BUILD_ID } from "../server/build_id.ts";
import { InternalFreshConfig } from "../server/types.ts";
import { InternalFreshState } from "../server/types.ts";
export async function build(
config: InternalFreshConfig,
state: InternalFreshState,
) {
// Ensure that build dir is empty
await fs.emptyDir(config.build.outDir);
const outDir = state.config.build.outDir;
const plugins = state.config.plugins;
await Promise.all(config.plugins.map((plugin) => plugin.buildStart?.()));
// Ensure that build dir is empty
await fs.emptyDir(outDir);
await Promise.all(plugins.map((plugin) => plugin.buildStart?.(state.config)));
// Bundle assets
const ctx = await getServerContext(config);
const ctx = await getServerContext(state);
const snapshot = await ctx.buildSnapshot();
// Write output files to disk
@@ -22,7 +25,7 @@ export async function build(
const data = await snapshot.read(fileName);
if (data === null) return;
return Deno.writeFile(join(config.build.outDir, fileName), data);
return Deno.writeFile(join(outDir, fileName), data);
}));
// Write dependency snapshot file to disk
@@ -35,12 +38,12 @@ export async function build(
jsonSnapshot.files[filePath] = dependencies;
}
const snapshotPath = join(config.build.outDir, "snapshot.json");
const snapshotPath = join(outDir, "snapshot.json");
await Deno.writeTextFile(snapshotPath, JSON.stringify(jsonSnapshot, null, 2));
console.log(
`Assets written to: ${colors.green(config.build.outDir)}`,
`Assets written to: ${colors.green(outDir)}`,
);
await Promise.all(config.plugins.map((plugin) => plugin.buildEnd?.()));
await Promise.all(plugins.map((plugin) => plugin.buildEnd?.()));
}
+10 -10
View File
@@ -4,7 +4,7 @@ import { FreshConfig, Manifest as ServerManifest } from "../server/mod.ts";
import { build } from "./build.ts";
import { collect, ensureMinDenoVersion, generate, Manifest } from "./mod.ts";
import { startServer } from "../server/boot.ts";
import { getFreshConfigWithDefaults } from "../server/config.ts";
import { getInternalFreshState } from "../server/config.ts";
import { getServerContext } from "$fresh/src/server/context.ts";
export async function dev(
@@ -39,22 +39,22 @@ export async function dev(
.default as ServerManifest;
if (Deno.args.includes("build")) {
const configWithDefaults = await getFreshConfigWithDefaults(
const state = await getInternalFreshState(
manifest,
config ?? {},
);
configWithDefaults.dev = false;
configWithDefaults.loadSnapshot = false;
await build(configWithDefaults);
state.config.dev = false;
state.loadSnapshot = false;
await build(state);
} else if (config) {
const configWithDefaults = await getFreshConfigWithDefaults(
const state = await getInternalFreshState(
manifest,
config,
);
configWithDefaults.dev = true;
configWithDefaults.loadSnapshot = false;
const ctx = await getServerContext(configWithDefaults);
await startServer(ctx.handler(), configWithDefaults.server);
state.config.dev = true;
state.loadSnapshot = false;
const ctx = await getServerContext(state);
await startServer(ctx.handler(), state.config.server);
} else {
// Legacy entry point: Back then `dev.ts` would call `main.ts` but
// this causes duplicate plugin instantiation if both `dev.ts` and