Skip to content

Commit

Permalink
Make surface.init compatible with phx_new 1.7.14 (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoefmoraes authored Oct 1, 2024
1 parent 72c80e0 commit 0519ab0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ defmodule Mix.Tasks.Surface.Init.FilePatchers.Phoenix do
esbuild_patcher ->
esbuild_patcher
|> halt_if(&find_keyword(&1, [:catalogue]), :already_patched)
|> find_keyword_value([:default])
|> replace_code(
&"""
#{&1},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.JsHooks do
&FilePatchers.JS.add_import(&1, ~S[import Hooks from "./_hooks"]),
&FilePatchers.Text.replace_line_text(
&1,
~S[let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})],
~S[let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})]
~S[ params: {_csrf_token: csrfToken}],
~s[ hooks: Hooks,\n params: {_csrf_token: csrfToken}]
)
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/mix/tasks/surface/surface.init/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Mix.Tasks.Surface.Init.IntegrationTest do

alias Mix.Tasks.Surface.Init.FilePatchers

@phx_new_version "1.7.10"
@phx_new_version "1.7.14"

setup_all do
{template_status, template_project_folder} = build_test_project_template("surface_init_test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.CatalogueTest do
end

test "add catalogue entry if esbuild config has already been set" do
code = ~S"""
profile = Enum.random(["default", "surface_init_test"])

code = ~s"""
import Config
# Use Jason for JSON parsing in Phoenix
Expand All @@ -237,20 +239,20 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.CatalogueTest do
# Configure esbuild (the version is required)
config :esbuild,
version: "0.14.10",
default: [
#{profile}: [
args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets),
cd: Path.expand("../assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
]
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
import_config "\#{config_env()}.exs"
"""

{:patched, updated_code} = Patcher.patch_code(code, configure_catalogue_esbuild())

assert updated_code == ~S"""
assert updated_code == ~s"""
import Config
# Use Jason for JSON parsing in Phoenix
Expand All @@ -259,7 +261,7 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.CatalogueTest do
# Configure esbuild (the version is required)
config :esbuild,
version: "0.14.10",
default: [
#{profile}: [
args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets),
cd: Path.expand("../assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
Expand All @@ -272,7 +274,7 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.CatalogueTest do
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
import_config "\#{config_env()}.exs"
"""
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.JSHooksTest do
import topbar from "../vendor/topbar"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
let liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
params: {_csrf_token: csrfToken}
})
// connect if there are any LiveViews on the page
liveSocket.connect()
window.liveSocket = liveSocket
"""

{:patched, updated_code} = Patcher.patch_code(code, js_hooks())
{_patched, updated_code} = Patcher.patch_code(code, js_hooks())

assert updated_code == """
// We import the CSS which is extracted to its own file by esbuild.
Expand All @@ -33,7 +36,11 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.JSHooksTest do
import Hooks from "./_hooks"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})
let liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
hooks: Hooks,
params: {_csrf_token: csrfToken}
})
// connect if there are any LiveViews on the page
liveSocket.connect()
Expand All @@ -52,7 +59,11 @@ defmodule Mix.Tasks.Surface.Init.ProjectPatchers.JSHooksTest do
import Hooks from "./_hooks"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})
let liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
hooks: Hooks,
params: {_csrf_token: csrfToken}
})
// connect if there are any LiveViews on the page
liveSocket.connect()
Expand Down

0 comments on commit 0519ab0

Please sign in to comment.