denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.26k stars 629 forks source link

main.ts not triggered by running dev.ts #1763

Closed cknight closed 11 months ago

cknight commented 1 year ago

I'm assuming this is a bug (either in my code or Fresh) and not my misunderstanding but if I run deno run -A --unstable --watch=static/,routes/ dev.ts , my console.log statements in main.ts are not outputting. Here's the code (mostly untouched from the project init):

dev.ts

#!/usr/bin/env -S deno run -A --watch=static/,routes/

import dev from "$fresh/dev.ts";
import config from "./fresh.config.ts";

import "$std/dotenv/load.ts";

await dev(import.meta.url, "./main.ts", config);

main.ts

/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />

import "$std/dotenv/load.ts";

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import config from "./fresh.config.ts";

console.log("Starting main...");

await start(manifest, config);

console logs

chris@chris-desktop:~/dev/deno/kv_enqueue_test$ deno run -A --unstable --watch=static/,routes/ dev.ts
Watcher Process started.
The manifest has been generated for 4 routes and 1 islands.

 🍋 Fresh ready 
    Local: http://localhost:8000/

I expected to see Starting main.... My use case is trying to register a KV queue listener in the main.ts for use in both dev and prod. I assumed that main.ts would always be executed.

cknight commented 1 year ago

Ah, I see reading the dev_command.ts code that main.ts is not executed by dev.ts if a plugin (such as tailwind in my case) in passed in as an option. In which case, where do I put app initialization code which needs executed once on server startup and can be used by both dev and prod development without duplication?

aLemonFox commented 1 year ago

Yeah this used to work in 1.4.2, but seems to have changed in 1.4.3 causing main.ts to no longer run. edit: seems this has been fixed but not yet released :)

deer commented 11 months ago

@cknight, are you referring to something like https://fresh.deno.dev/docs/examples/init-the-server? I think this needs to get changed to take into consideration the change described by #1735.

I believe the proper place to put this shared initialization code is now in fresh.config.ts. With a clean project I make the change...

diff --git a/fresh.config.ts b/fresh.config.ts
index 548e16a..b636973 100644
--- a/fresh.config.ts
+++ b/fresh.config.ts
@@ -2,6 +2,8 @@ import { defineConfig } from "$fresh/server.ts";
 import twindPlugin from "$fresh/plugins/twind.ts";
 import twindConfig from "./twind.config.ts";

+console.log("hello from config");
+
 export default defineConfig({
   plugins: [twindPlugin(twindConfig)],
 });

And when I run deno task start I see:

Task start deno run -A --watch=static/,routes/ dev.ts
Watcher Process started.
hello from config
The manifest has been generated for 5 routes and 1 islands.

 🍋 Fresh ready
    Local: http://localhost:8000/

And then I do deno task build:

Task build deno run -A dev.ts build
hello from config
The manifest has been generated for 5 routes and 1 islands.
Assets written to: /Users/reed/code/temp/1751/_fresh

And finally deno task preview (which invokes main.ts):

Task preview deno run -A main.ts
hello from config
Using snapshot found at /Users/reed/code/temp/1751/_fresh

 🍋 Fresh ready
    Local: http://localhost:8000/

To me this seems to prove that fresh.config.ts is the right spot for common code. @marvinhagemeister, if you agree I can create a PR to modify the documentation. It looks like it's currently out of date (hence this issue).

cknight commented 11 months ago

Great that there is now a documented approach. Apologies for the late feedback, but, semantically speaking, fresh.config.ts feels like somewhere for Fresh related configuration to go rather than non-Fresh application initialization code? I'm not all that fussed however, just a comment that for me it's non-intuitive.