|
| 1 | +if Code.ensure_loaded?(Igniter) do |
| 2 | + defmodule Phoenix.LiveView.Igniter.UpgradeTo1_1 do |
| 3 | + @moduledoc false |
| 4 | + |
| 5 | + def run(igniter, _opts) do |
| 6 | + igniter |
| 7 | + |> Igniter.Project.Deps.add_dep({:lazy_html, ">= 0.0.0", only: :test}, on_exists: :skip) |
| 8 | + |> Igniter.Project.MixProject.update(:project, [:compilers], fn |
| 9 | + nil -> |
| 10 | + {:ok, {:code, "[:phoenix_live_view] ++ Mix.compilers()"}} |
| 11 | + |
| 12 | + zipper -> |
| 13 | + cond do |
| 14 | + Igniter.Code.List.list?(zipper) and |
| 15 | + !Igniter.Code.List.find_list_item_index(zipper, &(&1 == :phoenix_live_view)) -> |
| 16 | + Igniter.Code.List.prepend_to_list(zipper, :phoenix_live_view) |
| 17 | + |
| 18 | + expected_compilers?(zipper) -> |
| 19 | + {:ok, zipper} |
| 20 | + |
| 21 | + true -> |
| 22 | + {:warning, |
| 23 | + """ |
| 24 | + Failed to automatically configure compilers. Please add the following code to the project section of your mix.exs: |
| 25 | +
|
| 26 | + compilers: [:phoenix_live_view] ++ Mix.compilers() |
| 27 | + """} |
| 28 | + end |
| 29 | + end) |
| 30 | + |> maybe_update_reloadable_compilers() |
| 31 | + |> maybe_update_esbuild_config() |
| 32 | + end |
| 33 | + |
| 34 | + defp maybe_update_reloadable_compilers(igniter) do |
| 35 | + endpoint_mod = Igniter.Libs.Phoenix.web_module(igniter) |> Module.concat(Endpoint) |
| 36 | + app_name = Igniter.Project.Application.app_name(igniter) |
| 37 | + |
| 38 | + warning = """ |
| 39 | + You have `:reloadable_compilers` configured on your dev endpoint in config/dev.exs. |
| 40 | +
|
| 41 | + Ensure that `:phoenix_live_view` is set in there as the first entry! |
| 42 | +
|
| 43 | + config :#{app_name}, #{inspect(endpoint_mod)}, |
| 44 | + reloadable_compilers: [:phoenix_live_view, :elixir, :app] |
| 45 | + """ |
| 46 | + |
| 47 | + if Igniter.Project.Config.configures_key?(igniter, "dev.exs", app_name, [ |
| 48 | + endpoint_mod, |
| 49 | + :reloadable_compilers |
| 50 | + ]) do |
| 51 | + Igniter.Project.Config.configure( |
| 52 | + igniter, |
| 53 | + "dev.exs", |
| 54 | + app_name, |
| 55 | + [endpoint_mod, :reloadable_compilers], |
| 56 | + nil, |
| 57 | + updater: fn zipper -> |
| 58 | + if Igniter.Code.List.list?(zipper) do |
| 59 | + index = |
| 60 | + Igniter.Code.List.find_list_item_index(zipper, fn zipper -> |
| 61 | + case Igniter.Code.Common.expand_literal(zipper) do |
| 62 | + {:ok, :phoenix_live_view} -> true |
| 63 | + _ -> false |
| 64 | + end |
| 65 | + end) |
| 66 | + |
| 67 | + cond do |
| 68 | + index == nil -> |
| 69 | + Igniter.Code.List.prepend_to_list(zipper, :phoenix_live_view) |
| 70 | + |
| 71 | + index == 0 -> |
| 72 | + {:ok, zipper} |
| 73 | + |
| 74 | + index > 0 -> |
| 75 | + zipper |
| 76 | + |> Igniter.Code.List.remove_index(index) |
| 77 | + |> case do |
| 78 | + {:ok, zipper} -> |
| 79 | + Igniter.Code.List.prepend_to_list(zipper, :phoenix_live_view) |
| 80 | + |
| 81 | + :error -> |
| 82 | + {:warning, warning} |
| 83 | + end |
| 84 | + end |
| 85 | + else |
| 86 | + {:warning, warning} |
| 87 | + end |
| 88 | + end |
| 89 | + ) |
| 90 | + else |
| 91 | + igniter |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + defp expected_compilers?(zipper) do |
| 96 | + Igniter.Code.Function.function_call?(zipper, {Kernel, :++}) && |
| 97 | + Igniter.Code.Function.argument_equals?(zipper, 0, [:phoenix_live_view]) && |
| 98 | + Igniter.Code.Function.argument_matches_predicate?(zipper, 1, fn zipper -> |
| 99 | + Igniter.Code.Function.function_call?(zipper, {Mix, :compilers}) |
| 100 | + end) |
| 101 | + end |
| 102 | + |
| 103 | + defp maybe_update_esbuild_config(igniter) do |
| 104 | + if igniter.args.options[:yes] || |
| 105 | + Igniter.Util.IO.yes?( |
| 106 | + "Do you want to update your esbuild configuration for colocated hooks?" |
| 107 | + ) do |
| 108 | + app_name = Igniter.Project.Application.app_name(igniter) |
| 109 | + |
| 110 | + warning = |
| 111 | + """ |
| 112 | + Failed to update esbuild configuration for colocated hooks. Please manually: |
| 113 | +
|
| 114 | + 1. append `--alias:@=.` to the `args` list |
| 115 | + 2. configure `NODE_PATH` to be a list including `Mix.Project.build_path()`: |
| 116 | +
|
| 117 | + config :esbuild, |
| 118 | + #{app_name}: [ |
| 119 | + args: |
| 120 | + ~w(js/app.js --bundle --target=es2022 --outdir=../priv/static/assets/js --external:/fonts/* --external:/images/* --alias:@=.), |
| 121 | + cd: "...", |
| 122 | + env: %{"NODE_PATH" => [Path.expand("../deps", __DIR__), Mix.Project.build_path()]} |
| 123 | + ] |
| 124 | + """ |
| 125 | + |
| 126 | + if Igniter.Project.Config.configures_key?(igniter, "config.exs", :esbuild, app_name) do |
| 127 | + config_exs_vsn = Rewrite.Source.version(igniter.rewrite.sources["config/config.exs"]) |
| 128 | + |
| 129 | + igniter = |
| 130 | + Igniter.Project.Config.configure( |
| 131 | + igniter, |
| 132 | + "config.exs", |
| 133 | + :esbuild, |
| 134 | + app_name, |
| 135 | + nil, |
| 136 | + updater: fn zipper -> |
| 137 | + if Igniter.Code.Keyword.keyword_has_path?(zipper, [:args]) and |
| 138 | + Igniter.Code.Keyword.keyword_has_path?(zipper, [:env]) do |
| 139 | + with {:ok, zipper} <- update_esbuild_args(zipper, warning), |
| 140 | + {:ok, zipper} <- update_esbuild_env(zipper) do |
| 141 | + {:ok, zipper} |
| 142 | + end |
| 143 | + else |
| 144 | + {:warning, warning} |
| 145 | + end |
| 146 | + end |
| 147 | + ) |
| 148 | + |
| 149 | + if config_exs_vsn == |
| 150 | + Rewrite.Source.version(igniter.rewrite.sources["config/config.exs"]) do |
| 151 | + igniter |
| 152 | + else |
| 153 | + igniter |
| 154 | + |> Igniter.add_notice(""" |
| 155 | + Final step for colocated hooks: |
| 156 | +
|
| 157 | + Add an import to your `app.js` and configure the hooks option of the LiveSocket: |
| 158 | +
|
| 159 | + ... |
| 160 | + import {LiveSocket} from "phoenix_live_view" |
| 161 | + + import {hooks as colocatedHooks} from "phoenix-colocated/#{app_name}" |
| 162 | + import topbar from "../vendor/topbar" |
| 163 | + ... |
| 164 | + const liveSocket = new LiveSocket("/live", Socket, { |
| 165 | + longPollFallbackMs: 2500, |
| 166 | + params: {_csrf_token: csrfToken}, |
| 167 | + + hooks: {...colocatedHooks} |
| 168 | + }) |
| 169 | +
|
| 170 | + """) |
| 171 | + end |
| 172 | + else |
| 173 | + igniter |
| 174 | + end |
| 175 | + else |
| 176 | + igniter |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + defp update_esbuild_args(zipper, warning) do |
| 181 | + Igniter.Code.Keyword.put_in_keyword(zipper, [:args], nil, fn zipper -> |
| 182 | + if Igniter.Code.List.list?(zipper) do |
| 183 | + Igniter.Code.List.append_new_to_list(zipper, "--alias:@=.") |
| 184 | + else |
| 185 | + # ~w() |
| 186 | + case zipper.node do |
| 187 | + {:sigil_w, _meta, [{:<<>>, _str_meta, [str]}, []]} -> |
| 188 | + if str =~ "--alias:@=." do |
| 189 | + {:ok, zipper} |
| 190 | + else |
| 191 | + {:ok, |
| 192 | + Igniter.Code.Common.replace_code( |
| 193 | + zipper, |
| 194 | + ~s[~w(#{str <> " --alias:@=."})] |
| 195 | + )} |
| 196 | + end |
| 197 | + |
| 198 | + _ -> |
| 199 | + {:warning, warning} |
| 200 | + end |
| 201 | + end |
| 202 | + end) |
| 203 | + end |
| 204 | + |
| 205 | + defp update_esbuild_env(zipper) do |
| 206 | + Igniter.Code.Keyword.put_in_keyword( |
| 207 | + zipper, |
| 208 | + [:env], |
| 209 | + # we already checked that env is configured |
| 210 | + nil, |
| 211 | + fn zipper -> |
| 212 | + Igniter.Code.Map.put_in_map( |
| 213 | + zipper, |
| 214 | + ["NODE_PATH"], |
| 215 | + ~s<"[Path.expand("../deps", __DIR__), Mix.Project.build_path()])>, |
| 216 | + fn zipper -> |
| 217 | + if Igniter.Code.List.list?(zipper) do |
| 218 | + index = |
| 219 | + Igniter.Code.List.find_list_item_index(zipper, fn zipper -> |
| 220 | + if Igniter.Code.Function.function_call?( |
| 221 | + zipper, |
| 222 | + {Mix.Project, :build_path}, |
| 223 | + 0 |
| 224 | + ) do |
| 225 | + true |
| 226 | + end |
| 227 | + end) |
| 228 | + |
| 229 | + if index do |
| 230 | + {:ok, zipper} |
| 231 | + else |
| 232 | + Igniter.Code.List.append_to_list(zipper, {:code, "Mix.Project.build_path()"}) |
| 233 | + end |
| 234 | + else |
| 235 | + # If NODE_PATH is not a list, convert it to a list with the original value and Mix.Project.build_path() |
| 236 | + zipper |
| 237 | + |> Igniter.Code.Common.replace_code("[Mix.Project.build_path()]") |
| 238 | + |> Igniter.Code.List.prepend_to_list(zipper.node) |
| 239 | + end |
| 240 | + end |
| 241 | + ) |
| 242 | + end |
| 243 | + ) |
| 244 | + end |
| 245 | + end |
| 246 | +end |
0 commit comments