class Isomorfeus::AssetManager::Asset

Attributes

bundle[R]
bundle_gz[R]
bundle_gz_size[R]
bundle_map[R]
bundle_map_gz[R]
bundle_size[R]
mtime[R]
mutex[R]
ruby_imports[R]
target[R]

Public Class Methods

new(target = :browser) click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 11
def initialize(target = :browser)
  @mutex = Mutex.new
  raise "Unknown asset target!" unless %i[browser node].include?(target)
  @target = target
  @bundled = false
  @ruby_compiled = false
  @js_imports = []
  @ruby_imports = []
end

Public Instance Methods

add_js_import(*args) click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 21
def add_js_import(*args)
  @js_imports << Isomorfeus::AssetManager::JsImport.new(*args)
end
add_ruby_import(*args) click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 25
def add_ruby_import(*args)
  @ruby_imports << Isomorfeus::AssetManager::RubyImport.new(*args)
end
bundle=(b) click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 29
def bundle=(b)  
  @bundled = true
  @bundle = b
  @bundle_size = @bundle.size
  unless @target == :node
    @bundle_gz = Zlib::gzip(b, level: Zlib::BEST_COMPRESSION) 
    @bundle_gz_size = @bundle_gz.size
  end
  @bundle
end
bundle_map=(m) click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 40
def bundle_map=(m)  
  @bundled = true
  @bundle_map = m
  @bundle_map_gz = Zlib::gzip(m, level: Zlib::BEST_COMPRESSION) unless @target == :node
  @bundle_map
end
bundled?() click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 47
def bundled?
  @bundled
end
ruby_modules() click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 51
def ruby_modules
  @ruby_imports.map(&:module_name)
end
to_s() click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 59
      def to_s
        js = @target == :node ? '' : "if (typeof globalThis !== 'undefined' && typeof global === 'undefined') { globalThis.global = globalThis; }\n"
        unless @js_imports.empty?
          js << "#{@js_imports.map(&:to_s).join("\n")}"
        end
        js << "\n" if !@js_imports.empty? && !@ruby_imports.empty?
        unless @ruby_imports.empty?
          if Isomorfeus.development? && @target == :browser
            js << <<~JAVASCRIPT
            // Isomorfeus Asset Manager HMR code begin
            let ws_protocol = (window.location.protocol == 'https:') ? 'wss:' : 'ws:';
            let ws_url = ws_protocol + '//' + window.location.host + "#{Isomorfeus.assets_websocket_path}";
            let hmr_ws = new WebSocket(ws_url);
            hmr_ws.onmessage = function(event) {
              let update = JSON.parse(event.data);
              if (typeof update.error !== 'undefined') { console.error(update.error); return; }
              let start_index = '/* Generated by Opal 1.2.0 */\\nOpal.modules[\\"'.length;
              let end_index = update.javascript.indexOf('"', start_index);
              let opal_module_name = update.javascript.substr(start_index, end_index - start_index);
              console.log('Updating ', opal_module_name);
              if (typeof Opal !== 'undefined' && typeof Opal.require_table !== "undefined" && Opal.require_table['corelib/module']) {
                try {
                  eval(update.javascript);
                  if (Opal.require_table[opal_module_name]) { Opal.load.call(Opal, opal_module_name); } 
                  else { Opal.require.call(Opal, opal_module_name); }
                  Opal.Isomorfeus.$force_render();
                } catch (e) { console.error(e); return; }
              }
            }
            // Isomorfeus Asset Manager HMR code end
            JAVASCRIPT
          end
          js << "#{@ruby_imports.map(&:to_s).join("\n")}"
        end
        js
      end
touch() click to toggle source
# File lib/isomorfeus/asset_manager/asset.rb, line 55
def touch
  @mtime = Time.now
end