feat(dev): Ignore common test files in routes and islands (#1039)

Co-authored-by: Marvin Hagemeister <marvinhagemeister50@gmail.com>
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Sebastian Gräßl
2023-09-11 16:05:55 +02:00
committed by GitHub
co-authored by Marvin Hagemeister Asher Gomez
parent 59fff11903
commit e01ef2d8a0
16 changed files with 142 additions and 5 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ export async function dev(
} else {
currentManifest = { islands: [], routes: [] };
}
const newManifest = await collect(dir);
const newManifest = await collect(dir, options);
Deno.env.set("FRSH_DEV_PREVIOUS_MANIFEST", JSON.stringify(newManifest));
const manifestChanged =
+10 -4
View File
@@ -1,7 +1,8 @@
import { gte, join, posix, relative, walk, WalkEntry } from "./deps.ts";
import { error } from "./error.ts";
import { FreshOptions } from "../server/mod.ts";
const MIN_DENO_VERSION = "1.31.0";
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
export function ensureMinDenoVersion() {
// Check that the minimum supported Deno version is being used.
@@ -23,6 +24,7 @@ export function ensureMinDenoVersion() {
async function collectDir(
dir: string,
callback: (entry: WalkEntry, dir: string) => void,
options: FreshOptions,
): Promise<void> {
// Check if provided path is a directory
try {
@@ -37,6 +39,7 @@ async function collectDir(
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
skip: [options?.router?.ignoreFilePattern || TEST_FILE_PATTERN],
});
for await (const entry of routesFolder) {
@@ -50,7 +53,10 @@ export interface Manifest {
}
const GROUP_REG = /[/\\\\]\((_[^/\\\\]+)\)[/\\\\]/;
export async function collect(directory: string): Promise<Manifest> {
export async function collect(
directory: string,
options: FreshOptions = {},
): Promise<Manifest> {
const filePaths = new Set<string>();
const routes: string[] = [];
@@ -78,11 +84,11 @@ export async function collect(directory: string): Promise<Manifest> {
}
filePaths.add(normalized);
routes.push(rel);
}),
}, options),
collectDir(join(directory, "./islands"), (entry, dir) => {
const rel = join("islands", relative(dir, entry.path));
islands.push(rel);
}),
}, options),
]);
routes.sort();
+9
View File
@@ -19,6 +19,15 @@ export interface RouterOptions {
* @default {false}
*/
trailingSlash?: boolean;
/**
* Configures the pattern of files to ignore in islands and routes.
*
* By default Fresh will ignore test files,
* for example files with a `.test.ts` or a `_test.ts` suffix.
*
* @default {/(?:[^/]*_|[^/]*\.|)test\.(?:ts|tsx|mts|js|mjs|jsx|)\/*$/}
*/
ignoreFilePattern?: RegExp;
}
export type RenderFunction = (
+5
View File
@@ -32,3 +32,8 @@ export {
export { defineConfig, type Preset } from "https://esm.sh/@twind/core@1.1.3";
export { default as presetTailwind } from "https://esm.sh/@twind/preset-tailwind@1.1.4";
export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts";
export {
dirname,
fromFileUrl,
join,
} from "https://deno.land/std@0.193.0/path/mod.ts";
+51
View File
@@ -0,0 +1,51 @@
import { collect } from "../src/dev/mod.ts";
import { assert, dirname, fromFileUrl, join } from "./deps.ts";
Deno.test({
name: "routes collect",
fn: async () => {
const { routes } = await collect(
join(dirname(fromFileUrl(import.meta.url)), "fixture"),
{},
);
assert(
!routes.includes("routes/not_found.test.ts") &&
!routes.includes("routes\\not_found.test.ts"),
);
assert(
!routes.includes("routes/_404_test.tsx") &&
!routes.includes("routes\\_404_test.tsx"),
);
assert(
!routes.includes("routes/islands/test_test.tsx") &&
!routes.includes("routes\\islands\\test_test.tsx"),
);
},
});
Deno.test({
name: "routes collect with custom pattern",
fn: async () => {
const { routes } = await collect(
join(
dirname(fromFileUrl(import.meta.url)),
"fixture_router_ignore_files",
),
{
router: {
ignoreFilePattern: /[\.|_]cy\.[t|j]s(x)?$/,
},
},
);
assert(
!routes.includes("routes/index.cy.ts") &&
!routes.includes("routes\\index.cy.ts"),
);
assert(
routes.includes("routes/index.tsx") ||
routes.includes("routes\\index.tsx"),
);
},
});
View File
View File
+3
View File
@@ -0,0 +1,3 @@
Deno.test("not really", () => {
return;
});
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.5",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.3.1"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
import options from "./options.ts";
await dev(import.meta.url, "./main.ts", options);
@@ -0,0 +1,15 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+11
View File
@@ -0,0 +1,11 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
import options from "./options.ts";
await start(routes, options);
@@ -0,0 +1,7 @@
import { FreshOptions } from "$fresh/server.ts";
export default {
router: {
ignoreFilePattern: /[\.|_]cy\.[t|j]s(x)?$/,
},
} as FreshOptions;
@@ -0,0 +1,9 @@
import { IS_BROWSER } from "$fresh/runtime.ts";
export default function Home() {
return (
<div>
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
</div>
);
}