I have written a little webapp with Elm which worked out very nicely. The elm code compiles to a single index.html file which is exactly what I want. However in this index.html file the title metadata field is set as
<title>Main</title>
Is there a way to instruct the Elm compiler to set it to another value? (Something like `MyApp V 1.2.3 – so that people inspecting the index.html file see, “aha, that’s what this file is.”)
You can use Browser#document instead of Browser#sandbox or Browser#element which allow to control the title of the page in Elm code.
You can use a port that will set the title javascript side and be called in the init function. This requires to use at least Browser#element and not Browser#sandbox. This also requires on of the bellow options, making this one a bit overkill
You can ask the elm compiler to generate JS code instead of HTML file (--output=elm.js) and craft your own index.html page which will have the propoer title and a script section to launch elm code
Same spirit as above but less code to write : patch the index.html generated by Elm using, for example sed command : sed -i -e 's!<title>Main</title>!<title>My Own Title</title>' index.html
Tell us if you need any complementary explanation on any of these options and which solution do you come finally with.
I think I go with option 4: Rewriting the HTML file with a command line tool.
One questions here: I use elm-live. Is there a way to hook the sed command into the elm-live watcher after it has generated the index.html? Or is another reload tool better suited for this than elm-live?
I’m not certain about outputting html. I’m not sure with either elm-live or elm-watch if you’d want to use sed with watch though. Saying you want to use sed, I’m guessing you’re only setting the title once on your final build. With that in mind, you would only need to do this for your production build in which case you can do elm make src/Main.elm --optimize | sed .... Is there a reason you’re needing to change the title during development?
elm-watch cannot generate an HTML file. The assumption is that you want to create it yourself. In my opinion, that elm can output HTML files is just a convenience for beginners, so they don’t need to learn too much at once. elm-watch is mostly geared towards people who have grown out of plain elm and are a little bit more advanced. (Though I still tried to keep things simple and friendly.)