Class: Opal::Builder

Inherits:
Object
  • Object
show all
Includes:
UseGem
Defined in:
opal/lib/opal/builder.rb

Defined Under Namespace

Classes: MissingRequire, ProcessorNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UseGem

#use_gem

Constructor Details

#initialize(options = nil) ⇒ Builder

Returns a new instance of Builder.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'opal/lib/opal/builder.rb', line 65

def initialize(options = nil)
  (options || {}).each_pair do |k, v|
    public_send("#{k}=", v)
  end

  @stubs                    ||= []
  @preload                  ||= []
  @processors               ||= ::Opal::Builder.processors
  @path_reader              ||= PathReader.new(Opal.paths, extensions.map { |e| [".#{e}", ".js.#{e}"] }.flatten)
  @prerequired              ||= []
  @compiler_options         ||= Opal::Config.compiler_options
  @missing_require_severity ||= Opal::Config.missing_require_severity
  @cache                    ||= Opal.cache
  @scheduler                ||= Opal.builder_scheduler

  if @scheduler.respond_to? :new
    @scheduler = @scheduler.new(self)
  end

  @processed = []
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def cache
  @cache
end

#compiler_optionsObject

Returns the value of attribute compiler_options.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def compiler_options
  @compiler_options
end

#missing_require_severityObject

Returns the value of attribute missing_require_severity.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def missing_require_severity
  @missing_require_severity
end

#path_readerObject

Returns the value of attribute path_reader.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def path_reader
  @path_reader
end

#preloadObject

Returns the value of attribute preload.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def preload
  @preload
end

#prerequiredObject

Returns the value of attribute prerequired.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def prerequired
  @prerequired
end

#processedObject (readonly)

Returns the value of attribute processed.



176
177
178
# File 'opal/lib/opal/builder.rb', line 176

def processed
  @processed
end

#processorsObject

Returns the value of attribute processors.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def processors
  @processors
end

#schedulerObject

Returns the value of attribute scheduler.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def scheduler
  @scheduler
end

#stubsObject

Returns the value of attribute stubs.



178
179
180
# File 'opal/lib/opal/builder.rb', line 178

def stubs
  @stubs
end

Class Method Details

.build(*args, &block) ⇒ Object



87
88
89
# File 'opal/lib/opal/builder.rb', line 87

def self.build(*args, &block)
  new.build(*args, &block)
end

.extensionsObject

All the extensions supported by registered processors



18
19
20
# File 'opal/lib/opal/builder.rb', line 18

def self.extensions
  @extensions ||= []
end

.processorsObject

The registered processors



13
14
15
# File 'opal/lib/opal/builder.rb', line 13

def self.processors
  @processors ||= []
end

.register_processor(processor, processor_extensions) ⇒ Object

Register a builder processor and the supported extensions. A processor will respond to:

#requires

An array of string containing the logic paths of required assets

#required_trees

An array of string containing the logic paths of required directories

#autoloads

An array of entities that are autoloaded and their compile-time load failure can be safely ignored

#to_s

The processed source

#source_map

An instance of ::Opal::SourceMap::File representing the processd asset's source map.

.new(source, filename, compiler_options)

The processor will be instantiated passing:

  • the unprocessed source
  • the asset's filename
  • Opal's compiler options

.match?(path)

The processor is able to recognize paths suitable for its type of processing.



53
54
55
56
57
# File 'opal/lib/opal/builder.rb', line 53

def self.register_processor(processor, processor_extensions)
  return if processors.include?(processor)
  processors << processor
  processor_extensions.each { |ext| extensions << ext }
end

Instance Method Details

#already_processedObject



170
171
172
# File 'opal/lib/opal/builder.rb', line 170

def already_processed
  @already_processed ||= Set.new
end

#append_paths(*paths) ⇒ Object



137
138
139
# File 'opal/lib/opal/builder.rb', line 137

def append_paths(*paths)
  path_reader.append_paths(*paths)
end

#build(path, options = {}) ⇒ Object



91
92
93
# File 'opal/lib/opal/builder.rb', line 91

def build(path, options = {})
  build_str(source_for(path), path, options)
end

#build_require(path, options = {}) ⇒ Object



112
113
114
# File 'opal/lib/opal/builder.rb', line 112

def build_require(path, options = {})
  process_require(path, [], options)
end

#build_str(source, rel_path, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'opal/lib/opal/builder.rb', line 100

def build_str(source, rel_path, options = {})
  return if source.nil?
  abs_path = expand_path(rel_path)
  rel_path = expand_ext(rel_path)
  asset = processor_for(source, rel_path, abs_path, false, options)
  requires = preload + asset.requires + tree_requires(asset, abs_path)
  # Don't automatically load modules required by the module
  process_requires(rel_path, requires, asset.autoloads, options.merge(load: false))
  processed << asset
  self
end

#dependent_filesObject

Return a list of dependent files, for watching purposes



196
197
198
# File 'opal/lib/opal/builder.rb', line 196

def dependent_files
  processed.map(&:abs_path).compact.select { |fn| File.exist?(fn) }
end

#esm?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'opal/lib/opal/builder.rb', line 181

def esm?
  @compiler_options[:esm]
end

#expand_ext(path) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'opal/lib/opal/builder.rb', line 200

def expand_ext(path)
  abs_path = path_reader.expand(path)

  if abs_path
    File.join(
      File.dirname(path),
      File.basename(abs_path)
    )
  else
    path
  end
end

#initialize_copy(other) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'opal/lib/opal/builder.rb', line 116

def initialize_copy(other)
  super
  @stubs = other.stubs.dup
  @preload = other.preload.dup
  @processors = other.processors.dup
  @path_reader = other.path_reader.dup
  @prerequired = other.prerequired.dup
  @compiler_options = other.compiler_options.dup
  @missing_require_severity = other.missing_require_severity.to_sym
  @processed = other.processed.dup
  @scheduler = other.scheduler.dup.tap { |i| i.builder = self }
end

#output_extensionObject

Output extension, to be used by runners. At least Node.JS switches to ESM mode only if the extension is "mjs"



187
188
189
190
191
192
193
# File 'opal/lib/opal/builder.rb', line 187

def output_extension
  if esm?
    'mjs'
  else
    'js'
  end
end

#process_require(rel_path, autoloads, options) ⇒ Object



163
164
165
166
167
168
# File 'opal/lib/opal/builder.rb', line 163

def process_require(rel_path, autoloads, options)
  return if already_processed.include?(rel_path)
  already_processed << rel_path
  asset = process_require_threadsafely(rel_path, autoloads, options)
  processed << asset if asset
end

#process_require_threadsafely(rel_path, autoloads, options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'opal/lib/opal/builder.rb', line 141

def process_require_threadsafely(rel_path, autoloads, options)
  return if prerequired.include?(rel_path)

  autoload = autoloads.include? rel_path

  source = stub?(rel_path) ? '' : read(rel_path, autoload)

  # The handling is delegated to the runtime
  return if source.nil?

  abs_path = expand_path(rel_path)
  rel_path = expand_ext(rel_path)
  asset = processor_for(source, rel_path, abs_path, autoload, options.merge(requirable: true))
  process_requires(
    rel_path,
    asset.requires + tree_requires(asset, abs_path),
    asset.autoloads,
    options
  )
  asset
end

#source_for(path) ⇒ Object

Retrieve the source for a given path the same way #build would do.



96
97
98
# File 'opal/lib/opal/builder.rb', line 96

def source_for(path)
  read(path, false)
end

#source_mapObject



133
134
135
# File 'opal/lib/opal/builder.rb', line 133

def source_map
  ::Opal::SourceMap::Index.new(processed.map(&:source_map), join: "\n")
end

#to_sObject



129
130
131
# File 'opal/lib/opal/builder.rb', line 129

def to_s
  processed.map(&:to_s).join("\n")
end