diff --git a/lib/ex_css_modules/ex_css_modules.ex b/lib/ex_css_modules/ex_css_modules.ex index 2296328..3491c86 100644 --- a/lib/ex_css_modules/ex_css_modules.ex +++ b/lib/ex_css_modules/ex_css_modules.ex @@ -21,7 +21,7 @@ defmodule ExCSSModules do def stylesheet(definition) when is_map(definition), do: definition @doc false - def stylesheet(definition), do: definition |> read_stylesheet() + def stylesheet(definition), do: read_stylesheet(definition) def read_stylesheet(filename) do case File.exists?(filename) do diff --git a/lib/ex_css_modules/view.ex b/lib/ex_css_modules/view.ex index c93bff3..67d127a 100644 --- a/lib/ex_css_modules/view.ex +++ b/lib/ex_css_modules/view.ex @@ -7,6 +7,8 @@ defmodule ExCSSModules.View do Use the ExCSSModules.View on a view which defines the JSON for CSS Modules as an external resource. + To embed the stylesheet in the file set :embed_stylesheet to true. + If adds the following functions to the View: - stylesheet/0 - same as ExCSSModules.stylesheet/1 with the stylesheet predefined - class/1 - same as ExCSSModules.class/2 with the stylesheet predefined @@ -14,20 +16,35 @@ defmodule ExCSSModules.View do - class_name/2 - same as ExCSSModules.class_name/3 with the stylesheet predefined - class_selector/1 - same as ExCSSModules.class_selector/2 with the stylesheet predefined """ + defmacro __using__(opts \\ []) do + {file, [file: relative_to]} = + Code.eval_quoted(opts[:stylesheet], file: __CALLER__.file) + + file = Path.expand(file, Path.dirname(relative_to)) + quote do - @external_resource unquote(opts[:stylesheet]) <> ".json" - @stylesheet ExCSSModules.read_stylesheet(unquote(opts[:stylesheet])) + @stylesheet unquote( + if opts[:embed_stylesheet] do + Macro.escape(ExCSSModules.read_stylesheet(file)) + else + Macro.escape(file) + end + ) + + def stylesheet_definition, do: @stylesheet def stylesheet, do: ExCSSModules.stylesheet(@stylesheet) - def class(key), do: ExCSSModules.class(@stylesheet, key) + def class(key), do: stylesheet() |> ExCSSModules.class(key) - def class_name(key), do: ExCSSModules.class_name(@stylesheet, key) + def class_name(key) do + ExCSSModules.class_name(stylesheet(), key) + end def class_name(key, value), do: - ExCSSModules.class_name(@stylesheet, key, value) + ExCSSModules.class_name(stylesheet(), key, value) - def class_selector(key), do: ExCSSModules.class_selector(@stylesheet, key) + def class_selector(key), do: ExCSSModules.class_selector(stylesheet(), key) end end end diff --git a/test/ex_css_modules/view_test.exs b/test/ex_css_modules/view_test.exs index ec54561..289e6f9 100644 --- a/test/ex_css_modules/view_test.exs +++ b/test/ex_css_modules/view_test.exs @@ -7,14 +7,33 @@ defmodule ExCSSModules.ViewTest do defmodule ViewModuleTest do use ExCSSModules.View, stylesheet: __ENV__.file - |> Path.dirname - |> Path.join("../support/stylesheet.css") + |> Path.dirname + |> Path.join("../support/stylesheet.css") + end + + defmodule EmbeddedViewModuleTest do + use ExCSSModules.View, stylesheet: __ENV__.file + |> Path.dirname + |> Path.join("../support/stylesheet.css"), + embed_stylesheet: true + end + describe "stylesheet_definition/0" do + test "gets the stylesheet string" do + assert ViewModuleTest.stylesheet_definition + == Path.expand(@example_stylesheet) + end + + test "gets the embedded stylesheet" do + assert EmbeddedViewModuleTest.stylesheet_definition + == ExCSSModules.stylesheet(@example_stylesheet) + end end describe "stylesheet/0" do test "calls the stylesheet" do - assert ViewModuleTest.stylesheet == ExCSSModules.stylesheet(@example_stylesheet) + assert ViewModuleTest.stylesheet + == ExCSSModules.stylesheet(@example_stylesheet) end end