feat: pass resolved config to plugin.buildStart() (#1961)
This commit is contained in:
+13
-10
@@ -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
@@ -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
|
||||
|
||||
+17
-11
@@ -1,6 +1,10 @@
|
||||
import { dirname, fromFileUrl, isAbsolute, join, JSONC } from "./deps.ts";
|
||||
import { FromManifestConfig, Manifest } from "./mod.ts";
|
||||
import { DenoConfig, InternalFreshConfig } from "./types.ts";
|
||||
import {
|
||||
DenoConfig,
|
||||
InternalFreshState,
|
||||
ResolvedFreshConfig,
|
||||
} from "./types.ts";
|
||||
|
||||
export async function readDenoConfig(
|
||||
directory: string,
|
||||
@@ -37,10 +41,10 @@ function isObject(value: unknown) {
|
||||
!Array.isArray(value);
|
||||
}
|
||||
|
||||
export async function getFreshConfigWithDefaults(
|
||||
export async function getInternalFreshState(
|
||||
manifest: Manifest,
|
||||
config: FromManifestConfig,
|
||||
): Promise<InternalFreshConfig> {
|
||||
): Promise<InternalFreshState> {
|
||||
const base = dirname(fromFileUrl(manifest.baseUrl));
|
||||
const { config: denoJson, path: denoJsonPath } = await readDenoConfig(base);
|
||||
|
||||
@@ -50,14 +54,8 @@ export async function getFreshConfigWithDefaults(
|
||||
);
|
||||
}
|
||||
|
||||
const internalConfig: InternalFreshConfig = {
|
||||
loadSnapshot: typeof config.skipSnapshot === "boolean"
|
||||
? !config.skipSnapshot
|
||||
: false,
|
||||
const internalConfig: ResolvedFreshConfig = {
|
||||
dev: config.dev ?? false,
|
||||
denoJsonPath,
|
||||
denoJson,
|
||||
manifest,
|
||||
build: {
|
||||
outDir: "",
|
||||
target: config.build?.target ?? ["chrome99", "firefox99", "safari15"],
|
||||
@@ -102,7 +100,15 @@ export async function getFreshConfigWithDefaults(
|
||||
? parseFileOrUrl(config.staticDir, base)
|
||||
: join(base, "static");
|
||||
|
||||
return internalConfig;
|
||||
return {
|
||||
config: internalConfig,
|
||||
manifest,
|
||||
loadSnapshot: typeof config.skipSnapshot === "boolean"
|
||||
? !config.skipSnapshot
|
||||
: false,
|
||||
denoJsonPath,
|
||||
denoJson,
|
||||
};
|
||||
}
|
||||
|
||||
function parseFileOrUrl(input: string, base: string) {
|
||||
|
||||
+27
-26
@@ -19,7 +19,7 @@ import {
|
||||
ErrorPage,
|
||||
ErrorPageModule,
|
||||
Handler,
|
||||
InternalFreshConfig,
|
||||
InternalFreshState,
|
||||
Island,
|
||||
LayoutModule,
|
||||
LayoutRoute,
|
||||
@@ -56,7 +56,7 @@ import {
|
||||
import { InternalRoute } from "./router.ts";
|
||||
import { setAllIslands } from "./rendering/preact_hooks.ts";
|
||||
import { getCodeFrame } from "./code_frame.ts";
|
||||
import { getFreshConfigWithDefaults } from "./config.ts";
|
||||
import { getInternalFreshState } from "./config.ts";
|
||||
|
||||
const DEFAULT_CONN_INFO: ServeHandlerInfo = {
|
||||
localAddr: { transport: "tcp", hostname: "localhost", port: 8080 },
|
||||
@@ -88,15 +88,15 @@ export type FromManifestConfig = FreshConfig & {
|
||||
dev?: boolean;
|
||||
};
|
||||
|
||||
export async function getServerContext(opts: InternalFreshConfig) {
|
||||
const { manifest, denoJson: config, denoJsonPath: configPath } = opts;
|
||||
export async function getServerContext(state: InternalFreshState) {
|
||||
const { manifest, config, denoJson, denoJsonPath: configPath } = state;
|
||||
// Get the manifest' base URL.
|
||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||
|
||||
// Restore snapshot if available
|
||||
let snapshot: BuildSnapshot | null = null;
|
||||
if (opts.loadSnapshot) {
|
||||
const snapshotDirPath = opts.build.outDir;
|
||||
if (state.loadSnapshot) {
|
||||
const snapshotDirPath = config.build.outDir;
|
||||
try {
|
||||
if ((await Deno.stat(snapshotDirPath)).isDirectory) {
|
||||
console.log(
|
||||
@@ -128,10 +128,10 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
config.compilerOptions ??= {};
|
||||
denoJson.compilerOptions ??= {};
|
||||
|
||||
let jsx: "react" | "react-jsx";
|
||||
switch (config.compilerOptions.jsx) {
|
||||
switch (denoJson.compilerOptions.jsx) {
|
||||
case "react":
|
||||
case undefined:
|
||||
jsx = "react";
|
||||
@@ -140,12 +140,12 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
jsx = "react-jsx";
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unknown jsx option: " + config.compilerOptions.jsx);
|
||||
throw new Error("Unknown jsx option: " + denoJson.compilerOptions.jsx);
|
||||
}
|
||||
|
||||
const jsxConfig: JSXConfig = {
|
||||
jsx,
|
||||
jsxImportSource: config.compilerOptions.jsxImportSource,
|
||||
jsxImportSource: denoJson.compilerOptions.jsxImportSource,
|
||||
};
|
||||
|
||||
// Extract all routes, and prepare them into the `Page` structure.
|
||||
@@ -158,8 +158,8 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
let error: ErrorPage = DEFAULT_ERROR;
|
||||
const allRoutes = [
|
||||
...Object.entries(manifest.routes),
|
||||
...(opts.plugins ? getMiddlewareRoutesFromPlugins(opts.plugins) : []),
|
||||
...(opts.plugins ? getRoutesFromPlugins(opts.plugins) : []),
|
||||
...(config.plugins ? getMiddlewareRoutesFromPlugins(config.plugins) : []),
|
||||
...(config.plugins ? getRoutesFromPlugins(config.plugins) : []),
|
||||
];
|
||||
|
||||
// Presort all routes so that we only need to sort once
|
||||
@@ -273,7 +273,8 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
path === "/_500.tsx" || path === "/_500.ts" ||
|
||||
path === "/_500.jsx" || path === "/_500.js"
|
||||
) {
|
||||
const { default: component, config } = module as ErrorPageModule;
|
||||
const { default: component, config: routeConfig } =
|
||||
module as ErrorPageModule;
|
||||
let { handler } = module as ErrorPageModule;
|
||||
if (component && handler === undefined) {
|
||||
handler = (_req, { render }) => render();
|
||||
@@ -286,7 +287,7 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
name,
|
||||
component,
|
||||
handler: (req, ctx) => {
|
||||
if (opts.dev) {
|
||||
if (config.dev) {
|
||||
const prevComp = error.component;
|
||||
error.component = DefaultErrorHandler;
|
||||
try {
|
||||
@@ -300,9 +301,9 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
? handler(req, ctx)
|
||||
: router.defaultErrorHandler(req, ctx, ctx.error);
|
||||
},
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
appWrapper: !config?.skipAppWrapper,
|
||||
inheritLayouts: !config?.skipInheritedLayouts,
|
||||
csp: Boolean(routeConfig?.csp ?? false),
|
||||
appWrapper: !routeConfig?.skipAppWrapper,
|
||||
inheritLayouts: !routeConfig?.skipInheritedLayouts,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -336,8 +337,8 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
|
||||
const staticFiles: StaticFile[] = [];
|
||||
try {
|
||||
const staticDirUrl = toFileUrl(opts.staticDir);
|
||||
const entries = walk(opts.staticDir, {
|
||||
const staticDirUrl = toFileUrl(config.staticDir);
|
||||
const entries = walk(config.staticDir, {
|
||||
includeFiles: true,
|
||||
includeDirs: false,
|
||||
followSymlinks: false,
|
||||
@@ -374,7 +375,7 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.dev) {
|
||||
if (config.dev) {
|
||||
// Ensure that debugging hooks are set up for SSR rendering
|
||||
await import("preact/debug");
|
||||
}
|
||||
@@ -383,18 +384,18 @@ export async function getServerContext(opts: InternalFreshConfig) {
|
||||
routes,
|
||||
islands,
|
||||
staticFiles,
|
||||
opts.render ?? DEFAULT_RENDER_FN,
|
||||
config.render ?? DEFAULT_RENDER_FN,
|
||||
middlewares,
|
||||
app,
|
||||
layouts,
|
||||
notFound,
|
||||
error,
|
||||
opts.plugins ?? [],
|
||||
config.plugins ?? [],
|
||||
configPath,
|
||||
jsxConfig,
|
||||
opts.dev,
|
||||
opts.router ?? DEFAULT_ROUTER_OPTIONS,
|
||||
opts.build.target,
|
||||
config.dev,
|
||||
config.router ?? DEFAULT_ROUTER_OPTIONS,
|
||||
config.build.target,
|
||||
snapshot,
|
||||
);
|
||||
}
|
||||
@@ -470,7 +471,7 @@ export class ServerContext {
|
||||
config.skipSnapshot = true;
|
||||
}
|
||||
|
||||
const configWithDefaults = await getFreshConfigWithDefaults(
|
||||
const configWithDefaults = await getInternalFreshState(
|
||||
manifest,
|
||||
config,
|
||||
);
|
||||
|
||||
+8
-4
@@ -108,12 +108,16 @@ export interface FreshConfig {
|
||||
onListen?: (params: { hostname: string; port: number }) => void;
|
||||
}
|
||||
|
||||
export interface InternalFreshConfig {
|
||||
dev: boolean;
|
||||
export interface InternalFreshState {
|
||||
config: ResolvedFreshConfig;
|
||||
manifest: Manifest;
|
||||
loadSnapshot: boolean;
|
||||
denoJsonPath: string;
|
||||
denoJson: DenoConfig;
|
||||
manifest: Manifest;
|
||||
}
|
||||
|
||||
export interface ResolvedFreshConfig {
|
||||
dev: boolean;
|
||||
build: {
|
||||
outDir: string;
|
||||
target: string | string[];
|
||||
@@ -545,7 +549,7 @@ export interface Plugin<State = Record<string, unknown>> {
|
||||
/**
|
||||
* Called before running the Fresh build task
|
||||
*/
|
||||
buildStart?(): Promise<void> | void;
|
||||
buildStart?(config: ResolvedFreshConfig): Promise<void> | void;
|
||||
/**
|
||||
* Called after completing the Fresh build task
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { defineConfig } from "$fresh/src/server/defines.ts";
|
||||
import * as path from "https://deno.land/std@0.193.0/path/mod.ts";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
@@ -20,5 +21,12 @@ export default defineConfig({
|
||||
console.log("Plugin b: buildStart");
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "c",
|
||||
buildStart(config) {
|
||||
const outDir = path.relative(Deno.cwd(), config.build.outDir);
|
||||
console.log(`Plugin c: ${outDir}`);
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
assertEquals,
|
||||
assertStringIncludes,
|
||||
delay,
|
||||
join,
|
||||
puppeteer,
|
||||
} from "./deps.ts";
|
||||
import manifest from "./fixture_plugin/fresh.gen.ts";
|
||||
@@ -146,6 +147,7 @@ Deno.test("calls buildStart() and buildEnd()", async () => {
|
||||
assertEquals(out, [
|
||||
"Plugin a: buildStart",
|
||||
"Plugin b: buildStart",
|
||||
`Plugin c: ${join("tests", "fixture_plugin_lifecycle", "_fresh")}`,
|
||||
"Plugin a: buildEnd",
|
||||
"Plugin b: buildEnd",
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user