LIFF ports for Elm

Hi, you cannot publish a ports module, but there is something of a workaround for that.

Suppose your module looks like this:

port module MyAwesomeLib exposing (somePort)

port somePort : String -> Cmd msg

You can change it instead to be:

module MyAwesomeLib exposing (SomePort)

type alias SomePort = String -> Cmd msg

Now anyone wanting to use your package needs to define the port themselves in their application. They will also need the corresponding javascript side code. You can make this easier for them by:

  1. Describe in the README exactly what ports they need to set up in the application, and what they need to be called. So tell them to put this in the application, along with the other instructions on how to use it:
    port somePort : MyAwesomeLib.SomePort
  1. Publish your javascript code on npm, so that it can easily be added as a dependency in package.json. OR if its just a very short snippet, you could put the javascript code in the README. There will need to be a short snippet in the README anyway, to show how to wire the port up:
    app.ports.somePort.subscribe(request => {
        callYourJSLib(request);
    });
2 Likes