Sha256: f6dcbb4361c225daf31d20b046ec360997ca51b58cf479d6c82c4639e1679734

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'action_view'

module CKEditor5::Rails::Assets
  WEBCOMPONENT_SOURCE = File.read(File.join(__dir__, 'webcomponent.mjs')).html_safe

  class AssetsBundleHtmlSerializer
    include ActionView::Helpers::TagHelper

    attr_reader :bundle

    def initialize(bundle)
      raise TypeError, 'bundle must be an instance of AssetsBundle' unless bundle.is_a?(AssetsBundle)

      @bundle = bundle
    end

    def to_html
      safe_join([
                  preload_tags,
                  styles_tags,
                  window_scripts_tags,
                  scripts_import_map_tag,
                  web_component_tag
                ])
    end

    def self.url_resource_preload_type(url)
      case File.extname(url)
      when '.js' then 'script'
      when '.css' then 'style'
      else 'fetch'
      end
    end

    private

    def web_component_tag
      @web_component_tag ||= tag.script(WEBCOMPONENT_SOURCE, type: 'module', nonce: true)
    end

    def window_scripts_tags
      @window_scripts_tags ||= safe_join(bundle.scripts.filter_map do |script|
        tag.script(src: script.url, nonce: true, async: true) if script.window?
      end)
    end

    def scripts_import_map_tag
      return @scripts_import_map_tag if defined?(@scripts_import_map_tag)

      import_map = bundle.scripts.each_with_object({}) do |script, map|
        map[script.import_name] = script.url if script.esm?
      end

      @scripts_import_map_tag = tag.script(
        { imports: import_map }.to_json.html_safe,
        type: 'importmap',
        nonce: true
      )
    end

    def styles_tags
      @styles_tags ||= safe_join(bundle.stylesheets.map do |url|
        tag.link(href: url, rel: 'stylesheet')
      end)
    end

    def preload_tags
      @preload_tags ||= safe_join(bundle.preloads.map do |url|
        tag.link(href: url, rel: 'preload', as: self.class.url_resource_preload_type(url))
      end)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ckeditor5-1.0.6 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb
ckeditor5-1.0.5 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb
ckeditor5-1.0.4 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb
ckeditor5-1.0.3 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb
ckeditor5-1.0.2 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb
ckeditor5-1.0.1 lib/ckeditor5/rails/assets/assets_bundle_html_serializer.rb