Class: Proscenium::Builder

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

Defined Under Namespace

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

Constant Summary collapse

ENVIRONMENTS =
{ development: 1, test: 2, production: 3 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Builder.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/proscenium/builder.rb', line 80

def initialize(root: nil, bundle: nil)
  bundle = Proscenium.config.bundle if bundle.nil?

  @request_config = FFI::MemoryPointer.from_string({
    RootPath: (root || Rails.root).to_s,
    GemPath: gem_root,
    Environment: ENVIRONMENTS.fetch(Rails.env.to_sym, 2),
    Engines: Proscenium.config.engines,
    EnvVars: env_vars,
    CodeSplitting: Proscenium.config.code_splitting,
    ExternalNodeModules: Proscenium.config.external_node_modules,
    Bundle: bundle,
    Debug: Proscenium.config.debug
  }.to_json)
end

Class Method Details

.build_to_path(path, root: nil) ⇒ Object



63
64
65
# File 'lib/proscenium/builder.rb', line 63

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

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



67
68
69
# File 'lib/proscenium/builder.rb', line 67

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

.reset_config!Object

Intended for tests only.



76
77
78
# File 'lib/proscenium/builder.rb', line 76

def self.reset_config!
  Request.reset_config
end

.resolve(path, root: nil) ⇒ Object



71
72
73
# File 'lib/proscenium/builder.rb', line 71

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

Instance Method Details

#build_to_path(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/proscenium/builder.rb', line 96

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, @request_config)

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

      result[:response]
    end
  end
end

#build_to_string(path) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/proscenium/builder.rb', line 110

def build_to_string(path)
  ActiveSupport::Notifications.instrument('build_to_string.proscenium', identifier: path) do
    result = Request.build_to_string(path, @request_config)

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

    result[:response]
  end
end

#resolve(path) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/proscenium/builder.rb', line 120

def resolve(path)
  ActiveSupport::Notifications.instrument('resolve.proscenium', identifier: path) do
    result = Request.resolve(path, @request_config)

    raise ResolveError.new(path, result[:response]) unless result[:success]

    result[:response]
  end
end