Skip to content

Commit

Permalink
Add compiler option :generate_assets
Browse files Browse the repository at this point in the history
  • Loading branch information
msaraiva committed Sep 18, 2024
1 parent 1f105ed commit b1318ec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/mix/tasks/compile/surface.asset_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/mix/tasks/compile/surface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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/`.
Expand Down Expand Up @@ -148,6 +152,7 @@ defmodule Mix.Tasks.Compile.Surface do
]

@assets_opts [
:generate_assets,
:hooks_output_dir,
:css_output_file,
:enable_variants,
Expand Down
12 changes: 12 additions & 0 deletions test/mix/tasks/compile/surface.asset_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b1318ec

Please sign in to comment.