Skip to content

Instantly share code, notes, and snippets.

@campezzi
Last active September 19, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save campezzi/9d3cdf19076a4602180e6ea4d340265e to your computer and use it in GitHub Desktop.
Save campezzi/9d3cdf19076a4602180e6ea4d340265e to your computer and use it in GitHub Desktop.
Phoenix + Elm setup

Phoenix + Elm Setup

Important

  1. This was tested on Phoenix 1.2.0;
  2. Make sure you have npm 3.x or above - older versions will spit out tons of Babel-related errors.

Instructions

  1. Create a Phoenix project: mix phoenix.new my_awesome_project
  2. Install dependencies when prompted, then navigate to your project folder
  3. Install the elm-brunch package: npm install elm-brunch --save-dev
  4. Edit brunch-config.js adding the elmBrunch configuration in the plugins key:
plugins: {
  // ...
  elmBrunch: {
    elmFolder: "web/static/elm",
    mainModules: ["Main.elm"],
    outputFolder: "../vendor"
  }
}
  1. Create the folder web/static/elm and navigate to it
  2. Install Elm's Html package: elm package install elm-lang/html
  3. In web/static/elm, create the Main.elm file (this will be your app's entry point):
module Main exposing (..)

import Html exposing (..)


main =
    text "Hello, World!"
  1. Create a container for your Elm application in the layout (usually web/templates/layout/app.html.eex):
<div id="main"></div>
  1. Embed the Elm application in that container by editing web/static/js/app.js:
const elmContainer = document.querySelector("#main");
const elmApp = Elm.Main.embed(elmContainer);
  1. From your project's root folder, start the Phoenix server: mix phoenix.server
  2. Done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment