diff --git a/src/dev/dev_command.ts b/src/dev/dev_command.ts index 222a2eb3..958b3b76 100644 --- a/src/dev/dev_command.ts +++ b/src/dev/dev_command.ts @@ -12,15 +12,13 @@ import { startFromContext } from "../server/boot.ts"; export async function dev( base: string, entrypoint: string, - options: FreshOptions = {}, + options?: FreshOptions, ) { ensureMinDenoVersion(); // Run update check in background updateCheck(DAY).catch(() => {}); - entrypoint = new URL(entrypoint, base).href; - const dir = dirname(fromFileUrl(base)); let currentManifest: Manifest; @@ -54,13 +52,15 @@ export async function dev( if (isBuild) { // Ensure that build dir is empty await fs.emptyDir(outDir); - await build(join(dir, "fresh.gen.ts"), options); + await build(join(dir, "fresh.gen.ts"), options ?? {}); } else if (options) { await startFromContext(ctx, options); } else { // Legacy entry point: Back then `dev.ts` would call `main.ts` but // this causes duplicate plugin instantiation if both `dev.ts` and // `main.ts` instantiate plugins. + Deno.env.set("__FRSH_LEGACY_DEV", "true"); + entrypoint = new URL(entrypoint, base).href; await import(entrypoint); } } diff --git a/src/server/context.ts b/src/server/context.ts index d2eff40c..6e593b73 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -165,7 +165,11 @@ export class ServerContext { // Restore snapshot if available let snapshot: BuildSnapshot | null = null; // Load from snapshot if not explicitly requested not to - const loadFromSnapshot = !opts.skipSnapshot; + let loadFromSnapshot = !opts.skipSnapshot; + // We still need to check for a legacy entry point :S + if (Deno.env.has("__FRSH_LEGACY_DEV")) { + loadFromSnapshot = false; + } if (loadFromSnapshot) { const snapshotDirPath = join( dirname(fromFileUrl(manifest.baseUrl)), diff --git a/tests/dev_command_test.ts b/tests/dev_command_test.ts new file mode 100644 index 00000000..5b7132f0 --- /dev/null +++ b/tests/dev_command_test.ts @@ -0,0 +1,27 @@ +import { waitForStyle, withPageName } from "$fresh/tests/test_utils.ts"; + +Deno.test({ + name: "dev_command config", + async fn() { + await withPageName( + "./tests/fixture_dev_config/main.ts", + async (page, address) => { + await page.goto(`${address}`); + await waitForStyle(page, "h1", "color", "rgb(220, 38, 38)"); + }, + ); + }, +}); + +Deno.test({ + name: "dev_command legacy", + async fn() { + await withPageName( + "./tests/fixture_dev_legacy/main.ts", + async (page, address) => { + await page.goto(`${address}`); + await waitForStyle(page, "h1", "color", "rgb(220, 38, 38)"); + }, + ); + }, +}); diff --git a/tests/fixture_dev_config/dev.ts b/tests/fixture_dev_config/dev.ts new file mode 100644 index 00000000..1b53e2e7 --- /dev/null +++ b/tests/fixture_dev_config/dev.ts @@ -0,0 +1,4 @@ +import { dev } from "$fresh/src/dev/dev_command.ts"; +import config from "./fresh.config.ts"; + +await dev(import.meta.url, "./main.ts", config); diff --git a/tests/fixture_dev_config/fresh.config.ts b/tests/fixture_dev_config/fresh.config.ts new file mode 100644 index 00000000..889a6862 --- /dev/null +++ b/tests/fixture_dev_config/fresh.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "$fresh/server.ts"; +import twind from "$fresh/plugins/twind.ts"; + +export default defineConfig({ + plugins: [twind({ + selfURL: import.meta.url, + })], +}); diff --git a/tests/fixture_dev_config/fresh.gen.ts b/tests/fixture_dev_config/fresh.gen.ts new file mode 100644 index 00000000..b321c0e0 --- /dev/null +++ b/tests/fixture_dev_config/fresh.gen.ts @@ -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; diff --git a/tests/fixture_dev_config/main.ts b/tests/fixture_dev_config/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_dev_config/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_dev_config/routes/index.tsx b/tests/fixture_dev_config/routes/index.tsx new file mode 100644 index 00000000..d549f8ae --- /dev/null +++ b/tests/fixture_dev_config/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

check color

; +} diff --git a/tests/fixture_dev_legacy/dev.ts b/tests/fixture_dev_legacy/dev.ts new file mode 100644 index 00000000..dfd26db6 --- /dev/null +++ b/tests/fixture_dev_legacy/dev.ts @@ -0,0 +1,3 @@ +import { dev } from "$fresh/src/dev/dev_command.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_dev_legacy/fresh.gen.ts b/tests/fixture_dev_legacy/fresh.gen.ts new file mode 100644 index 00000000..b321c0e0 --- /dev/null +++ b/tests/fixture_dev_legacy/fresh.gen.ts @@ -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; diff --git a/tests/fixture_dev_legacy/main.ts b/tests/fixture_dev_legacy/main.ts new file mode 100644 index 00000000..5638b1c2 --- /dev/null +++ b/tests/fixture_dev_legacy/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import twind from "$fresh/plugins/twind.ts"; + +await start(manifest, { plugins: [twind({ selfURL: import.meta.url })] }); diff --git a/tests/fixture_dev_legacy/routes/index.tsx b/tests/fixture_dev_legacy/routes/index.tsx new file mode 100644 index 00000000..d549f8ae --- /dev/null +++ b/tests/fixture_dev_legacy/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

check color

; +} diff --git a/tests/main_test.ts b/tests/main_test.ts index 20f5ae42..2b01f59d 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -59,7 +59,7 @@ Deno.test("/props/123 page prerender", async () => { assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); const doc = parseHtml(body); - const data = JSON.parse(doc.querySelector("body > div").textContent); + const data = JSON.parse(doc.querySelector("body > div")!.textContent!); assertEquals(data, { "params": { "id": "123" }, @@ -613,7 +613,7 @@ Deno.test({ const body = await resp.text(); const doc = parseHtml(body); - assertEquals(JSON.parse(doc.querySelector("pre").textContent), { + assertEquals(JSON.parse(doc.querySelector("pre")!.textContent!), { handler1: "it works", handler2: "it works", handler3: "it works", diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 1e25bcee..c29d67b5 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -10,8 +10,9 @@ import { TextLineStream, } from "./deps.ts"; -export function parseHtml(input: string) { - return new DOMParser().parseFromString(input, "text/html"); +export function parseHtml(input: string): Document { + // deno-lint-ignore no-explicit-any + return new DOMParser().parseFromString(input, "text/html") as any; } export async function startFreshServer(options: Deno.CommandOptions) { @@ -87,7 +88,6 @@ export const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; function prettyDom(doc: Document) { let out = colors.dim(`\n`); - console.log(out); const node = doc.documentElement; out += _printDomNode(node, 0); @@ -269,6 +269,41 @@ export async function waitForText( ); } +export async function waitForStyle( + page: Page, + selector: string, + name: keyof CSSStyleDeclaration, + value: string, +) { + await page.waitForSelector(selector); + + const start = Date.now(); + let now = start; + let found = false; + while (now < start + 2000) { + found = await page.evaluate( + (s, n, v) => { + const el = document.querySelector(s); + if (!el) return false; + return window.getComputedStyle(el)[n] === v; + }, + selector, + name, + value, + ); + + if (found) break; + + await delay(200); + now = Date.now(); + } + + if (!found) { + console.log(prettyDom(parseHtml(await page.content()))); + throw new Error(`Could not find style ${String(name)}: ${value}`); + } +} + async function spawnServer( options: Deno.CommandOptions, expectErrors = false,