Module: Opal::Sprockets::AssetsHelper

Included in:
Opal::Sprockets
Defined in:
lib/opal/sprockets/assets_helper.rb

Instance Method Summary collapse

Instance Method Details

#javascript_include_tag(name, options = {}) ⇒ Object

Generate a ‘<script>` tag for Opal assets.

Parameters:

  • name (String)

    The logical name of the asset to be loaded (without extension)

  • options (Hash) (defaults to: {})

    The options about sprockets

Options Hash (options):

  • :sprockets (Sprockets::Environment)

    The sprockets instance

  • :prefix (String)

    The prefix String at which is mounted Sprockets, e.g. ‘/assets’

  • :debug (Boolean)

    Wether to enable debug mode along with sourcemaps support

Returns:

  • a string of HTML code containing ‘<script>` tags.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opal/sprockets/assets_helper.rb', line 56

def javascript_include_tag(name, options = {})
  sprockets = options.fetch(:sprockets)
  prefix    = options.fetch(:prefix)
  debug     = options.fetch(:debug)

  # Avoid double slashes
  prefix = prefix.chop if prefix.end_with? '/'

  asset = sprockets[name, accept: "application/javascript", pipeline: debug ? :debug : nil]
  raise "Cannot find asset: #{name}" if asset.nil?
  scripts = []

  if debug
    scripts << %{<script src="#{prefix}/#{asset.digest_path}"></script>}
  else
    scripts << %{<script src="#{prefix}/#{name}.js"></script>}
  end

  scripts << %{<script>#{load_asset('opal', name)}</script>}

  scripts.join "\n"
end

#load_asset(*names) ⇒ String

Bootstraps modules loaded by sprockets on ‘Opal.modules` marking any non-Opal asset as already loaded.

Examples:


Opal::Sprockets.load_asset('application')

Will output the following JavaScript:


Opal.loaded("jquery.self", "yet_another_carousel.self");
Opal.require("opal", "application");

Parameters:

  • name (String)

    The logical name of the main asset to be loaded (without extension)

Returns:

  • (String)

    JavaScript code



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opal/sprockets/assets_helper.rb', line 17

def load_asset(*names)
  if names.last.is_a?(::Sprockets::Environment)
    unless @load_asset_warning_displayed
      @load_asset_warning_displayed = true
      warn "Passing a sprockets environment to Opal::Sprockets.load_asset no more needed.\n  #{caller(1, 3).join("\n  ")}"
    end
    names.pop
  end

  names = names.map { |name| name.sub(/(\.(js|rb|opal))*\z/, '') }
  stubbed = ::Opal::Config.stubbed_files.to_a

  loaded = 'typeof(OpalLoaded) === "undefined" ? [] : OpalLoaded'
  loaded = "#{stubbed.to_json}.concat(#{loaded})" if stubbed.any?

  [
    "Opal.loaded(#{loaded});",
    *names.map { |name| "Opal.require(#{name.to_json});" }
  ].join("\n")
end

#loaded_asset(name) ⇒ String

Mark an asset as already loaded. This is useful for requiring JavaScript files which are not managed by Opal’s laoding system.

Parameters:

  • name (String)

    The “logical name” of the asset

Returns:

  • (String)

    JavaScript code



43
44
45
# File 'lib/opal/sprockets/assets_helper.rb', line 43

def loaded_asset(name)
  %{if (typeof(OpalLoaded) === 'undefined') OpalLoaded = []; OpalLoaded.push(#{name.to_json});}
end