Skip to content

Added Lamdera alternative implementation for HTML generation. #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: lamdera-next
Choose a base branch
from

Conversation

rupertlssmith
Copy link
Contributor

Added Lamdera alternative implementation for HTML generation only when building a Lamdera project, not regular Elm.

This means you can do lamdera make src/Main.elm with a Browser.sandbox/document/application and get an index.html output file that will run that application.

Without this change some Lamdera extra stuff gets put in the HTML for running a Lamdera Live application, and the application hangs on startup looking for a websocket connection to the backend.

The alternative implementation is wrapped like this:

Lamdera.alternativeImplementationWhen Lamdera.isLamdera_ (sandwich_ root moduleName javascript) $

So contingent on the Lamdera.isLamdera_ condition being true. This is true when the elm.json contains lamdera/core as a package, and there are Types.elm and Backend.elm files under src/.

Reason for this PR is the aim of making lamdera a drop-in replacement for Elm. Html generation is probably not used as much with Elm in production compared with generating an elm.json and a custom Html document, but worth consdering for completeness of providing a drop-in replacement for the make command.

@supermario
Copy link
Member

Thanks @rupertlssmith ! Didn't realise we'd broken the index.html behavior.

Would you be up for having a go at adding a test for this?

@rupertlssmith
Copy link
Contributor Author

Would you be up for having a go at adding a test for this?

Do you have an pointers or ideas on what a test should do? For example are there already tests that run in a headless browser that I could use as a starting point or some other approach?

@supermario
Copy link
Member

supermario commented Jun 25, 2025

I was thinking something very simple stupid; commit the output index.html that Elm makes, and then in the test assert that the Lamdera generation of index.html is equal to it.

We already have test/scenario-empty-elm-init Elm project which could have the Elm-generated index.html committed.

And there's a basic test in test/Test/Caching.hs that uses that project that should help as a starting point.

Looking at briefly I think that specific example is probably broken (as it relies on the official Elm compiler at an overly specific path) but hopefully it gives you the gist. But for this new test, we wouldn't need the Elm compiler just it's committed output to compare to. Unless you want to get fancy and future proof us by ensuring we've pulled down the latest Elm on the fly 😁

Lamdera.Compile has a few different helper variant configurations to invoke the equivalent of lamdera make directly from code with various flags. We'll need another variant with _output = Nothing to get the index.html I think, as IIRC all the existing ones emulate --output=/dev/null or --output=/some/file.js.

Feel free to start a thread on Discord if you want more realtime help, the rest of the team probably have a better timezone overlap to be more responsive :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants