Class: Isomorfeus::AssetManager::NodeAsset

Inherits:
Asset
  • Object
show all
Defined in:
lib/isomorfeus/asset_manager/node_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

Methods inherited from Asset

_original_new, #add_js_import, #add_ruby_import, #bundled!, #bundled?, #touch, #unbundle!

Constructor Details

#initialize(target = :node) ⇒ NodeAsset

Returns a new instance of NodeAsset.



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

def initialize(target = :node)
  super(:node)
end

Class Method Details

.newObject



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

def self.new
  _original_new
end

Instance Method Details

#browser?Boolean

Returns:

  • (Boolean)


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

def browser?
  false
end

#build_ruby_and_save(asset_key, asset_name, imports_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 24

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 })
    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, result.to_s)
  end
  nil
end

#bundleObject



20
21
22
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 20

def bundle
  @hash[:js][:js][:raw]
end

#bundle_js(asset_key, asset_name, imports_path, output_path) ⇒ Object



44
45
46
47
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 44

def bundle_js(asset_key, asset_name, imports_path, output_path)
  js = File.binread(File.join(output_path, asset_key))
  @hash[:js][:js] = { raw: js }
end

#generate_entry(asset_name) ⇒ Object



49
50
51
52
53
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 49

def generate_entry(asset_name)
  js = ''
  js << "#{@js_imports.map(&:to_s).join("\n")}"
  js << "#{@ruby_imports.map { |i| i.to_s(asset_name) }.join('')}"
end

#node?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 16

def node?
  true
end

#save_entry(asset_key, asset_name, imports_path) ⇒ Object



38
39
40
41
42
# File 'lib/isomorfeus/asset_manager/node_asset.rb', line 38

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