From b1318ecba7af0a026e331f3c652d6da098028dcb Mon Sep 17 00:00:00 2001 From: Marlus Saraiva Date: Wed, 18 Sep 2024 16:16:19 -0300 Subject: [PATCH] Add compiler option :generate_assets --- lib/mix/tasks/compile/surface.asset_generator.ex | 10 ++++++++++ lib/mix/tasks/compile/surface.ex | 5 +++++ .../tasks/compile/surface.asset_generator_test.exs | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/lib/mix/tasks/compile/surface.asset_generator.ex b/lib/mix/tasks/compile/surface.asset_generator.ex index fcd0c237..ebbf384c 100644 --- a/lib/mix/tasks/compile/surface.asset_generator.ex +++ b/lib/mix/tasks/compile/surface.asset_generator.ex @@ -12,6 +12,16 @@ defmodule Mix.Tasks.Compile.Surface.AssetGenerator do @hooks_extension "#{@hooks_tag}.{#{@supported_hooks_extensions}}" def run(components, opts \\ []) do + generate_assets? = Keyword.get(opts, :generate_assets, true) + + if generate_assets? do + do_run(components, opts) + else + [] + end + end + + defp do_run(components, opts) do components = Enum.sort(components, :desc) hooks_output_dir = Keyword.get(opts, :hooks_output_dir, @default_hooks_output_dir) css_output_file = Keyword.get(opts, :css_output_file, @default_css_output_file) diff --git a/lib/mix/tasks/compile/surface.ex b/lib/mix/tasks/compile/surface.ex index e33e7910..f56f908d 100644 --- a/lib/mix/tasks/compile/surface.ex +++ b/lib/mix/tasks/compile/surface.ex @@ -21,6 +21,10 @@ defmodule Mix.Tasks.Compile.Surface do ### Options + * `generate_assets` - instructs the compiler to generate components' css and js files. + Set it to `false` when developing a library of components that doesn't require any CSS + style nor JS hooks. Default is `true`. + * `hooks_output_dir` - defines the folder where the compiler generates the JS hooks files. Default is `./assets/js/_hooks/`. @@ -148,6 +152,7 @@ defmodule Mix.Tasks.Compile.Surface do ] @assets_opts [ + :generate_assets, :hooks_output_dir, :css_output_file, :enable_variants, diff --git a/test/mix/tasks/compile/surface.asset_generator_test.exs b/test/mix/tasks/compile/surface.asset_generator_test.exs index e91398cc..bfad6d1d 100644 --- a/test/mix/tasks/compile/surface.asset_generator_test.exs +++ b/test/mix/tasks/compile/surface.asset_generator_test.exs @@ -374,6 +374,18 @@ defmodule Mix.Tasks.Compile.Surface.AssetGeneratorTest do refute File.exists?(@link_dest_hooks_file) end + test "don't generate assets if :generate_assets is false", %{opts: opts} do + refute File.exists?(@css_output_file) + refute File.exists?(@variants_output_file) + refute File.exists?(@hooks_output_dir) + + assert run(@components, [generate_assets: false] ++ opts) == [] + + refute File.exists?(@css_output_file) + refute File.exists?(@variants_output_file) + refute File.exists?(@hooks_output_dir) + end + test "returns diagnostic if component has more then 1 hook and uses the first one", %{opts: opts} do File.write!(@other_link_src_hooks_file, @link_src_hooks_file_content) refute File.exists?(@hooks_output_dir)