Module: Opal::Sprockets

Defined in:
opal/lib/opal/sprockets.rb,
opal/lib/opal/sprockets/path_reader.rb,
opal/lib/opal/sprockets/source_map_header_patch.rb

Defined Under Namespace

Modules: SourceMapHeaderPatch Classes: PathReader

Class Method Summary collapse

Class 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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'opal/lib/opal/sprockets.rb', line 66

def self.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]
  raise "Cannot find asset: #{name}" if asset.nil?
  scripts = []

  if debug
    asset.to_a.map do |dependency|
      scripts << %{<script src="#{prefix}/#{dependency.logical_path}?body=1"></script>}
    end
  else
    scripts << %{<script src="#{prefix}/#{name}.js"></script>}
  end

  scripts << %{<script>#{::Opal::Sprockets.load_asset(name, sprockets)}</script>}

  scripts.join "\n"
end

.load_asset(name, sprockets) ⇒ String

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

Examples:


Opal::Sprockets.load_asset(Rails.application.assets, 'application')

Will output the following JavaScript:


if (typeof(Opal) !== 'undefined') {
  Opal.loaded("opal");
  Opal.loaded("jquery.self");
  Opal.load("application");
}

Parameters:

  • name (String)

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

  • sprockets (Sprockets::Environment)

Returns:

  • (String)

    JavaScript code



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'opal/lib/opal/sprockets.rb', line 26

def self.load_asset(name, sprockets)
  asset = sprockets[name.sub(/(\.(js|rb|opal))*#{REGEXP_END}/, '.js')]
  return '' if asset.nil?

  opal_extnames = sprockets.engines.map do |ext, engine|
    ext if engine <= ::Opal::Processor
  end.compact

  module_name       = -> asset { asset.logical_path.sub(/\.js#{REGEXP_END}/, '') }
  path_extnames     = -> path  { File.basename(path).scan(/\.[^.]+/) }
  mark_loaded       = -> paths { "Opal.loaded([#{paths.map(&:inspect).join(',')}]);" }
  processed_by_opal = -> asset { (path_extnames[asset.pathname] & opal_extnames).any? }
  stubbed_files     = ::Opal::Config.stubbed_files

  non_opal_assets = ([asset]+asset.dependencies)
    .select { |asset| not(processed_by_opal[asset]) }
    .map { |asset| module_name[asset] }

  loaded = ['opal'] + non_opal_assets + stubbed_files.to_a

  if processed_by_opal[asset]
    load_asset_code = "Opal.load(#{module_name[asset].inspect});"
  end


  "if (typeof(Opal) !== 'undefined') { "\
    "#{mark_loaded[loaded]} "\
    "#{load_asset_code} "\
  "}"
end