tauling

tauling

Livereload stops working when importing Elm bundle

Hi,

I’m using phoenix 1.7 and trying to integrate my Elm front end with live reloading in the development setup. Starting from a fresh a phoenix project, I added import "./elm.js" to my app.js. In addition, I have a build.js file that bundles my Elm code using esbuild.

Whenever I modify my Elm code, I see the building of my Elm code taking place in the console. But the live reloading stops working if I import my elm.js in app.js. I still see the rebuilding taking place in the console, but the browser just doesn’t reload.

Would be great if someone has an idea on what I’m doing wrong here.

app.js

import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
//import topbar from "../vendor/topbar"
// Import the compiled Elm app:
import "./elm.js"

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})

// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())

// connect if there are any LiveViews on the page
liveSocket.connect()

// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000)  // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket

build.js

const esbuild = require('esbuild');
const ElmPlugin = require('esbuild-plugin-elm');

async function watch() {
  const ctx = await esbuild.context({
    entryPoints: ['src/index.js'],
    bundle: true,
    outfile: '../js/elm.js',
    plugins: [
      ElmPlugin({
        debug: true,
        clearOnWatch: true
      }),
    ],
  }).catch(_e => process.exit(1))
  await ctx.watch()

  console.log("done")
}
watch()

dev.exs watchers

node: ["./build.js", cd: Path.expand("../assets/elm", __DIR__)]

Best regards
Max

First Post!

codeanpeace

codeanpeace

I’d give the approach outlined in this Phoenix Elm Starter repo a try. At a glance, the build.js in particular takes a different approach.

esbuild.build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outdir: '../js',
  watch: process.argv.includes('--watch'),
  plugins: [
    ElmPlugin({
      debug: true,
      clearOnWatch: true,
    }),
  ],
}).catch(_e => process.exit(1))

Last Post!

tauling

tauling

good point, I actually started with this exact starter repo, which currently doesn’t work properly due to the fact that esbuild 0.17.0 doesn’t support a watch flag anymore

my build.js uses an updated configuration to work with esbuild 0.17.0, which turns out be working fine

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement