@@ -28,12 +28,15 @@ defmodule Surface.BaseComponent do
2828 { Surface.Components.Form.Inputs , :render } => true
2929 }
3030
31+ @ supported_hooks_extensions ~W" js jsx ts tsx"
32+
3133 defmacro __using__ ( opts ) do
3234 type = Keyword . fetch! ( opts , :type )
3335
3436 root = Path . dirname ( __CALLER__ . file )
3537 css_file_name = css_filename ( __CALLER__ )
3638 css_file = Path . join ( root , css_file_name )
39+ hooks_files = hooks_files ( __CALLER__ )
3740
3841 Module . register_attribute ( __CALLER__ . module , :__style__ , accumulate: true )
3942
@@ -69,6 +72,13 @@ defmodule Surface.BaseComponent do
6972
7073 @ external_resource unquote ( css_file )
7174
75+ # Although hooks have no effect in the generated beam file, we need this dependency
76+ # now to trigger js assets building by the surface compiler, which has been optimized
77+ # to run only if any of the previous compilers have also run.
78+ for file <- unquote ( hooks_files ) do
79+ @ external_resource file
80+ end
81+
7282 @ propagate_context_to_slots_map unquote ( __MODULE__ ) . build_propagate_context_to_slots_map ( )
7383 end
7484 end
@@ -246,10 +256,22 @@ defmodule Surface.BaseComponent do
246256 end
247257
248258 defp css_filename ( env ) do
249- env . module
259+ base_file_name ( env . module ) <> ".css"
260+ end
261+
262+ defp hooks_files ( env ) do
263+ root = Path . dirname ( env . file )
264+ base_name = base_file_name ( env . module )
265+
266+ for ext <- @ supported_hooks_extensions do
267+ Path . join ( root , "#{ base_name } .hooks.#{ ext } " )
268+ end
269+ end
270+
271+ defp base_file_name ( module ) do
272+ module
250273 |> Module . split ( )
251274 |> List . last ( )
252275 |> Macro . underscore ( )
253- |> Kernel . <> ( ".css" )
254276 end
255277end
0 commit comments