refactor: replace references of options with config (#1903)
This commit is contained in:
+34
-34
@@ -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
@@ -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,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
@@ -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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user