Skip to content

Commit 23b2e54

Browse files
authored
write empty manifest when no colocated js is used (#3845)
1 parent 9623458 commit 23b2e54

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

lib/phoenix_live_view/colocated_js.ex

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ defmodule Phoenix.LiveView.ColocatedJS do
102102
> #### Warning! {: .warning}
103103
>
104104
> LiveView assumes full ownership over the configured `:target_directory`. When
105-
> compiling, it will **delete** any files and folders inside the `:target_directory`,
105+
> compiling, it will **delete** any files and folders inside the `:target_directory`,
106106
> that it does not associate with a colocated JavaScript module or manifest.
107107
108108
### Imports in colocated JS
@@ -275,13 +275,20 @@ defmodule Phoenix.LiveView.ColocatedJS do
275275
end
276276

277277
defp write_new_manifests!(files) do
278-
files
279-
|> Enum.group_by(fn {_file, config} ->
280-
config[:manifest] || "index.js"
281-
end)
282-
|> Enum.each(fn {manifest, entries} ->
283-
write_manifest(manifest, entries)
284-
end)
278+
if files == [] do
279+
File.write!(
280+
Path.join(target_dir(), "index.js"),
281+
"export const hooks = {};\nexport default {};"
282+
)
283+
else
284+
files
285+
|> Enum.group_by(fn {_file, config} ->
286+
config[:manifest] || "index.js"
287+
end)
288+
|> Enum.each(fn {manifest, entries} ->
289+
write_manifest(manifest, entries)
290+
end)
291+
end
285292
end
286293

287294
defp write_manifest(manifest, entries) do

test/phoenix_live_view/colocated_hook_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ defmodule Phoenix.LiveView.ColocatedHookTest do
5757
script,
5858
Path.join(Mix.Project.build_path(), "phoenix-colocated/phoenix_live_view/")
5959
)
60+
after
61+
:code.delete(__MODULE__.TestComponent)
62+
:code.purge(__MODULE__.TestComponent)
6063
end
6164

6265
test "raises for invalid name" do

test/phoenix_live_view/colocated_js_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ defmodule Phoenix.LiveView.ColocatedJSTest do
5959
script,
6060
Path.join(Mix.Project.build_path(), "phoenix-colocated/phoenix_live_view/")
6161
)
62+
after
63+
:code.delete(__MODULE__.TestComponent)
64+
:code.purge(__MODULE__.TestComponent)
6265
end
6366

6467
test "keyed script is available under default named export" do
@@ -124,6 +127,9 @@ defmodule Phoenix.LiveView.ColocatedJSTest do
124127

125128
assert [_match, export_name] = Regex.run(~r/export \{ (imp_.*) as components \}/, manifest)
126129
assert manifest =~ "#{export_name}[\"my-script\"] = #{js_name};"
130+
after
131+
:code.delete(__MODULE__.TestComponentKey)
132+
:code.purge(__MODULE__.TestComponentKey)
127133
end
128134

129135
test "raises for invalid name" do
@@ -144,4 +150,11 @@ defmodule Phoenix.LiveView.ColocatedJSTest do
144150
end
145151
end
146152
end
153+
154+
test "writes empty index.js when no colocated scripts exist" do
155+
manifest = Path.join(Mix.Project.build_path(), "phoenix-colocated/phoenix_live_view/index.js")
156+
Phoenix.LiveView.ColocatedJS.compile()
157+
assert File.exists?(manifest)
158+
assert File.read!(manifest) == "export const hooks = {};\nexport default {};"
159+
end
147160
end

0 commit comments

Comments
 (0)