Skip to content

Commit

Permalink
Change stylesheet recompilation strategy
Browse files Browse the repository at this point in the history
Stylesheets will now compile into a component module
via `embed_stylesheet/1` and be renderable as a function component via
`stylesheet/1`

Also added `__stylesheet_ast__/0` to access the AST results

to fully enable the LiveReloader needs its pattern extended to look
at the directly the stylesheets are in and the `.neex` template extension
  • Loading branch information
bcardarella committed Jan 17, 2024
1 parent e3c2521 commit 6015139
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
22 changes: 11 additions & 11 deletions lib/live_view_native/stylesheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule LiveViewNative.Stylesheet do
use unquote(parser)
@format unquote(format)
@before_compile LiveViewNative.Stylesheet
@after_verify LiveViewNative.Stylesheet
# @after_verify LiveViewNative.Stylesheet

def compile_ast(class_or_list, target \\ [target: :all])
def compile_ast(class_or_list, target: target) do
Expand Down Expand Up @@ -73,17 +73,17 @@ defmodule LiveViewNative.Stylesheet do
end
end

def __after_verify__(module) do
compiled_sheet =
LiveViewNative.Stylesheet.Extractor.run()
|> module.compile_string()
# def __after_verify__(module) do
# compiled_sheet =
# LiveViewNative.Stylesheet.Extractor.run()
# |> module.compile_string()

output_path = file_path(module)
# output_path = file_path(module)

output_path
|> Path.dirname()
|> File.mkdir_p!()
# output_path
# |> Path.dirname()
# |> File.mkdir_p!()

File.write(output_path, compiled_sheet)
end
# File.write(output_path, compiled_sheet)
# end
end
43 changes: 28 additions & 15 deletions lib/live_view_native/stylesheet/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,39 @@ defmodule LiveViewNative.Stylesheet.Component do
import LiveViewNative.Component, only: [sigil_LVN: 2]

@doc """
Embed the stylesheet within a template
Embeds a stylehseet within the component module
Take a module as an attribute value:
Compiled stylesheet can be called with the
functional component `stylesheet/1`
```heex
<.embed_stylsheet module={MyAppWeb.HomeSheet} />
<.stylesheet />
```
"""
def embed_stylesheet(%{module: module} = assigns) do
sheet =
module
|> LiveViewNative.Stylesheet.file_path()
|> File.read!()
|> Phoenix.HTML.raw()

assigns = Map.put(assigns, :sheet, sheet)

~LVN"""
<Style><%= @sheet %></Style>
"""
defmacro embed_stylesheet(stylesheet_module) do
stylesheet_module = Macro.expand(stylesheet_module, __CALLER__)

extracted_class_names = LiveViewNative.Stylesheet.Extractor.run()

compiled_sheet_string = stylesheet_module.compile_string(extracted_class_name)

Check warning on line 22 in lib/live_view_native/stylesheet/component.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "compiled_sheet_string" is unused (if the variable is not meant to be used, prefix it with an underscore)
compiled_sheet_ast = stylesheet_module.compile_ast(extracted_class_names)

quote do
# this function is mostly intended for debugging purposes
# it isn't intended to be used directly in your application code
def __stylsheet_ast__ do
unquote(compiled_sheet_ast)
end

def stylesheet(var!(assigns)) do
sheet = unquote(Macro.escape(compiled_sheet))
var!(assigns) = Map.put(var!(assigns), :sheet, sheet)

~LVN"""
<Style><%= @sheet %></Style>
"""
end
end
end

@doc """
Expand Down

0 comments on commit 6015139

Please sign in to comment.