Skip to content

Commit 374ba76

Browse files
committed
fix: allow relative paths for outDir and staticDir
1 parent cc83b57 commit 374ba76

2 files changed

Lines changed: 85 additions & 14 deletions

File tree

src/config.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,35 @@ export interface ResolvedFreshConfig {
3535
}
3636

3737
export function parseRootPath(root: string, cwd: string): string {
38-
if (root.startsWith("file://")) {
39-
root = path.fromFileUrl(root);
40-
} else if (!path.isAbsolute(root)) {
41-
root = path.join(cwd, root);
38+
return parseDirPath(root, cwd, true);
39+
}
40+
41+
function parseDirPath(
42+
dirPath: string,
43+
root: string,
44+
fileToDir = false,
45+
): string {
46+
if (dirPath.startsWith("file://")) {
47+
dirPath = path.fromFileUrl(dirPath);
48+
} else if (!path.isAbsolute(dirPath)) {
49+
dirPath = path.join(root, dirPath);
4250
}
4351

44-
const ext = path.extname(root);
45-
if (
46-
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
47-
ext === ".mjs"
48-
) {
49-
root = path.dirname(root);
52+
if (fileToDir) {
53+
const ext = path.extname(dirPath);
54+
if (
55+
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
56+
ext === ".mjs"
57+
) {
58+
dirPath = path.dirname(dirPath);
59+
}
5060
}
5161

5262
if (Deno.build.os === "windows") {
53-
root = root.replaceAll("\\", "/");
63+
dirPath = dirPath.replaceAll("\\", "/");
5464
}
5565

56-
return root;
66+
return dirPath;
5767
}
5868

5969
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
@@ -62,10 +72,10 @@ export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
6272
return {
6373
root,
6474
build: {
65-
outDir: options.build?.outDir ?? path.join(root, "_fresh"),
75+
outDir: parseDirPath(options.build?.outDir ?? "_fresh", root),
6676
},
6777
basePath: options.basePath ?? "",
68-
staticDir: options.staticDir ?? path.join(root, "static"),
78+
staticDir: parseDirPath(options.staticDir ?? "static", root),
6979
mode: "production",
7080
};
7181
}

src/config_test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from "@std/expect";
22
import { normalizeConfig, parseRootPath } from "./config.ts";
3+
import type { FreshConfig } from "./mod.ts";
34

45
Deno.test("parseRootPath", () => {
56
const cwd = Deno.cwd().replaceAll("\\", "/");
@@ -35,3 +36,63 @@ Deno.test("normalizeConfig - root", () => {
3536
expect(configRoot("./foo/bar")).toEqual(`${cwd}/foo/bar`);
3637
expect(configRoot("./foo/bar.ts")).toEqual(`${cwd}/foo`);
3738
});
39+
40+
Deno.test("normalizeConfig - build.outDir", () => {
41+
const cwd = Deno.cwd().replaceAll("\\", "/");
42+
const outDir = (options: FreshConfig) =>
43+
normalizeConfig(options).build.outDir;
44+
45+
// Default outDir
46+
expect(outDir({ root: "./src" })).toEqual(`${cwd}/src/_fresh`);
47+
expect(outDir({ root: "/src" })).toEqual("/src/_fresh");
48+
expect(outDir({ root: "file:///src" })).toEqual("/src/_fresh");
49+
50+
// Relative outDir
51+
expect(outDir({ root: "/src", build: { outDir: "dist" } })).toEqual(
52+
"/src/dist",
53+
);
54+
expect(outDir({ root: "/src", build: { outDir: "./dist" } })).toEqual(
55+
"/src/dist",
56+
);
57+
58+
// Absolute outDir
59+
expect(outDir({ root: "/src", build: { outDir: "/dist" } })).toEqual(
60+
"/dist",
61+
);
62+
expect(outDir({ root: "/src", build: { outDir: "/dist/fresh" } })).toEqual(
63+
"/dist/fresh",
64+
);
65+
expect(outDir({ root: "/src", build: { outDir: "file:///dist" } })).toEqual(
66+
"/dist",
67+
);
68+
});
69+
70+
Deno.test("normalizeConfig - staticDir", () => {
71+
const cwd = Deno.cwd().replaceAll("\\", "/");
72+
const staticDir = (options: FreshConfig) =>
73+
normalizeConfig(options).staticDir;
74+
75+
// Default staticDir
76+
expect(staticDir({ root: "./src" })).toEqual(`${cwd}/src/static`);
77+
expect(staticDir({ root: "/src" })).toEqual("/src/static");
78+
expect(staticDir({ root: "file:///src" })).toEqual("/src/static");
79+
80+
// Relative staticDir
81+
expect(staticDir({ root: "/src", staticDir: "public" })).toEqual(
82+
"/src/public",
83+
);
84+
expect(staticDir({ root: "/src", staticDir: "./public" })).toEqual(
85+
"/src/public",
86+
);
87+
88+
// Absolute staticDir
89+
expect(staticDir({ root: "/src", staticDir: "/public" })).toEqual(
90+
"/public",
91+
);
92+
expect(staticDir({ root: "/src", staticDir: "/public/assets" })).toEqual(
93+
"/public/assets",
94+
);
95+
expect(staticDir({ root: "/src", staticDir: "file:///public" })).toEqual(
96+
"/public",
97+
);
98+
});

0 commit comments

Comments
 (0)