~mika/www.m1kadev.nl

ref: e5ef6146526cba076e1901f7d93010d1f36fe935 www.m1kadev.nl/kethel/lib/statics.ex -rw-r--r-- 1.1 KiB
e5ef6146 — mika Merge pull request #1 from m1kadev/kethel 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule Statics do
  @moduledoc """
    Statics is an exception, because we only copy the files. Therefore, the performance gain from reading files into memory for processing isn't worth it. The compile fuction gets called at load-time for all other asset types.
  """

  @spec ensure_file_structure(binary()) :: :ok
  defp ensure_file_structure(project_root) do
    File.mkdir_p!("#{project_root}/build/static/")
  end

  defp trim_abs_path(path, project_root) do
    # ROOT + static
    begin = byte_size(project_root) + byte_size("static/")
    # remove extension
    binary_part(path, begin, byte_size(path) - begin)
  end

  # we dont async_stream here because copying is (relatively speaking) inexpensive, and we want to keep more threads open for other (more expensive) compilation processes
  def compile(project_root) do
    ensure_file_structure(project_root)

    for path <- Path.wildcard("#{project_root}/static/**/*") do
      rel_path = trim_abs_path(path, project_root)
      output_path = "#{project_root}/build/static#{rel_path}"

      File.copy!(path, output_path)

      IO.puts("[ COPIED ] #{path} -> #{output_path}")
    end
  end
end