Skip to content

Commit dea25ad

Browse files
committed
Add option to embed the stylesheet
1 parent 58f8e08 commit dea25ad

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

lib/ex_css_modules/ex_css_modules.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule ExCSSModules do
2121
def stylesheet(definition) when is_map(definition), do: definition
2222

2323
@doc false
24-
def stylesheet(definition), do: definition |> read_stylesheet()
24+
def stylesheet(definition), do: read_stylesheet(definition)
2525

2626
def read_stylesheet(filename) do
2727
case File.exists?(filename) do

lib/ex_css_modules/view.ex

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,43 @@ defmodule ExCSSModules.View do
77
Use the ExCSSModules.View on a view which defines the JSON for CSS Modules
88
as an external resource.
99
10+
To embed the stylesheet in the file set :embed_stylesheet to true.
11+
1012
If adds the following functions to the View:
1113
- stylesheet/0 - same as ExCSSModules.stylesheet/1 with the stylesheet predefined
1214
- class/1 - same as ExCSSModules.class/2 with the stylesheet predefined
1315
- class_name/1 - same as ExCSSModules.class_name/2 with the stylesheet predefined
1416
- class_name/2 - same as ExCSSModules.class_name/3 with the stylesheet predefined
1517
- class_selector/1 - same as ExCSSModules.class_selector/2 with the stylesheet predefined
1618
"""
19+
1720
defmacro __using__(opts \\ []) do
21+
{file, [file: relative_to]} = Code.eval_quoted(opts[:stylesheet], file: __CALLER__.file)
22+
23+
file = Path.expand(file, Path.dirname(relative_to))
24+
1825
quote do
19-
@external_resource unquote(opts[:stylesheet]) <> ".json"
20-
@stylesheet ExCSSModules.read_stylesheet(unquote(opts[:stylesheet]))
26+
@stylesheet unquote(
27+
if opts[:embed_stylesheet] do
28+
Macro.escape(ExCSSModules.read_stylesheet(file))
29+
else
30+
Macro.escape(file)
31+
end
32+
)
33+
34+
def stylesheet_definition, do: @stylesheet
2135

2236
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)
2337

24-
def class(key), do: ExCSSModules.class(@stylesheet, key)
38+
def class(key), do: stylesheet() |> ExCSSModules.class(key)
2539

26-
def class_name(key), do: ExCSSModules.class_name(@stylesheet, key)
40+
def class_name(key) do
41+
ExCSSModules.class_name(stylesheet(), key)
42+
end
2743
def class_name(key, value), do:
28-
ExCSSModules.class_name(@stylesheet, key, value)
44+
ExCSSModules.class_name(stylesheet(), key, value)
2945

30-
def class_selector(key), do: ExCSSModules.class_selector(@stylesheet, key)
46+
def class_selector(key), do: ExCSSModules.class_selector(stylesheet(), key)
3147
end
3248
end
3349
end

test/ex_css_modules/view_test.exs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,33 @@ defmodule ExCSSModules.ViewTest do
77

88
defmodule ViewModuleTest do
99
use ExCSSModules.View, stylesheet: __ENV__.file
10-
|> Path.dirname
11-
|> Path.join("../support/stylesheet.css")
10+
|> Path.dirname
11+
|> Path.join("../support/stylesheet.css")
12+
end
13+
14+
defmodule EmbeddedViewModuleTest do
15+
use ExCSSModules.View, stylesheet: __ENV__.file
16+
|> Path.dirname
17+
|> Path.join("../support/stylesheet.css"),
18+
embed_stylesheet: true
19+
end
1220

21+
describe "stylesheet_definition/0" do
22+
test "gets the stylesheet string" do
23+
assert ViewModuleTest.stylesheet_definition
24+
== Path.expand(@example_stylesheet)
25+
end
26+
27+
test "gets the embedded stylesheet" do
28+
assert EmbeddedViewModuleTest.stylesheet_definition
29+
== ExCSSModules.stylesheet(@example_stylesheet)
30+
end
1331
end
1432

1533
describe "stylesheet/0" do
1634
test "calls the stylesheet" do
17-
assert ViewModuleTest.stylesheet == ExCSSModules.stylesheet(@example_stylesheet)
35+
assert ViewModuleTest.stylesheet
36+
== ExCSSModules.stylesheet(@example_stylesheet)
1837
end
1938
end
2039

0 commit comments

Comments
 (0)