Skip to content

Commit 33ef426

Browse files
committed
Add optional task to generate style.css.json file before trying to read
1 parent 027336f commit 33ef426

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/ex_css_modules/ex_css_modules.ex

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,26 @@ defmodule ExCSSModules do
3131
%{}
3232
3333
"""
34-
@spec stylesheet(String.t() | map()) :: map()
35-
def stylesheet(definition) when is_map(definition), do: definition
36-
def stylesheet(definition), do: read_stylesheet(definition)
37-
38-
defp read_stylesheet(filename) do
39-
case File.exists?(filename) do
40-
true ->
34+
@spec stylesheet(String.t() | map(), module()) :: map()
35+
def stylesheet(definition, task \\ nil)
36+
def stylesheet(definition, _task) when is_map(definition), do: definition
37+
def stylesheet(definition, task), do: read_stylesheet(definition, task)
38+
39+
defp read_stylesheet(filename, task) do
40+
cond do
41+
File.exists?(filename <> ".json") ->
4142
(filename <> ".json")
4243
|> File.read!()
4344
|> json_library().decode!()
4445

45-
false ->
46+
task && File.exists?(filename) ->
47+
with {:ok, json_filename} <- task.run(filename: filename) do
48+
json_filename
49+
|> File.read!()
50+
|> json_library().decode!()
51+
end
52+
53+
true ->
4654
%{}
4755
end
4856
end

lib/ex_css_modules/view.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ defmodule ExCSSModules.View do
2929
)
3030

3131
filename = Path.expand(filename, Path.dirname(relative_to))
32+
generate_scoped_name_task = Keyword.get(opts, :generate_scoped_name_task)
3233

3334
quote do
3435
@stylesheet unquote(
3536
if embed_stylesheet? do
36-
Macro.escape(ExCSSModules.stylesheet(filename))
37+
Macro.escape(ExCSSModules.stylesheet(filename, generate_scoped_name_task))
3738
else
3839
Macro.escape(filename)
3940
end
4041
)
4142

4243
def stylesheet_definition, do: @stylesheet
4344

44-
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)
45+
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet, unquote(generate_scoped_name_task))
4546

4647
def class(key), do: stylesheet() |> ExCSSModules.class(key)
4748
def class(key, value), do: stylesheet() |> ExCSSModules.class(key, value)

0 commit comments

Comments
 (0)