diff --git a/lib/live_view_native/extensions.ex b/lib/live_view_native/extensions.ex index c21455c3..af885371 100644 --- a/lib/live_view_native/extensions.ex +++ b/lib/live_view_native/extensions.ex @@ -9,9 +9,11 @@ defmodule LiveViewNative.Extensions do respectively. """ defmacro __using__(opts) do + compiled_at = :os.system_time(:nanosecond) role = opts[:role] - quote bind_quoted: [caller: Macro.escape(__CALLER__), role: role], location: :keep do + quote bind_quoted: [caller: Macro.escape(__CALLER__), compiled_at: compiled_at, role: role], + location: :keep do Code.put_compiler_option(:ignore_module_conflict, true) for {platform_id, platform_context} <- LiveViewNative.platforms() do @@ -43,10 +45,12 @@ defmodule LiveViewNative.Extensions do if is_nil(platform_context.render_macro) do use LiveViewNative.Extensions.InlineRender, + compiled_at: compiled_at, platform_id: platform_id, role: role else use LiveViewNative.Extensions.RenderMacro, + compiled_at: compiled_at, platform_id: platform_id, render_macro: platform_context.render_macro, role: role diff --git a/lib/live_view_native/extensions/inline_render.ex b/lib/live_view_native/extensions/inline_render.ex index 73ae14d2..07524bfb 100644 --- a/lib/live_view_native/extensions/inline_render.ex +++ b/lib/live_view_native/extensions/inline_render.ex @@ -31,10 +31,12 @@ defmodule LiveViewNative.Extensions.InlineRender do """ defmacro __using__(opts \\ []) do quote bind_quoted: [ + compiled_at: opts[:compiled_at], platform_id: opts[:platform_id], stylesheet: opts[:stylesheet], role: opts[:role] - ], location: :keep do + ], + location: :keep do require EEx defmacro sigil_LVN({:<<>>, meta, [expr]}, modifiers) do @@ -47,6 +49,7 @@ defmodule LiveViewNative.Extensions.InlineRender do platform_module <- Module.concat(__ENV__.module, context.template_namespace) do base_opts = [ caller: __CALLER__, + compiled_at: unquote(compiled_at), engine: Phoenix.LiveView.TagEngine, file: __CALLER__.file, indentation: meta[:indentation] || 0, diff --git a/lib/live_view_native/extensions/modifiers.ex b/lib/live_view_native/extensions/modifiers.ex index c40d4397..dd0a1f14 100644 --- a/lib/live_view_native/extensions/modifiers.ex +++ b/lib/live_view_native/extensions/modifiers.ex @@ -100,7 +100,8 @@ defmodule LiveViewNative.Extensions.Modifiers do modifiers_struct: opts[:modifiers_struct], platform_modifiers: opts[:platform_modifiers], platform_module: opts[:platform_module] - ], location: :keep do + ], + location: :keep do all_modifiers = Keyword.merge(platform_modifiers, custom_modifiers) if is_nil(platform_module) do diff --git a/lib/live_view_native/extensions/render_macro.ex b/lib/live_view_native/extensions/render_macro.ex index 14a03a97..7e6c7192 100644 --- a/lib/live_view_native/extensions/render_macro.ex +++ b/lib/live_view_native/extensions/render_macro.ex @@ -9,10 +9,12 @@ defmodule LiveViewNative.Extensions.RenderMacro do """ defmacro __using__(opts \\ []) do quote bind_quoted: [ + compiled_at: opts[:compiled_at], render_macro: opts[:render_macro], platform_id: opts[:platform_id], role: opts[:role] - ], location: :keep do + ], + location: :keep do defmacro unquote(:"#{render_macro}")({:<<>>, meta, [expr]}, _modifiers) do unless Macro.Env.has_var?(__CALLER__, {:assigns, nil}) do raise "#{unquote(render_macro)} requires a variable named \"assigns\" to exist and be set to a map" @@ -22,6 +24,7 @@ defmodule LiveViewNative.Extensions.RenderMacro do %LiveViewNativePlatform.Env{} = context <- Map.get(platforms, unquote(platform_id)), platform_module <- Module.concat(__ENV__.module, context.template_namespace) do base_opts = [ + compiled_at: unquote(compiled_at), caller: __CALLER__, engine: Phoenix.LiveView.TagEngine, file: __CALLER__.file, diff --git a/lib/live_view_native/extensions/stylesheets.ex b/lib/live_view_native/extensions/stylesheets.ex index 3957b703..f4915780 100644 --- a/lib/live_view_native/extensions/stylesheets.ex +++ b/lib/live_view_native/extensions/stylesheets.ex @@ -11,22 +11,12 @@ defmodule LiveViewNative.Extensions.Stylesheets do quote bind_quoted: [module: module], location: :keep do def __compiled_stylesheet__(stylesheet_key) do - class_tree_module = - Module.safe_concat([LiveViewNative, Internal, ClassTree, unquote(module)]) + stylesheet_modules = __stylesheet_modules__() - class_tree = apply(class_tree_module, :class_tree, [stylesheet_key]) - - class_names = - class_tree - |> Map.values() - |> List.flatten() - - __stylesheet_modules__() - |> Enum.reduce(%{}, fn stylesheet_module, acc -> - compiled_stylesheet = apply(stylesheet_module, :compile_ast, [class_names]) - - Map.merge(acc, compiled_stylesheet) - end) + unquote(module) + |> LiveViewNative.Stylesheets.get_class_tree_module() + |> LiveViewNative.Stylesheets.get_class_tree(stylesheet_key) + |> LiveViewNative.Stylesheets.reduce_stylesheets(stylesheet_modules) |> inspect(limit: :infinity, charlists: :as_list, printable_limit: :infinity) end diff --git a/lib/live_view_native/extensions/templates.ex b/lib/live_view_native/extensions/templates.ex index 983513f1..260e05ac 100644 --- a/lib/live_view_native/extensions/templates.ex +++ b/lib/live_view_native/extensions/templates.ex @@ -24,7 +24,8 @@ defmodule LiveViewNative.Extensions.Templates do template_basename: opts[:template_basename], template_directory: opts[:template_directory], template_extension: opts[:template_extension] - ], location: :keep do + ], + location: :keep do template_path = Path.join(template_directory, template_basename) <> template_extension if is_binary(template_path) and File.exists?(template_path) do diff --git a/lib/live_view_native/layouts.ex b/lib/live_view_native/layouts.ex index d03b041a..59b8abcd 100644 --- a/lib/live_view_native/layouts.ex +++ b/lib/live_view_native/layouts.ex @@ -6,7 +6,7 @@ defmodule LiveViewNative.Layouts do |> extract_layouts_recursive(opts) |> List.flatten() |> Enum.map(fn layout_params -> {layout_params.render_function, layout_params} end) - |> Enum.reject(&(format_excluded?(&1, opts))) + |> Enum.reject(&format_excluded?(&1, opts)) |> Enum.into(%{}) |> apply_default_layouts(opts) |> generate_class_trees(opts) @@ -26,7 +26,8 @@ defmodule LiveViewNative.Layouts do def extract_layouts_recursive({_func, _meta, [_ | _] = nodes}, %{} = opts), do: Enum.map(nodes, &extract_layouts_recursive(&1, opts)) - def extract_layouts_recursive([do: {:__block__, [], args}], %{} = opts), do: extract_layouts_recursive(args, opts) + def extract_layouts_recursive([do: {:__block__, [], args}], %{} = opts), + do: extract_layouts_recursive(args, opts) def extract_layouts_recursive([_ | _] = nodes, %{} = opts), do: Enum.map(nodes, &extract_layouts_recursive(&1, opts)) @@ -49,9 +50,11 @@ defmodule LiveViewNative.Layouts do |> String.replace(".", "_") |> String.to_atom() + is_root_template? = "#{func_name}" == "root_#{platform.platform_id}" + %{ class_tree: %{}, - template: File.read!(template_path), + template: layout_template(template_path, is_root_template?), eex_engine: platform.eex_engine, platform_id: platform.platform_id, render_function: func_name, @@ -62,6 +65,31 @@ defmodule LiveViewNative.Layouts do def compile_layout(_platform, _template_path, _opts), do: nil + def layout_template(template_path, is_root_template?) do + template_path + |> File.read!() + |> layout_template_with_live_reload(Mix.env()) + |> layout_template_with_csrf_token(is_root_template?) + end + + def layout_template_with_live_reload(template, :dev) do + """ + #{template} +