Module: Proscenium::SideLoad

Defined in:
lib/proscenium/side_load.rb

Defined Under Namespace

Modules: Monkey Classes: NotFound

Constant Summary collapse

DEFAULT_EXTENSIONS =
%i[js css].freeze
EXTENSIONS =
%i[js css].freeze

Class Method Summary collapse

Class Method Details

.append(path, *extensions) ⇒ Object

Side load the given asset `path`, by appending it to `Proscenium::Current.loaded`, which is a Set of 'js' and 'css' asset paths. This is safe to call multiple times, as it uses Set's. Meaning that side loading will never include duplicates.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/proscenium/side_load.rb', line 24

def append(path, *extensions)
  Proscenium::Current.loaded ||= EXTENSIONS.to_h { |e| [e, Set[]] }

  unless (unknown_extensions = extensions.difference(EXTENSIONS)).empty?
    raise ArgumentError, "unsupported extension(s): #{unknown_extensions.join(',')}"
  end

  loaded_types = []

  (extensions.empty? ? DEFAULT_EXTENSIONS : extensions).each do |ext|
    path_with_ext = "#{path}.#{ext}"
    ext = ext.to_sym

    next if Proscenium::Current.loaded[ext].include?(path_with_ext)
    next unless Rails.root.join(path_with_ext).exist?

    Proscenium::Current.loaded[ext] << path_with_ext
    loaded_types << ext
  end

  !loaded_types.empty? && Rails.logger.debug do
    "[Proscenium] Side loaded /#{path}.(#{loaded_types.join(',')})"
  end
end

.append!(pathname) ⇒ Object

Like #append, but only accepts a single `path` argument, which must be a Pathname. Raises `NotFound` if path does not exist,

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/proscenium/side_load.rb', line 51

def append!(pathname)
  Proscenium::Current.loaded ||= EXTENSIONS.to_h { |e| [e, Set[]] }

  unless pathname.is_a?(Pathname)
    raise ArgumentError, "Argument `pathname` (#{pathname}) must be a Pathname"
  end

  ext = pathname.extname.sub('.', '').to_sym
  path = pathname.relative_path_from(Rails.root).to_s

  raise ArgumentError, "unsupported extension: #{ext}" unless EXTENSIONS.include?(ext)

  return if Proscenium::Current.loaded[ext].include?(path)

  raise NotFound, path unless pathname.exist?

  Proscenium::Current.loaded[ext] << path

  Rails.logger.debug "[Proscenium] Side loaded /#{path}"
end