Skip to content

Commit 5a5ccde

Browse files
Merge pull request #5 from DefactoSoftware/jesse/add-option-to-embed-stylesheet
Add option to embed the stylesheet
2 parents 58f8e08 + 0e21aaa commit 5a5ccde

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-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: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,44 @@ 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]} =
22+
Code.eval_quoted(opts[:stylesheet], file: __CALLER__.file)
23+
24+
file = Path.expand(file, Path.dirname(relative_to))
25+
1826
quote do
19-
@external_resource unquote(opts[:stylesheet]) <> ".json"
20-
@stylesheet ExCSSModules.read_stylesheet(unquote(opts[:stylesheet]))
27+
@stylesheet unquote(
28+
if opts[:embed_stylesheet] do
29+
Macro.escape(ExCSSModules.read_stylesheet(file))
30+
else
31+
Macro.escape(file)
32+
end
33+
)
34+
35+
def stylesheet_definition, do: @stylesheet
2136

2237
def stylesheet, do: ExCSSModules.stylesheet(@stylesheet)
2338

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

26-
def class_name(key), do: ExCSSModules.class_name(@stylesheet, key)
41+
def class_name(key) do
42+
ExCSSModules.class_name(stylesheet(), key)
43+
end
2744
def class_name(key, value), do:
28-
ExCSSModules.class_name(@stylesheet, key, value)
45+
ExCSSModules.class_name(stylesheet(), key, value)
2946

30-
def class_selector(key), do: ExCSSModules.class_selector(@stylesheet, key)
47+
def class_selector(key), do: ExCSSModules.class_selector(stylesheet(), key)
3148
end
3249
end
3350
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)