So first in my ./web/elm/
directory I made a file of ./web/elm/ExampleElmApp.elm
, and in that file I have:
module ExampleElmApp exposing (..)
import Html
main : Html.Html msg
main = Html.text "Consider this text a promise of future beauty"
Then in my ./brunch-config.js
file I added the new root App by altering the elmBrunch
part of the plugins
section like:
// Configure your plugins
plugins: {
// snip other plugins that I have
elmBrunch: { // I included my entire elmBrunch section with two Apps instead of my larger amount for brevity
elmFolder: '.',
mainModules: [
'web/elm/MessengerApp.elm',
'web/elm/ExampleElmApp.elm' // I added this line to add the new App, with a comma on the previous line of course
],
// If these next two are specified, all mainModules will be compiled to a single file (optional and merged with outputFolder)
outputFolder: 'web/static/js',
outputFile: 'elm.js',
makeParameters: ['--warn']
}
},
And upon saving this my server printed:
Elm compile: web/elm/MessengerApp.elm web/elm/ExampleElmApp.elm, in ., to web\static\js\elm.js
29 Jul 15:59:44 - info: compiling
29 Jul 15:59:48 - info: compiled 2 files and 193 cached into app.js in 12.3 sec
It takes so long because I have SO many files now, still blazing fast compared to when I tried webpackā¦
But now I go to my app.js and add it to my appropriate starting code. I have a section for things that do not require a phoenix active socket (which this one does not), and a section that does (which does not start those Apps until it is active, but that is just because I am too lazy to check if the socket is open before I start using it in Elm). So the relevant bits with the irrelevant omitted of my app.js
:
import $ from "jquery"
import Elm from '../elm'
$(document).ready(() => { // Standard jQuery function run when the page finishes loading
let app = Elm.ExampleElmApp.embed(document.querySelector('#my-elm-example'))
// I have a lot of Helper libraries that make Elm a *LOT* easier to use that I attach via Elm Ports here with app.
}
And then in, oh letās say my ./web/templates/layout/app.html.eex' file I add this to the
` section:
<div id="my-elm-example"></div>
And then I run it and I see at the top of every single one of my server pages:
Consider this text a promise of future beauty
Aaaand then I run git reset --hard
to complete undo my examples changes so I can keep working. ^.^
EDIT: Do note, there is a bit of a race condition between elm compiling and brunch recognizing it so if that happens you will not see elm changes until the next compile, so I am in the habit of saving, waiting until the compiled appears in 10 seconds or so, then saving again. I should probably report this to elm-brunchā¦
EDIT2: It also really is best to compile all Elm Apps into a single elm.js file so that you do not duplicate the Elm standard library code inside each package and so Brunch/Babel can properly trim out what is not needed.
EDIT3: I use the elm-brunch plugin for brunch for handling it, which you just add to the devDependencies section of your NPM package.json file:
"elm-brunch": "~0.6.0",