refactor: replace references of options with config (#1903)

This commit is contained in:
Asher Gomez
2023-10-11 09:25:03 +02:00
committed by GitHub
parent 1d12853889
commit 6ddef91d95
28 changed files with 138 additions and 127 deletions
+5 -11
View File
@@ -8,22 +8,16 @@ In this page we discuss how the server can be configured during startup.
The signature of the primary method looks like this:
```ts main.ts
export async function start(manifest: Manifest, opts: StartOptions = {});
export async function start(manifest: Manifest, config: FreshConfig = {});
```
## Options
## Configuration
`Manifest` comes from `fresh.gen.ts`, so nothing to do there. `opts` is where
things get interesting. `StartOptions` looks like this:
```
export type StartOptions = ServeInit & FreshOptions;
```
The good stuff is really in...
`Manifest` comes from `fresh.gen.ts`, so nothing to do there. `config` is where
things get interesting. `FreshConfig` looks like this:
```ts
export interface FreshOptions {
export interface FreshConfig {
render?: RenderFunction;
plugins?: Plugin[];
staticDir?: string;
@@ -35,7 +35,7 @@ passing the extra folder as an argument: `--watch=static/`. You should also add
`routes/` to the watch list, so that the server restarts automatically whenever
you add a new route.
If you want to change the port or host, modify the options bag of the `start()`
If you want to change the port or host, modify the config bag of the `start()`
call in `main.ts` to include an explicit port number:
```js main.ts
+2 -2
View File
@@ -3,10 +3,10 @@ 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 { InternalFreshOptions } from "../server/types.ts";
import { InternalFreshConfig } from "../server/types.ts";
export async function build(
config: InternalFreshOptions,
config: InternalFreshConfig,
) {
// Ensure that build dir is empty
await fs.emptyDir(config.build.outDir);
+15 -15
View File
@@ -1,6 +1,6 @@
import { updateCheck } from "./update_check.ts";
import { DAY, dirname, fromFileUrl, join, toFileUrl } from "./deps.ts";
import { FreshOptions, Manifest as ServerManifest } from "../server/mod.ts";
import { FreshConfig, 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";
@@ -10,7 +10,7 @@ import { getServerContext } from "$fresh/src/server/context.ts";
export async function dev(
base: string,
entrypoint: string,
options?: FreshOptions,
config?: FreshConfig,
) {
ensureMinDenoVersion();
@@ -26,7 +26,7 @@ export async function dev(
} else {
currentManifest = { islands: [], routes: [] };
}
const newManifest = await collect(dir, options?.router?.ignoreFilePattern);
const newManifest = await collect(dir, config?.router?.ignoreFilePattern);
Deno.env.set("FRSH_DEV_PREVIOUS_MANIFEST", JSON.stringify(newManifest));
const manifestChanged =
@@ -39,22 +39,22 @@ export async function dev(
.default as ServerManifest;
if (Deno.args.includes("build")) {
const config = await getFreshConfigWithDefaults(
const configWithDefaults = await getFreshConfigWithDefaults(
manifest,
options ?? {},
config ?? {},
);
config.dev = false;
config.loadSnapshot = false;
await build(config);
} else if (options) {
const config = await getFreshConfigWithDefaults(
configWithDefaults.dev = false;
configWithDefaults.loadSnapshot = false;
await build(configWithDefaults);
} else if (config) {
const configWithDefaults = await getFreshConfigWithDefaults(
manifest,
options,
config,
);
config.dev = true;
config.loadSnapshot = false;
const ctx = await getServerContext(config);
await startFromContext(ctx, config.server);
configWithDefaults.dev = true;
configWithDefaults.loadSnapshot = false;
const ctx = await getServerContext(configWithDefaults);
await startFromContext(ctx, configWithDefaults.server);
} else {
// Legacy entry point: Back then `dev.ts` would call `main.ts` but
// this causes duplicate plugin instantiation if both `dev.ts` and
+34 -34
View File
@@ -1,6 +1,6 @@
import { dirname, fromFileUrl, isAbsolute, join, JSONC } from "./deps.ts";
import { FromManifestOptions, Manifest } from "./mod.ts";
import { DenoConfig, InternalFreshOptions } from "./types.ts";
import { FromManifestConfig, Manifest } from "./mod.ts";
import { DenoConfig, InternalFreshConfig } from "./types.ts";
export async function readDenoConfig(
directory: string,
@@ -39,8 +39,8 @@ function isObject(value: unknown) {
export async function getFreshConfigWithDefaults(
manifest: Manifest,
opts: FromManifestOptions,
): Promise<InternalFreshOptions> {
config: FromManifestConfig,
): Promise<InternalFreshConfig> {
const base = dirname(fromFileUrl(manifest.baseUrl));
const { config: denoJson, path: denoJsonPath } = await readDenoConfig(base);
@@ -50,59 +50,59 @@ export async function getFreshConfigWithDefaults(
);
}
const config: InternalFreshOptions = {
loadSnapshot: typeof opts.skipSnapshot === "boolean"
? !opts.skipSnapshot
const internalConfig: InternalFreshConfig = {
loadSnapshot: typeof config.skipSnapshot === "boolean"
? !config.skipSnapshot
: false,
dev: opts.dev ?? false,
dev: config.dev ?? false,
denoJsonPath,
denoJson,
manifest,
build: {
outDir: "",
target: opts.build?.target ?? ["chrome99", "firefox99", "safari15"],
target: config.build?.target ?? ["chrome99", "firefox99", "safari15"],
},
plugins: opts.plugins ?? [],
plugins: config.plugins ?? [],
staticDir: "",
render: opts.render,
router: opts.router,
server: opts.server ?? {},
render: config.render,
router: config.router,
server: config.server ?? {},
};
if (opts.cert) {
config.server.cert = opts.cert;
if (config.cert) {
internalConfig.server.cert = config.cert;
}
if (opts.hostname) {
config.server.hostname = opts.hostname;
if (config.hostname) {
internalConfig.server.hostname = config.hostname;
}
if (opts.key) {
config.server.key = opts.key;
if (config.key) {
internalConfig.server.key = config.key;
}
if (opts.onError) {
config.server.onError = opts.onError;
if (config.onError) {
internalConfig.server.onError = config.onError;
}
if (opts.onListen) {
config.server.onListen = opts.onListen;
if (config.onListen) {
internalConfig.server.onListen = config.onListen;
}
if (opts.port) {
config.server.port = opts.port;
if (config.port) {
internalConfig.server.port = config.port;
}
if (opts.reusePort) {
config.server.reusePort = opts.reusePort;
if (config.reusePort) {
internalConfig.server.reusePort = config.reusePort;
}
if (opts.signal) {
config.server.signal = opts.signal;
if (config.signal) {
internalConfig.server.signal = config.signal;
}
config.build.outDir = opts.build?.outDir
? parseFileOrUrl(opts.build.outDir, base)
internalConfig.build.outDir = config.build?.outDir
? parseFileOrUrl(config.build.outDir, base)
: join(base, "_fresh");
config.staticDir = opts.staticDir
? parseFileOrUrl(opts.staticDir, base)
internalConfig.staticDir = config.staticDir
? parseFileOrUrl(config.staticDir, base)
: join(base, "static");
return config;
return internalConfig;
}
function parseFileOrUrl(input: string, base: string) {
+18 -11
View File
@@ -9,7 +9,7 @@ import {
} from "./deps.ts";
import { ComponentType, h } from "preact";
import * as router from "./router.ts";
import { Manifest } from "./mod.ts";
import { FreshConfig, Manifest } from "./mod.ts";
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import { BUILD_ID, setBuildId } from "./build_id.ts";
import DefaultErrorHandler from "./default_error_page.tsx";
@@ -18,9 +18,8 @@ import {
BaseRoute,
ErrorPage,
ErrorPageModule,
FreshOptions,
Handler,
InternalFreshOptions,
InternalFreshConfig,
Island,
LayoutModule,
LayoutRoute,
@@ -79,12 +78,17 @@ interface StaticFile {
etag: string;
}
export type FromManifestOptions = FreshOptions & {
/**
* @deprecated Use {@linkcode FromManifestConfig} instead
*/
export type FromManifestOptions = FromManifestConfig;
export type FromManifestConfig = FreshConfig & {
skipSnapshot?: boolean;
dev?: boolean;
};
export async function getServerContext(opts: InternalFreshOptions) {
export async function getServerContext(opts: InternalFreshConfig) {
const { manifest, denoJson: config, denoJsonPath: configPath } = opts;
// Get the manifest' base URL.
const baseUrl = new URL("./", manifest.baseUrl).href;
@@ -455,18 +459,21 @@ export class ServerContext {
*/
static async fromManifest(
manifest: Manifest,
opts: FromManifestOptions,
config: FromManifestConfig,
): Promise<ServerContext> {
const isLegacyDev = Deno.env.get("__FRSH_LEGACY_DEV") === "true";
opts.dev = isLegacyDev ||
Boolean(opts.dev);
config.dev = isLegacyDev ||
Boolean(config.dev);
if (isLegacyDev) {
opts.skipSnapshot = true;
config.skipSnapshot = true;
}
const config = await getFreshConfigWithDefaults(manifest, opts);
return getServerContext(config);
const configWithDefaults = await getFreshConfigWithDefaults(
manifest,
config,
);
return getServerContext(configWithDefaults);
}
/**
+2 -2
View File
@@ -2,14 +2,14 @@ import { ComponentChildren } from "preact";
import {
AsyncLayout,
AsyncRoute,
FreshConfig,
LayoutContext,
RouteContext,
StartOptions,
} from "./types.ts";
import { checkAsyncComponent } from "./render.ts";
import { AppContext } from "$fresh/server.ts";
export function defineConfig(config: StartOptions): StartOptions {
export function defineConfig(config: FreshConfig): FreshConfig {
return config;
}
+8 -7
View File
@@ -1,8 +1,9 @@
import { ServerContext } from "./context.ts";
export type { FromManifestOptions } from "./context.ts";
export type { FromManifestConfig, FromManifestOptions } from "./context.ts";
export { Status } from "./deps.ts";
import {
ErrorHandler,
FreshConfig,
Handler,
Handlers,
IslandModule,
@@ -10,7 +11,6 @@ import {
MiddlewareModule,
RouteConfig,
ServeHandlerInfo,
StartOptions,
UnknownHandler,
} from "./types.ts";
import { startFromContext } from "./boot.ts";
@@ -27,6 +27,7 @@ export type {
ErrorHandler,
ErrorHandlerContext,
ErrorPageProps,
FreshConfig,
FreshOptions,
Handler,
HandlerContext,
@@ -86,19 +87,19 @@ export { ServerContext };
export async function createHandler(
manifest: Manifest,
opts: StartOptions = {},
config: FreshConfig = {},
): Promise<
(req: Request, connInfo?: ServeHandlerInfo) => Promise<Response>
> {
const ctx = await ServerContext.fromManifest(manifest, opts);
const ctx = await ServerContext.fromManifest(manifest, config);
return ctx.handler();
}
export async function start(manifest: Manifest, opts: StartOptions = {}) {
export async function start(manifest: Manifest, config: FreshConfig = {}) {
const ctx = await ServerContext.fromManifest(manifest, {
...opts,
...config,
skipSnapshot: false,
dev: false,
});
await startFromContext(ctx, opts.server ?? opts);
await startFromContext(ctx, config.server ?? config);
}
+11 -3
View File
@@ -23,9 +23,17 @@ export interface DenoConfig {
// --- APPLICATION CONFIGURATION ---
export type StartOptions = FreshOptions;
/**
* @deprecated Use {@linkcode FreshConfig} instead
*/
export type StartOptions = FreshConfig;
export interface FreshOptions {
/**
* @deprecated Use {@linkcode FreshConfig} interface instead.
*/
export type FreshOptions = FreshConfig;
export interface FreshConfig {
build?: {
/**
* The directory to write generated files to when `dev.ts build` is run.
@@ -100,7 +108,7 @@ export interface FreshOptions {
onListen?: (params: { hostname: string; port: number }) => void;
}
export interface InternalFreshOptions {
export interface InternalFreshConfig {
dev: boolean;
loadSnapshot: boolean;
denoJsonPath: string;
@@ -1,4 +1,4 @@
import { FreshOptions } from "$fresh/server.ts";
import { FreshConfig } from "$fresh/server.ts";
export default {
async render(_ctx, render) {
@@ -8,4 +8,4 @@ export default {
throw new Error("body is missing");
}
},
} as FreshOptions;
} as FreshConfig;
+2 -2
View File
@@ -6,6 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
+2 -2
View File
@@ -6,7 +6,7 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
// this just exists to function as a type check to assert that we can actually pass a key and cert in
await start(manifest, { ...options, key: "test", cert: "test" });
await start(manifest, { ...config, key: "test", cert: "test" });
+2 -2
View File
@@ -9,6 +9,6 @@
import "./polyfill_deno_deploy.ts";
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
@@ -1,4 +1,4 @@
import { FreshOptions } from "$fresh/server.ts";
import { FreshConfig } from "$fresh/server.ts";
import cssInjectPlugin from "./utils/css-inject-plugin.ts";
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts";
@@ -13,4 +13,4 @@ export default {
routePlugin({ title: "Title Set From Plugin Config" }),
secondMiddlewarePlugin(),
],
} as FreshOptions;
} as FreshConfig;
+3 -2
View File
@@ -6,5 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
await start(manifest, options);
import config from "./fresh.config.ts";
await start(manifest, config);
@@ -1,4 +1,4 @@
import { FreshOptions } from "$fresh/server.ts";
import { FreshConfig } from "$fresh/server.ts";
export default {
async render(_ctx, render) {
@@ -8,4 +8,4 @@ export default {
throw new Error("body is missing");
}
},
} as FreshOptions;
} as FreshConfig;
+2 -2
View File
@@ -6,6 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
+2 -2
View File
@@ -9,6 +9,6 @@
import "./polyfill_deno_deploy.ts";
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
+2 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await dev(import.meta.url, "./main.ts", options);
await dev(import.meta.url, "./main.ts", config);
@@ -1,7 +1,7 @@
import { FreshOptions } from "$fresh/server.ts";
import { FreshConfig } from "$fresh/server.ts";
export default {
router: {
ignoreFilePattern: /[\.|_]cy\.[t|j]s(x)?$/,
},
} as FreshOptions;
} as FreshConfig;
+2 -2
View File
@@ -6,6 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
@@ -0,0 +1,5 @@
import { FreshConfig } from "$fresh/server.ts";
import twindPlugin from "../../plugins/twindv1.ts";
import twindConfig from "./twind.config.ts";
export default { plugins: [twindPlugin(twindConfig)] } as FreshConfig;
+2 -2
View File
@@ -6,6 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import options from "./options.ts";
import config from "./fresh.config.ts";
await start(manifest, options);
await start(manifest, config);
-5
View File
@@ -1,5 +0,0 @@
import { FreshOptions } from "$fresh/server.ts";
import twindPlugin from "../../plugins/twindv1.ts";
import twindConfig from "./twind.config.ts";
export default { plugins: [twindPlugin(twindConfig)] } as FreshOptions;
+3 -3
View File
@@ -10,7 +10,7 @@ import {
retry,
} from "./deps.ts";
import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts";
import config from "./fixture/fresh.config.ts";
import { BUILD_ID } from "../src/server/build_id.ts";
import {
assertSelector,
@@ -22,7 +22,7 @@ import {
withPageName,
} from "./test_utils.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const ctx = await ServerContext.fromManifest(manifest, config);
const handler = ctx.handler();
Deno.test("/ page prerender", async () => {
@@ -303,7 +303,7 @@ Deno.test("/foo/:path*", async () => {
Deno.test("static files in custom directory", async () => {
const newCtx = await ServerContext.fromManifest(manifest, {
...options,
...config,
staticDir: "./custom_static",
});
const newRouter = (req: Request) => {
+2 -2
View File
@@ -8,7 +8,7 @@ import {
puppeteer,
} from "./deps.ts";
import manifest from "./fixture_plugin/fresh.gen.ts";
import options from "./fixture_plugin/options.ts";
import config from "./fixture_plugin/fresh.config.ts";
import {
clickWhenListenerReady,
runBuild,
@@ -16,7 +16,7 @@ import {
withPageName,
} from "./test_utils.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const ctx = await ServerContext.fromManifest(manifest, config);
const handler = ctx.handler();
const router = (req: Request) => {
return handler(req, {
+2 -2
View File
@@ -2,9 +2,9 @@ import { assertSelector, parseHtml } from "$fresh/tests/test_utils.ts";
import { assertEquals } from "$std/testing/asserts.ts";
import { createHandler } from "$fresh/server.ts";
import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts";
import config from "./fixture/fresh.config.ts";
const handler = await createHandler(manifest, options);
const handler = await createHandler(manifest, config);
// Issue: https://github.com/denoland/fresh/issues/1636
Deno.test("doesn't leak data across renderers", async () => {
+3 -3
View File
@@ -2,7 +2,7 @@ import { colors, toFileUrl } from "$fresh/src/server/deps.ts";
import { assert } from "$std/_util/asserts.ts";
import * as path from "$std/path/mod.ts";
import {
FromManifestOptions,
FromManifestConfig,
Manifest,
ServeHandlerInfo,
ServerContext,
@@ -252,9 +252,9 @@ async function handleRequest(
export async function fakeServe(
manifest: Manifest,
options: FromManifestOptions,
config: FromManifestConfig,
): Promise<FakeServer> {
const ctx = await ServerContext.fromManifest(manifest, options);
const ctx = await ServerContext.fromManifest(manifest, config);
const handler = ctx.handler();
const conn: ServeHandlerInfo = {