Class: Proscenium::Builder

Inherits:
Object show all
Defined in:
lib/proscenium/builder.rb

Defined Under Namespace

Modules: Request Classes: BuildError, CompileError, ResolveError, Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: nil, base_url: nil) ⇒ Builder

Returns a new instance of Builder.



98
99
100
101
# File 'lib/proscenium/builder.rb', line 98

def initialize(root: nil, base_url: nil)
  @root = root || Rails.root
  @base_url = base_url
end

Class Method Details

.build_to_path(path, root: nil, base_url: nil) ⇒ Object



86
87
88
# File 'lib/proscenium/builder.rb', line 86

def self.build_to_path(path, root: nil, base_url: nil)
  new(root:, base_url:).build_to_path(path)
end

.build_to_string(path, root: nil, base_url: nil) ⇒ Object



90
91
92
# File 'lib/proscenium/builder.rb', line 90

def self.build_to_string(path, root: nil, base_url: nil)
  new(root:, base_url:).build_to_string(path)
end

.resolve(path, root: nil) ⇒ Object



94
95
96
# File 'lib/proscenium/builder.rb', line 94

def self.resolve(path, root: nil)
  new(root:).resolve(path)
end

Instance Method Details

#build_to_path(path) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/proscenium/builder.rb', line 103

def build_to_path(path)
  ActiveSupport::Notifications.instrument('build_to_path.proscenium',
                                          identifier: path,
                                          cached: Proscenium.cache.exist?(path)) do
    Proscenium.cache.fetch path do
      result = Request.build_to_path(path, @base_url, import_map, env_vars.to_json,
                                     @root.to_s,
                                     gem_root,
                                     Rails.env.to_sym,
                                     Proscenium.config.code_splitting,
                                     engines.to_json,
                                     Proscenium.config.debug)

      raise BuildError, result[:response] unless result[:success]

      result[:response]
    end
  end
end

#build_to_string(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/proscenium/builder.rb', line 123

def build_to_string(path)
  ActiveSupport::Notifications.instrument('build_to_string.proscenium', identifier: path) do
    result = Request.build_to_string(path, @base_url, import_map, env_vars.to_json,
                                     @root.to_s,
                                     gem_root,
                                     Rails.env.to_sym,
                                     Proscenium.config.code_splitting,
                                     engines.to_json,
                                     Proscenium.config.debug)

    raise BuildError, result[:response] unless result[:success]

    result[:response]
  end
end

#resolve(path) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/proscenium/builder.rb', line 139

def resolve(path)
  ActiveSupport::Notifications.instrument('resolve.proscenium', identifier: path) do
    result = Request.resolve(path, import_map, @root.to_s,
                             gem_root,
                             Rails.env.to_sym,
                             Proscenium.config.debug)
    raise ResolveError.new(path, result[:response]) unless result[:success]

    result[:response]
  end
end