Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Journey of Upgrading an App to Phoenix 1.6

Wu Qing
September 16, 2021

Journey of Upgrading an App to Phoenix 1.6

I'm really excited about the newly released Phoenix 1.6 because it provides the option to replace Webpack with esbuild, so that we no longer need Node for asset building.
Over the weekend, I managed to upgrade my little side project to Phoenix 1.6. In this talk I shared my journey.

Presented at Elixir Australia September 2021 Meetup: https://www.meetup.com/elixir-sydney/events/280659594

Recording video available at https://www.youtube.com/watch?v=NR_Jk3yUEqc

Wu Qing

September 16, 2021
Tweet

More Decks by Wu Qing

Other Decks in Programming

Transcript

  1. Update dependencies def deps do [ {:phoenix, "~> 1.6.0-rc.0", override:

    true}, {:phoenix_html, "~> 3.0"}, {:phoenix_live_view, "~> 0.16.0"}, {:phoenix_live_dashboard, "~> 0.5"}, {:telemetry_metrics, "~> 0.6"}, {:telemetry_poller, "~> 0.5"}, ... ] end
  2. Phoenix asset pipeline review • All static assets are served

    from priv/static • Javascript: assets/js -> priv/static/js • CSS: assets/css -> priv/static/css • Images and other assets: assets/static -> priv/static
  3. Remove webpack config and related node files $ cd assets

    $ rm webpack.config.js package.json package-lock.json .babelrc $ rm -rf node_modules
  4. Add esbuild in deps def deps do [ ... {:esbuild,

    "~> 0.2", runtime: Mix.env() == :dev}, ] end
  5. Configure esbuild # config/config.exs config :esbuild, version: "0.12.18", default: [

    args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets), cd: Path.expand("../assets", __DIR__), env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)} ]
  6. Update watcher in endpoint config # config/dev.exs config :your_web_app, YourWebApp.Endpoint,

    ..., watchers: [ esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]} ]
  7. Move images and other assets • mv assets/static/* priv/static •

    Stop ignoring priv/static in VCS • Ignore priv/static/assets in VCS José's answer on Reddit
  8. Add assets.deploy mix alias defp aliases do [ ..., "assets.deploy":

    ["esbuild default --minify", "phx.digest"] ] end
  9. Update layouts # In `app.html.eex` or `root.html.eex`, update Routes.static_path(@conn, "/js/app.js")

    Routes.static_path(@conn, "/css/app.css") # to Routes.static_path(@conn, "/assets/app.js") Routes.static_path(@conn, "/assets/app.css")
  10. Update Plug.Static configuration plug Plug.Static, at: "/", from: :my_app, gzip:

    false, only: ~w(assets fonts images favicon.ico robots.txt)
  11. HEEx template engine • Make sure phoenix_live_view is in deps

    • Rename .html.eex and .html.leex templates to .html.heex • Update elixir expressions inside tags
  12. Update Elixir expressions inside tags # EEx expression <a href="/prefix/<%=

    @item.text %>"> # this won't work <a href="/prefix/{@item.text}">
  13. Update Elixir expressions inside tags # EEx expression <a href="/prefix/<%=

    @item.text %>"> # becomes <a href={"/prefix/#{@item.text}"}>
  14. Deploy to Gigalixir • Remove phoenix_static_buildpack.config • add in elixir_buildpack.config

    hook_post_compile="eval mix assets.deploy && rm -f _build/esbuild"
  15. References • Phoenix 1.6.0-rc.0 Released • Phoenix 1.5.x to 1.6

    upgrade instructions • Phoenix + esbuild + yarn + postcss + tailwindcss + cpx2 • Deploying on Gigalixir • Rubik's Cube Algorithms Trainer