@@ -128,15 +128,14 @@ defmodule Phoenix.LiveView.ColocatedJS do
128128 The supported attributes are:
129129
130130 * `name` - The name under which the default export of the script is available when importing
131- the manifest. This is required, even if you don't plan to access the exported values or the
132- script does not actually export anything.
131+ the manifest. If omitted, the file will be imported for side effects only.
133132
134133 * `key` - A custom key to use for the export. This is used by `Phoenix.LiveView.ColocatedHook` to
135134 export all hooks under the named `hooks` export (`export { ... as hooks }`).
136135 For example, you could set this to `web_components` for each colocated script that defines
137136 a web component and then import all of them as `import { web_components } from "phoenix-colocated/my_app"`.
138137 Defaults to `:default`, which means the export will be available under the manifest's `default` export.
139- This needs to be a valid JavaScript identifier.
138+ This needs to be a valid JavaScript identifier. When given, a `name` is required as well.
140139
141140 * `extension` - a custom extension to use when writing the extracted file. The default is `js`.
142141
@@ -173,8 +172,12 @@ defmodule Phoenix.LiveView.ColocatedJS do
173172 raise ArgumentError ,
174173 "the name attribute of a colocated script must be a compile-time string. Got: #{ Macro . to_string ( name ) } "
175174
175+ % { "key" => _ } ->
176+ raise ArgumentError ,
177+ "a name is required when a key is given"
178+
176179 _ ->
177- raise ArgumentError , "missing required name attribute for ColocatedJS"
180+ :ok
178181 end
179182 end
180183
@@ -191,7 +194,7 @@ defmodule Phoenix.LiveView.ColocatedJS do
191194 |> maybe_put_opt ( opts , "manifest" , :manifest )
192195
193196 hashed_name =
194- filename_opts . name
197+ ( filename_opts . name || text_content )
195198 |> then ( & :crypto . hash ( :md5 , & 1 ) )
196199 |> Base . encode32 ( case: :lower , padding: false )
197200
@@ -304,13 +307,17 @@ defmodule Phoenix.LiveView.ColocatedJS do
304307 { :default , entries } ->
305308 [
306309 acc ,
307- Enum . map ( entries , fn { file , % { name: name } } ->
308- import_name =
309- "js_" <> Base . encode32 ( :crypto . hash ( :md5 , file ) , case: :lower , padding: false )
310+ Enum . map ( entries , fn
311+ { file , % { name: nil } } ->
312+ ~s [ import "./ #{ Path . relative_to ( file , target_dir ) } "; \n ]
310313
311- escaped_name = Phoenix.HTML . javascript_escape ( name )
314+ { file , % { name: name } } ->
315+ import_name =
316+ "js_" <> Base . encode32 ( :crypto . hash ( :md5 , file ) , case: :lower , padding: false )
312317
313- ~s< import #{ import_name } from "./#{ Path . relative_to ( file , target_dir ) } "; js["#{ escaped_name } "] = #{ import_name } ;\n >
318+ escaped_name = Phoenix.HTML . javascript_escape ( name )
319+
320+ ~s< import #{ import_name } from "./#{ Path . relative_to ( file , target_dir ) } "; js["#{ escaped_name } "] = #{ import_name } ;\n >
314321 end )
315322 ]
316323
@@ -320,13 +327,17 @@ defmodule Phoenix.LiveView.ColocatedJS do
320327 [
321328 acc ,
322329 ~s< const #{ tmp_name } = {}; export { #{ tmp_name } as #{ key } };\n > ,
323- Enum . map ( entries , fn { file , % { name: name } } ->
324- import_name =
325- "js_" <> Base . encode32 ( :crypto . hash ( :md5 , file ) , case: :lower , padding: false )
330+ Enum . map ( entries , fn
331+ { file , % { name: nil } } ->
332+ ~s[ import "./#{ Path . relative_to ( file , target_dir ) } ";\n ]
333+
334+ { file , % { name: name } } ->
335+ import_name =
336+ "js_" <> Base . encode32 ( :crypto . hash ( :md5 , file ) , case: :lower , padding: false )
326337
327- escaped_name = Phoenix.HTML . javascript_escape ( name )
338+ escaped_name = Phoenix.HTML . javascript_escape ( name )
328339
329- ~s< import #{ import_name } from "./#{ Path . relative_to ( file , target_dir ) } "; #{ tmp_name } ["#{ escaped_name } "] = #{ import_name } ;\n >
340+ ~s< import #{ import_name } from "./#{ Path . relative_to ( file , target_dir ) } "; #{ tmp_name } ["#{ escaped_name } "] = #{ import_name } ;\n >
330341 end )
331342 ]
332343 end
0 commit comments