Class: RailsAdmin::ImportmapFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/rails_admin/importmap_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'config/importmap.rails_admin.rb') ⇒ ImportmapFormatter

Returns a new instance of ImportmapFormatter.



9
10
11
# File 'lib/generators/rails_admin/importmap_formatter.rb', line 9

def initialize(path = 'config/importmap.rails_admin.rb')
  @packager = Importmap::Packager.new(path)
end

Instance Attribute Details

#packagerObject (readonly)

Returns the value of attribute packager.



7
8
9
# File 'lib/generators/rails_admin/importmap_formatter.rb', line 7

def packager
  @packager
end

Instance Method Details

#formatObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/rails_admin/importmap_formatter.rb', line 13

def format
  imports = packager.import("rails_admin@#{RailsAdmin::Version.js}", from: 'jspm.io')

  # Use ESM compatible version to work around https://github.com/cljsjs/packages/issues/1579
  imports['@popperjs/core'].gsub!('lib/index.js', 'dist/esm/popper.js')

  # Tidy up jQuery UI dependencies
  jquery_uis = imports.keys.filter { |key, _| key =~ /jquery-ui/ }
  imports['jquery-ui/'] = imports[jquery_uis.first].gsub(%r{(@[^/@]+)/[^@]+$}, '\1/')
  imports.reject! { |key, _| jquery_uis.include? key }

  pins = ['pin "rails_admin", preload: true', packager.pin_for('rails_admin/src/rails_admin/base', imports.delete('rails_admin'))]
  (pins + imports.map { |package, url| packager.pin_for(package, url) }).join("\n")
end