Class: Isomorfeus::AssetManager::BrowserAsset
- Inherits:
-
Asset
- Object
- Asset
- Isomorfeus::AssetManager::BrowserAsset
show all
- Defined in:
- lib/isomorfeus/asset_manager/browser_asset.rb
Instance Attribute Summary
Attributes inherited from Asset
#bundle_ruby_modules, #hash, #js_imports, #mtime, #mutex, #ruby_imports, #target
Class Method Summary
collapse
Instance Method Summary
collapse
-
#browser? ⇒ Boolean
-
#build_ruby_and_save(asset_key, asset_name, imports_path) ⇒ Object
-
#bundle_css(asset_name, output_path) ⇒ Object
-
#bundle_css_map(asset_name, output_path) ⇒ Object
-
#bundle_js(asset_key, asset_name, imports_path, output_path) ⇒ Object
-
#bundle_js_map(asset_key, output_path) ⇒ Object
-
#generate_entry(asset_name) ⇒ Object
-
#initialize(target = :browser) ⇒ BrowserAsset
constructor
A new instance of BrowserAsset.
-
#node? ⇒ Boolean
-
#ruby_imports_to_s(asset_name, out_dir) ⇒ Object
-
#save_entry(asset_key, asset_name, imports_path) ⇒ Object
Methods inherited from Asset
_original_new, #add_js_import, #add_ruby_import, #bundle, #bundled!, #bundled?, #touch, #unbundle!
Constructor Details
#initialize(target = :browser) ⇒ BrowserAsset
Returns a new instance of BrowserAsset.
8
9
10
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 8
def initialize(target = :browser)
super(:browser)
end
|
Class Method Details
.new ⇒ Object
4
5
6
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 4
def self.new
_original_new
end
|
Instance Method Details
#browser? ⇒ Boolean
12
13
14
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 12
def browser?
true
end
|
#build_ruby_and_save(asset_key, asset_name, imports_path) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 20
def build_ruby_and_save(asset_key, asset_name, imports_path)
@ruby_imports.each do |ruby_import|
module_name = ruby_import.module_name
asset_dir = File.join(imports_path, asset_name)
out_file = File.join(asset_dir, "#{module_name}.rb.js")
next if Isomorfeus.production? && File.exist?(out_file)
result = Opal::Builder.build(ruby_import.resolved_path, { esm: true })
js = result.to_s
if Isomorfeus.production?
FileUtils.mkdir_p(asset_dir) unless Dir.exist?(asset_dir)
FileUtils.mkdir_p(File.join(*[asset_dir].concat(module_name.split('/')[0...-1]))) if module_name.include?('/')
File.binwrite(out_file, js)
else
js << "\n//# sourceMappingURL=#{Isomorfeus.assets_path}/#{asset_name}/#{module_name}.rb.js.map\n"
@bundle_ruby_modules[module_name] = { js: { raw: js }, map: { raw: Oj.dump(result.source_map.as_json, mode: :strict) }}
end
end
end
|
#bundle_css(asset_name, output_path) ⇒ Object
45
46
47
48
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 45
def bundle_css(asset_name, output_path)
filename = File.join(output_path, asset_name + '.css')
@hash[:css][:css] = { raw: File.binread(filename) } if File.exist?(filename)
end
|
#bundle_css_map(asset_name, output_path) ⇒ Object
50
51
52
53
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 50
def bundle_css_map(asset_name, output_path)
filename = File.join(output_path, asset_name + '.css.map')
@hash[:css][:map] = { raw: File.binread(filename) } if File.exist?(filename)
end
|
#bundle_js(asset_key, asset_name, imports_path, output_path) ⇒ Object
55
56
57
58
59
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 55
def bundle_js(asset_key, asset_name, imports_path, output_path)
js = File.binread(File.join(output_path, asset_key))
js << ruby_imports_to_s(asset_name, imports_path) unless Isomorfeus.production?
@hash[:js][:js] = { raw: js }
end
|
#bundle_js_map(asset_key, output_path) ⇒ Object
61
62
63
64
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 61
def bundle_js_map(asset_key, output_path)
filename = File.join(output_path, asset_key + '.map')
@hash[:js][:map] = { raw: File.binread(filename) } if File.exist?(filename)
end
|
#generate_entry(asset_name) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
135
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 76
def generate_entry(asset_name)
js = "if (typeof globalThis !== 'undefined' && typeof global === 'undefined') { globalThis.global = globalThis; }\n"
js << "#{@js_imports.map(&:to_s).join("\n")}"
if @ruby_imports.any?
if Isomorfeus.development?
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.hmr_websocket_path}";
let hmr_ws_fun = function() {
let hmr_ws = new WebSocket(ws_url);
hmr_ws.onopen = function(event) { console.log("Isomorfeus Asset Manager HMR socket connected"); }
hmr_ws.onmessage = function(event) {
let update = JSON.parse(event.data);
if (typeof update.error !== 'undefined') { console.error(update.error); return; }
if (typeof update.locale !== 'undefined') {
console.log('Isomorfeus Asset Manager updating locale ', update.locale);
try {
let domain = Opal.Isomorfeus.$i18n_domain();
let i18n_state = Opal.Isomorfeus.store.$dig('i18n_state');
i18n_state["$[]"](domain)["$[]="](update.locale, Opal.hash());
Opal.Isomorfeus.store.$deferred_dispatch(Opal.hash({type: 'I18N_STATE', set_state: i18n_state}));
Opal.Isomorfeus.$force_render();
} catch { console.log('Isomorfeus Asset Manager could not update locale ', update.locale) }
return;
}
if (typeof update.reload !== 'undefined') {
console.log('Isomorfeus Asset Manager reloading page, updating javascript import ', update.reload);
setTimeout(function() { window.location.reload(true); }, 500);
return;
}
let start_index = 'Opal.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('Isomorfeus Asset Manager updating ', opal_module_name);
if (typeof Opal !== 'undefined' && typeof Opal.require_table !== "undefined" && Opal.require_table['corelib/module']) {
try {
window.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; }
}
};
hmr_ws.onclose = function(event) {
setTimeout(function() {
console.log("Isomorfeus Asset Manager reconnecting HMR socket");
hmr_ws_fun();
}, 2000);
};
}
hmr_ws_fun()
// Isomorfeus Asset Manager HMR code end
JAVASCRIPT
elsif Isomorfeus.production?
js << "#{@ruby_imports.map { |i| i.to_s(asset_name) }.join("\n")}"
end
end
js
end
|
#node? ⇒ Boolean
16
17
18
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 16
def node?
false
end
|
#ruby_imports_to_s(asset_name, out_dir) ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 66
def ruby_imports_to_s(asset_name, out_dir)
s = @ruby_imports.size - 1
return '' if s < 0
js = "async function iam_load_ruby_modules() {\n"
@ruby_imports.each do |import|
js << import.to_dev_s(asset_name)
end
js << "}\niam_load_ruby_modules();\n"
end
|
#save_entry(asset_key, asset_name, imports_path) ⇒ Object
39
40
41
42
43
|
# File 'lib/isomorfeus/asset_manager/browser_asset.rb', line 39
def save_entry(asset_key, asset_name, imports_path)
entry = File.join(imports_path, asset_key)
File.binwrite(entry, generate_entry(asset_name))
entry
end
|