Closed cknight closed 11 months 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?
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 :)
@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).
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.
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
main.ts
console logs
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.