Using custom HTML with elm reactor, or another dev server, in 0.19

折月煮酒 提交于 2019-12-12 19:22:30

问题


As this answer shows it's possible in Elm 0.18 to run elm reactor with a custom HTML file if this snippet is included in the HTML file:

<script src="/_compile/src/Main.elm"></script>
<script>
  runElmProgram();
</script>

In 0.19, however, it doesn't seem like the _compile directory exists or is served anymore. Is there another way to accomplish the same, preferably with elm reactor so as to be able to leverage existing IDE support etc., but I'm interested in any kind of "dev server" solution.


回答1:


It's possible to use elm-live, which needs to be invoked with a bit more ceremony. It defaults to serving index.html but can be configured to serve a different html page using the command line option -s, --start-page [STARTPAGE] (Thank you for this tip, @kaskelotti)

The equivalent snippet needed in index.html to start your elm program is:

<script src="build/elm.js"></script>
<div id="elm"></div>
<script>
  var app = Elm.Main.init({
    node: document.getElementById('elm')
  });
</script>

and to start it run the following command in a terminal:

elm-live src/Main.elm -- --debug --output=build/elm.js

The script path in the HTML file must be the same as the one passed as output on the command line.

Unlike elm reactor, elm-live will not display compiler errors in the browser, but instead prints it to the terminal, and will continue to serve the last successfully compiled output.

It also randomly hangs and crashes...



来源:https://stackoverflow.com/questions/52702961/using-custom-html-with-elm-reactor-or-another-dev-server-in-0-19

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!