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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'opal/lib/opal/builder.rb', line 60

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

  @processed = []
end

Instance Attribute Details

#compiler_optionsObject

Returns the value of attribute compiler_options



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def compiler_options
  @compiler_options
end

#path_readerObject

Returns the value of attribute path_reader



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def path_reader
  @path_reader
end

#preloadObject

Returns the value of attribute preload



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def preload
  @preload
end

#prerequiredObject

Returns the value of attribute prerequired



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def prerequired
  @prerequired
end

#processedObject (readonly)

Returns the value of attribute processed



124
125
126
# File 'opal/lib/opal/builder.rb', line 124

def processed
  @processed
end

#processorsObject

Returns the value of attribute processors



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def processors
  @processors
end

#stubsObject

Returns the value of attribute stubs



126
127
128
# File 'opal/lib/opal/builder.rb', line 126

def stubs
  @stubs
end

Class Method Details

.build(*args, &block) ⇒ Object



75
76
77
# File 'opal/lib/opal/builder.rb', line 75

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

.extensionsObject

All the extensions supported by registered processors



15
16
17
# File 'opal/lib/opal/builder.rb', line 15

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

.processorsObject

The registered processors



10
11
12
# File 'opal/lib/opal/builder.rb', line 10

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

#to_s

The processed source

#source_map

An instance of ::SourceMap::Map 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.



46
47
48
49
50
# File 'opal/lib/opal/builder.rb', line 46

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

Instance Method Details

#append_paths(*paths) ⇒ Object



118
119
120
# File 'opal/lib/opal/builder.rb', line 118

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

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



79
80
81
82
# File 'opal/lib/opal/builder.rb', line 79

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

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



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

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

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



84
85
86
87
88
89
90
91
92
93
# File 'opal/lib/opal/builder.rb', line 84

def build_str source, filename, options = {}
  path = path_from_filename(filename)
  asset = processor_for(source, filename, path, options)
  requires = preload + asset.requires + tree_requires(asset, path)
  requires.map { |r| process_require(r, options) }
  processed << asset
  self
rescue MissingRequire => error
  raise error, "A file required by #{filename.inspect} wasn't found.\n#{error.message}", error.backtrace
end

#initialize_copy(other) ⇒ Object



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

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
  @processed = other.processed.dup
end

#source_mapObject



114
115
116
# File 'opal/lib/opal/builder.rb', line 114

def source_map
  processed.map(&:source_map).reduce(:+).as_json.to_json
end

#to_sObject



110
111
112
# File 'opal/lib/opal/builder.rb', line 110

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