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
-
.javascript_include_tag(name, options = {}) ⇒ Object
Generate a
<script>
tag for Opal assets. -
.load_asset(name, sprockets) ⇒ String
Bootstraps modules loaded by sprockets on
Opal.modules
marking any non-Opal asset as already loaded.
Class Method Details
.javascript_include_tag(name, options = {}) ⇒ Object
Generate a <script>
tag for Opal assets.
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, = {}) sprockets = .fetch(:sprockets) prefix = .fetch(:prefix) debug = .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.
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 |