Class: Wedge::Opal::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/wedge/opal.rb

Instance Method Summary collapse

Instance Method Details

#compileObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wedge/opal.rb', line 20

def compile
  @result = original_compile

  if defined? Wedge
    logical_path = self.file
    classes    = Wedge.config.component_class
    comp_class = classes["#{Wedge.config.app_dir}/#{logical_path}".gsub(/\//, '__')] || classes[logical_path.gsub(/\//, '__')]

    if logical_path == 'wedge'
      compiled_data = Base64.encode64 Wedge.config.client_data.to_json
      # We need to merge in some data that is only set on the server.
      # i.e. path, assets_key etc....
      @result << Opal.original_compile("require '#{self.file}'; Wedge.config.data = HashObject.new(Wedge.config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")
      # load all global plugins into wedge
      Wedge.config.plugins.each do |path|
        @result << Builder.build(path).to_s
      end
    elsif comp_class
      comp_class.config.on_compile.each { |blk| comp_class.instance_exec(true, &blk) }
      comp_name     = comp_class.config.name
      compiled_data = Base64.encode64 comp_class.config.client_data.to_json
      js            = ''

      js << "require '#{self.file}'; Wedge.config.component_class[:#{comp_name}].config.data = HashObject.new(Wedge.config.component_class[:#{comp_name}].config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))"
      # todo: discuss: pass plugin settings that were set server side?
      js << "; Wedge.plugin(:#{comp_name.to_s.gsub(/_plugin$/, '')})" if comp_class.config.is_plugin

      @result << Opal.original_compile(js)

      if compile_str = comp_class.config.compile_str
        @result << compile_str
      end

      load_requires logical_path
    end
  end

  @result
end

#load_requires(path_name) ⇒ Object



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

def load_requires path_name
  if requires = Wedge.config.requires[path_name.gsub(/\//, '__')]
    requires.each do |path|
      next unless comp_class = Wedge.config.component_class[path]

      comp_class.config.on_compile.each { |blk| comp_class.instance_eval(&blk) }

      comp_name     = comp_class.config.name
      compiled_data = Base64.encode64 comp_class.config.client_data.to_json

      load_requires path

      @result << Opal.original_compile("require '#{path}'; Wedge.config.component_class[:#{comp_name}].config.data = HashObject.new(Wedge.config.component_class[:#{comp_name}].config.data.to_h.merge JSON.parse(Base64.decode64('#{compiled_data}')))")
    end
  end
end

#original_compileObject



19
# File 'lib/wedge/opal.rb', line 19

alias_method :original_compile, :compile