Skip to content

Commit c626d84

Browse files
committed
Better developer experience when missing/empty style definitions files
Raise a compile error with custom message if the JSON file is empty or absent, explaining what to do to be able to compile again.
1 parent 283d1ae commit c626d84

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

lib/ex_css_modules/ex_css_modules.ex

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,40 @@ defmodule ExCSSModules do
3737
def stylesheet(definition, build_json_task), do: read_stylesheet(definition, build_json_task)
3838

3939
defp read_stylesheet(filename, build_json_task) do
40-
cond do
41-
File.exists?("#{filename}.json") && !empty_file?("#{filename}.json") ->
42-
"#{filename}.json"
43-
|> File.read!()
44-
|> json_library().decode!()
45-
46-
build_json_task && File.exists?(filename) ->
47-
with {:ok, json_filename} <- build_json_task.run(filename: filename) do
48-
json_filename
40+
if File.exists?(filename) do
41+
cond do
42+
File.exists?("#{filename}.json") && not empty_file?("#{filename}.json") ->
43+
"#{filename}.json"
4944
|> File.read!()
5045
|> json_library().decode!()
51-
end
5246

53-
true ->
54-
%{}
47+
not is_nil(build_json_task) ->
48+
with {:ok, json_filename} <- build_json_task.run(filename: filename) do
49+
json_filename
50+
|> File.read!()
51+
|> json_library().decode!()
52+
end
53+
54+
not File.exists?("#{filename}.json") ->
55+
message =
56+
"""
57+
File does not exist.
58+
Error compiling ExCSSModules: Be sure to build your style definitions JSON files before compiling your application.
59+
"""
60+
61+
raise(CompileError, description: message, file: "#{filename}.json")
62+
63+
empty_file?(filename) ->
64+
message =
65+
"""
66+
File is empty.
67+
Error compiling ExCSSModules: Remove the file and/or (re)build your style definitions JSON files before compiling your application.
68+
"""
69+
70+
raise(CompileError, description: message, file: "#{filename}.json")
71+
end
72+
else
73+
%{}
5574
end
5675
end
5776

test/ex_css_modules/view_test.exs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ defmodule ExCSSModules.ViewTest do
4343
stylesheet:
4444
__ENV__.file
4545
|> Path.dirname()
46-
|> Path.join("../support/no_stylesheet_definition.css")
46+
|> Path.join("../support/no_stylesheet_definitions.css")
4747
end
4848

4949
describe "stylesheet_definition/0" do
@@ -68,12 +68,28 @@ defmodule ExCSSModules.ViewTest do
6868
assert ViewModule.StylesheetDefinitions.EmptyObject.Test.stylesheet() == %{}
6969
end
7070

71-
test "returns an empty map if the stylesheet definitions json file is empty" do
72-
assert ViewModule.StylesheetDefinitions.EmptyFile.Test.stylesheet() == %{}
71+
test "raises if the stylesheet definitions json file is empty" do
72+
message =
73+
"""
74+
test/support/empty_file.css.json: File is empty.
75+
Error compiling ExCSSModules: Remove the file and/or (re)build your style definitions JSON files before compiling your application.
76+
"""
77+
78+
assert_raise(CompileError, message, fn ->
79+
ViewModule.StylesheetDefinitions.EmptyFile.Test.stylesheet()
80+
end)
7381
end
7482

75-
test "returns an empty map if the stylesheet definitions json file does not exist" do
76-
assert ViewModule.StylesheetDefinitions.NoFile.Test.stylesheet() == %{}
83+
test "raises if the stylesheet definitions json file does not exist" do
84+
message =
85+
"""
86+
test/support/no_stylesheet_definitions.css.json: File does not exist.
87+
Error compiling ExCSSModules: Be sure to build your style definitions JSON files before compiling your application.
88+
"""
89+
90+
assert_raise(CompileError, message, fn ->
91+
ViewModule.StylesheetDefinitions.NoFile.Test.stylesheet()
92+
end)
7793
end
7894
end
7995

0 commit comments

Comments
 (0)