-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathconfig.ts
More file actions
92 lines (82 loc) · 2.37 KB
/
config.ts
File metadata and controls
92 lines (82 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
/**
* 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"
*/
buildOutDir?: string;
/**
* Serve fresh from a base path instead of from the root.
* "/foo/bar" -> http://localhost:8000/foo/bar
* @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;
}
/**
* The final resolved Fresh configuration where fields the user didn't specify are set to the default values.
*/
export interface ResolvedFreshConfig extends Required<FreshConfig> {
/**
* The mode Fresh can run in.
*/
mode: "development" | "production";
}
export function parseRootPath(root: string, cwd: string): string {
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);
}
if (fileToDir) {
const ext = path.extname(dirPath);
if (
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
ext === ".mjs"
) {
dirPath = path.dirname(dirPath);
}
}
if (Deno.build.os === "windows") {
dirPath = dirPath.replaceAll("\\", "/");
}
return dirPath;
}
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
const root = parseRootPath(options.root ?? ".", Deno.cwd());
return {
root,
buildOutDir: parseDirPath(options.buildOutDir ?? "_fresh", root),
basePath: options.basePath ?? "",
staticDir: parseDirPath(options.staticDir ?? "static", root),
mode: "production",
};
}
export function getSnapshotPath(config: ResolvedFreshConfig): string {
return path.join(config.buildOutDir, "snapshot.json");
}