Skip to content

Commit 425bb3a

Browse files
authored
Merge pull request #112 from DefactoSoftware/pien/add-task
Add optional task to generate style.css.json file before trying to read
2 parents 027336f + 725b4d8 commit 425bb3a

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

CHANGELOG.MD

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.0.8
2+
3+
## Added
4+
- `:build_stylesheet_definitions_json_task` config option to run a task for building missing stylesheet definitions JSON file when using `:embed_stylsheet` (/DefactoSoftware/ex_css_modules/pull/#112).
5+
16
# v0.0.7
27

38
## Added

README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,26 @@
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.
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:
11+
12+
```ex
13+
defmodule Mix.Tasks.GenerateMissingJson do
14+
use Mix.Task
15+
16+
@impl Mix.Task
17+
18+
def run(filename) do
19+
with _exit_code = 0 <-
20+
Mix.shell().cmd("npx postcss #{filename} --dir ./tmp/postcss --verbose") do
21+
{:ok, filename <> ".json"}
22+
else
23+
_exit_code ->
24+
relative_path = Path.relative_to_cwd(filename)
25+
exit("Error generating scoped JSON file ./#{relative_path}.json")
26+
end
27+
end
28+
end
29+
```
1130

1231
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.
1332

@@ -16,7 +35,7 @@ Install from [Hex.pm](https://hex.pm/packages/ex_css_modules):
1635

1736
```ex
1837
def deps do
19-
[{:ex_css_modules, "~> 0.0.7"}]
38+
[{:ex_css_modules, "~> 0.0.8"}]
2039
end
2140
```
2241

lib/ex_css_modules/ex_css_modules.ex

+16-8
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, build_json_task \\ nil)
36+
def stylesheet(definition, _build_json_task) when is_map(definition), do: definition
37+
def stylesheet(definition, build_json_task), do: read_stylesheet(definition, build_json_task)
38+
39+
defp read_stylesheet(filename, build_json_task) do
40+
cond do
41+
File.exists?(filename <> ".json") ->
4142
(filename <> ".json")
4243
|> File.read!()
4344
|> json_library().decode!()
4445

45-
false ->
46+
build_json_task && File.exists?(filename) ->
47+
with {:ok, json_filename} <- build_json_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

+3-2
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+
build_json_task = Keyword.get(opts, :build_stylesheet_definitions_json_task, nil)
3233

3334
quote do
3435
@stylesheet unquote(
3536
if embed_stylesheet? do
36-
Macro.escape(ExCSSModules.stylesheet(filename))
37+
Macro.escape(ExCSSModules.stylesheet(filename, build_json_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(build_json_task))
4546

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

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule ExCSSModules.Mixfile do
22
use Mix.Project
33

4-
@version "0.0.7"
4+
@version "0.0.8"
55

66
def project do
77
[

0 commit comments

Comments
 (0)