fix: allow relative paths for outDir and staticDir (#2914)

This PR consists of several commits, where the goal is to re-enable the
test (fix for Windows), as well as allow relative paths for config
options to address #2913.

1. Re-enables test and normalizes paths for Windows
2. Add unit test for `normalizeConfig` and the `root` option
3. Add unit test for `normalizeConfig` for `build.outDir` and
`staticDir`, and allow the properties to be relative
4. Add JSDoc for fields in `FreshConfig`

I can split these into separate PR's if preferred, but all code changes
are very in nearly the same places.

Closes #2913
This commit is contained in:
Christian Svensson
2025-05-12 09:43:01 +02:00
committed by GitHub
parent d10ea5a89a
commit c0e2b80690
2 changed files with 143 additions and 21 deletions
+46 -17
View File
@@ -1,20 +1,37 @@
import * as path from "@std/path";
export interface FreshConfig {
/**
* The root directory of the Fresh project.
*
* Other paths, such as `build.outDir`, `staticDir`, and `fsRoutes()`
* are resolved relative to this directory.
* @default Deno.cwd()
*/
root?: string;
build?: {
/**
* The directory to write generated files to when `dev.ts build` is run.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "_fresh"
*/
outDir?: string;
};
/**
* Serve fresh from a base path instead of from the root.
* "/foo/bar" -> http://localhost:8000/foo/bar
* @default {undefined}
* @default undefined
*/
basePath?: string;
/**
* The directory to serve static files from.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "static"
*/
staticDir?: string;
}
@@ -35,35 +52,47 @@ export interface ResolvedFreshConfig {
}
export function parseRootPath(root: string, cwd: string): string {
if (root.startsWith("file://")) {
root = path.fromFileUrl(root);
} else if (!path.isAbsolute(root)) {
root = path.join(cwd, root);
return parseDirPath(root, cwd, true);
}
function parseDirPath(
dirPath: string,
root: string,
fileToDir = false,
): string {
if (dirPath.startsWith("file://")) {
dirPath = path.fromFileUrl(dirPath);
} else if (!path.isAbsolute(dirPath)) {
dirPath = path.join(root, dirPath);
}
const ext = path.extname(root);
if (
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
ext === ".mjs"
) {
root = path.dirname(root);
if (fileToDir) {
const ext = path.extname(dirPath);
if (
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
ext === ".mjs"
) {
dirPath = path.dirname(dirPath);
}
}
return root;
if (Deno.build.os === "windows") {
dirPath = dirPath.replaceAll("\\", "/");
}
return dirPath;
}
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
const root = options.root
? parseRootPath(options.root, Deno.cwd())
: Deno.cwd();
const root = parseRootPath(options.root ?? ".", Deno.cwd());
return {
root,
build: {
outDir: options.build?.outDir ?? path.join(root, "_fresh"),
outDir: parseDirPath(options.build?.outDir ?? "_fresh", root),
},
basePath: options.basePath ?? "",
staticDir: options.staticDir ?? path.join(root, "static"),
staticDir: parseDirPath(options.staticDir ?? "static", root),
mode: "production",
};
}