Skip to content

Commit 220b629

Browse files
committed
Fix error with empty style definitions json file
An artifact in our own codebase could sometimes cause empty style definitions json files. This will fail compilation of ex_css_modules because decoding an empty string is not working. To capture this edge case, let's check if the file is empty before trying to decode OR generate the json file through a task.
1 parent 425bb3a commit 220b629

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
ExCSSModules defines two ways to read the stylesheet: embedded and read.
99

10-
If you set the `embed_stylesheet` option to the `use` macro the stylesheet definitions JSON have to be compiled before the application is compiled. This flag is used for production to optimize read times. Additionally, you can add a task to generate missing stylesheet definitions JSON files when compiling ExCSSModules, to prevent compilation errors when one of the stylesheet definitions JSON might be missing:
10+
If you set the `embed_stylesheet` option to the `use` macro the stylesheet definitions JSON have to be compiled before the application is compiled. This flag is used for production to optimize read times.
11+
12+
If you don't set the flag or set it to false, the stylesheet definition JSON files are read live from the server which creates a lot of IO for each request.
13+
14+
Additionally, you can pass a task to generate missing stylesheet definitions JSON files when compiling ExCSSModules, to prevent compilation errors when one of the stylesheet definitions JSON might be missing:
1115

1216
```ex
1317
defmodule Mix.Tasks.GenerateMissingJson do
@@ -28,7 +32,18 @@ defmodule Mix.Tasks.GenerateMissingJson do
2832
end
2933
```
3034

31-
If you don't set the flag or set it to false, the stylesheet definition JSON files are read live from the server which creates a lot of IO for each request.
35+
This task can be passed through the option `build_stylesheet_definitions_json_task`:
36+
37+
```ex
38+
use ExCSSModules.View,
39+
namespace: MyApplicationWeb,
40+
embed_stylesheet: ApplicationSettings.built_for?(:prod),
41+
build_stylesheet_definitions_json_task: unquote(Mix.Tasks.BuildScopedJson),
42+
stylesheet:
43+
__ENV__.file
44+
|> Path.dirname()
45+
|> Path.join("./style.css")
46+
```
3247

3348
## Installation
3449
Install from [Hex.pm](https://hex.pm/packages/ex_css_modules):

lib/ex_css_modules/ex_css_modules.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ defmodule ExCSSModules do
3838

3939
defp read_stylesheet(filename, build_json_task) do
4040
cond do
41-
File.exists?(filename <> ".json") ->
42-
(filename <> ".json")
41+
File.exists?("#{filename}.json") && !empty_file?("#{filename}.json") ->
42+
"#{filename}.json"
4343
|> File.read!()
4444
|> json_library().decode!()
4545

@@ -55,6 +55,8 @@ defmodule ExCSSModules do
5555
end
5656
end
5757

58+
defp empty_file?(filename), do: File.read!(filename) == ""
59+
5860
@doc """
5961
Reads the class definitions from the definition map and maps them to a class
6062
attribute. The `keys` argument is used to retrieve the class name or multiple

0 commit comments

Comments
 (0)