Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.0.8

## Added
- `: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).

# v0.0.7

## Added
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@

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

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.
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:

```ex
defmodule Mix.Tasks.GenerateMissingJson do
use Mix.Task

@impl Mix.Task

def run(filename) do
with _exit_code = 0 <-
Mix.shell().cmd("npx postcss #{filename} --dir ./tmp/postcss --verbose") do
{:ok, filename <> ".json"}
else
_exit_code ->
relative_path = Path.relative_to_cwd(filename)
exit("Error generating scoped JSON file ./#{relative_path}.json")
end
end
end
```

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.

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

```ex
def deps do
[{:ex_css_modules, "~> 0.0.7"}]
[{:ex_css_modules, "~> 0.0.8"}]
end
```

Expand Down
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, build_json_task \\ nil)
def stylesheet(definition, _build_json_task) when is_map(definition), do: definition
def stylesheet(definition, build_json_task), do: read_stylesheet(definition, build_json_task)

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

false ->
build_json_task && File.exists?(filename) ->
with {:ok, json_filename} <- build_json_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))
build_json_task = Keyword.get(opts, :build_stylesheet_definitions_json_task, nil)

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

def stylesheet_definition, do: @stylesheet

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

def class(key), do: stylesheet() |> ExCSSModules.class(key)
def class(key, value), do: stylesheet() |> ExCSSModules.class(key, value)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExCSSModules.Mixfile do
use Mix.Project

@version "0.0.7"
@version "0.0.8"

def project do
[
Expand Down