Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/ex_css_modules/ex_css_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,26 @@ defmodule ExCSSModules do
%{}

"""
@spec stylesheet(String.t() | map()) :: map()
def stylesheet(definition) when is_map(definition), do: definition
def stylesheet(definition), do: read_stylesheet(definition)

defp read_stylesheet(filename) do
case File.exists?(filename) do
true ->
@spec stylesheet(String.t() | map(), module()) :: map()
def stylesheet(definition, task \\ nil)
def stylesheet(definition, _task) when is_map(definition), do: definition
def stylesheet(definition, task), do: read_stylesheet(definition, task)

defp read_stylesheet(filename, task) do
cond do
File.exists?(filename <> ".json") ->
(filename <> ".json")
|> File.read!()
|> json_library().decode!()

false ->
task && File.exists?(filename) ->
with {:ok, json_filename} <- task.run(filename: filename) do
json_filename
|> File.read!()
|> json_library().decode!()
end

true ->
%{}
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/ex_css_modules/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ defmodule ExCSSModules.View do
)

filename = Path.expand(filename, Path.dirname(relative_to))
generate_scoped_name_task = Keyword.get(opts, :generate_scoped_name_task)

quote do
@stylesheet unquote(
if embed_stylesheet? do
Macro.escape(ExCSSModules.stylesheet(filename))
Macro.escape(ExCSSModules.stylesheet(filename, generate_scoped_name_task))
else
Macro.escape(filename)
end
)

def stylesheet_definition, do: @stylesheet

def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet, unquote(generate_scoped_name_task))

def class(key), do: stylesheet() |> ExCSSModules.class(key)
def class(key, value), do: stylesheet() |> ExCSSModules.class(key, value)
Expand Down