Class: Isomorfeus::AssetManager::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/asset_manager/asset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = :browser) ⇒ Asset

Returns a new instance of Asset.



15
16
17
18
19
20
21
22
23
24
# File 'lib/isomorfeus/asset_manager/asset.rb', line 15

def initialize(target = :browser)
  @mutex = Mutex.new
  raise "Unknown asset target!" unless i[browser node].include?(target)
  @target = target
  @bundled = false
  @css_bundled = false
  @ruby_compiled = false
  @js_imports = []
  @ruby_imports = []
end

Instance Attribute Details

#bundleObject

Returns the value of attribute bundle.



4
5
6
# File 'lib/isomorfeus/asset_manager/asset.rb', line 4

def bundle
  @bundle
end

#bundle_gzObject (readonly)

Returns the value of attribute bundle_gz.



5
6
7
# File 'lib/isomorfeus/asset_manager/asset.rb', line 5

def bundle_gz
  @bundle_gz
end

#bundle_gz_sizeObject (readonly)

Returns the value of attribute bundle_gz_size.



5
6
7
# File 'lib/isomorfeus/asset_manager/asset.rb', line 5

def bundle_gz_size
  @bundle_gz_size
end

#bundle_mapObject

Returns the value of attribute bundle_map.



6
7
8
# File 'lib/isomorfeus/asset_manager/asset.rb', line 6

def bundle_map
  @bundle_map
end

#bundle_map_gzObject (readonly)

Returns the value of attribute bundle_map_gz.



7
8
9
# File 'lib/isomorfeus/asset_manager/asset.rb', line 7

def bundle_map_gz
  @bundle_map_gz
end

#bundle_map_gz_sizeObject (readonly)

Returns the value of attribute bundle_map_gz_size.



7
8
9
# File 'lib/isomorfeus/asset_manager/asset.rb', line 7

def bundle_map_gz_size
  @bundle_map_gz_size
end

#bundle_map_sizeObject (readonly)

Returns the value of attribute bundle_map_size.



6
7
8
# File 'lib/isomorfeus/asset_manager/asset.rb', line 6

def bundle_map_size
  @bundle_map_size
end

#bundle_sizeObject (readonly)

Returns the value of attribute bundle_size.



4
5
6
# File 'lib/isomorfeus/asset_manager/asset.rb', line 4

def bundle_size
  @bundle_size
end

#cssObject

Returns the value of attribute css.



8
9
10
# File 'lib/isomorfeus/asset_manager/asset.rb', line 8

def css
  @css
end

#css_gzObject (readonly)

Returns the value of attribute css_gz.



9
10
11
# File 'lib/isomorfeus/asset_manager/asset.rb', line 9

def css_gz
  @css_gz
end

#css_gz_sizeObject (readonly)

Returns the value of attribute css_gz_size.



9
10
11
# File 'lib/isomorfeus/asset_manager/asset.rb', line 9

def css_gz_size
  @css_gz_size
end

#css_mapObject

Returns the value of attribute css_map.



10
11
12
# File 'lib/isomorfeus/asset_manager/asset.rb', line 10

def css_map
  @css_map
end

#css_map_gzObject (readonly)

Returns the value of attribute css_map_gz.



11
12
13
# File 'lib/isomorfeus/asset_manager/asset.rb', line 11

def css_map_gz
  @css_map_gz
end

#css_map_gz_sizeObject (readonly)

Returns the value of attribute css_map_gz_size.



11
12
13
# File 'lib/isomorfeus/asset_manager/asset.rb', line 11

def css_map_gz_size
  @css_map_gz_size
end

#css_map_sizeObject (readonly)

Returns the value of attribute css_map_size.



10
11
12
# File 'lib/isomorfeus/asset_manager/asset.rb', line 10

def css_map_size
  @css_map_size
end

#css_sizeObject (readonly)

Returns the value of attribute css_size.



8
9
10
# File 'lib/isomorfeus/asset_manager/asset.rb', line 8

def css_size
  @css_size
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



12
13
14
# File 'lib/isomorfeus/asset_manager/asset.rb', line 12

def mtime
  @mtime
end

#mutexObject (readonly)

Returns the value of attribute mutex.



13
14
15
# File 'lib/isomorfeus/asset_manager/asset.rb', line 13

def mutex
  @mutex
end

#ruby_importsObject (readonly)

Returns the value of attribute ruby_imports.



12
13
14
# File 'lib/isomorfeus/asset_manager/asset.rb', line 12

def ruby_imports
  @ruby_imports
end

#targetObject (readonly)

Returns the value of attribute target.



12
13
14
# File 'lib/isomorfeus/asset_manager/asset.rb', line 12

def target
  @target
end

Instance Method Details

#add_js_import(*args) ⇒ Object



26
27
28
# File 'lib/isomorfeus/asset_manager/asset.rb', line 26

def add_js_import(*args)
  @js_imports << Isomorfeus::AssetManager::JsImport.new(*args)
end

#add_ruby_import(*args) ⇒ Object



30
31
32
# File 'lib/isomorfeus/asset_manager/asset.rb', line 30

def add_ruby_import(*args)
  @ruby_imports << Isomorfeus::AssetManager::RubyImport.new(*args)
end

#bundled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/isomorfeus/asset_manager/asset.rb', line 55

def bundled?
  @bundled
end

#has_css?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/isomorfeus/asset_manager/asset.rb', line 87

def has_css?
  @has_css
end

#ruby_modulesObject



91
92
93
# File 'lib/isomorfeus/asset_manager/asset.rb', line 91

def ruby_modules
  @ruby_imports.map(&:module_name)
end

#to_sObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/isomorfeus/asset_manager/asset.rb', line 99

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 << "      // Isomorfeus Asset Manager HMR code begin\n      let ws_protocol = (window.location.protocol == 'https:') ? 'wss:' : 'ws:';\n      let ws_url = ws_protocol + '//' + window.location.host + \"\#{Isomorfeus.assets_websocket_path}\";\n      let hmr_ws = new WebSocket(ws_url);\n      hmr_ws.onmessage = function(event) {\n        let update = JSON.parse(event.data);\n        if (typeof update.error !== 'undefined') { console.error(update.error); return; }\n        let start_index = 'Opal.modules[\\\\\"'.length;\n        let end_index = update.javascript.indexOf('\"', start_index);\n        let opal_module_name = update.javascript.substr(start_index, end_index - start_index);\n        console.log('Updating ', opal_module_name);\n        if (typeof Opal !== 'undefined' && typeof Opal.require_table !== \"undefined\" && Opal.require_table['corelib/module']) {\n          try {\n            eval(update.javascript);\n            if (Opal.require_table[opal_module_name]) { Opal.load.call(Opal, opal_module_name); }\n            else { Opal.require.call(Opal, opal_module_name); }\n            Opal.Isomorfeus.$force_render();\n          } catch (e) { console.error(e); return; }\n        }\n      }\n      // Isomorfeus Asset Manager HMR code end\n      JAVASCRIPT\n    end\n    js << \"\#{@ruby_imports.map(&:to_s).join(\"\\n\")}\"\n  end\n  js\nend\n"

#touchObject



95
96
97
# File 'lib/isomorfeus/asset_manager/asset.rb', line 95

def touch
  @mtime = (Time.now.to_f * (10 ** 9)).to_i
end