Module: Opal::Sprockets::AssetsHelper
- Included in:
- Opal::Sprockets
- Defined in:
- lib/opal/sprockets/assets_helper.rb
Instance Method Summary collapse
-
#javascript_include_tag(name, options = {}) ⇒ Object
Generate a ‘<script>` tag for Opal assets.
-
#load_asset(*names) ⇒ String
Bootstraps modules loaded by sprockets on ‘Opal.modules` marking any non-Opal asset as already loaded.
-
#loaded_asset(name) ⇒ String
Mark an asset as already loaded.
Instance Method Details
#javascript_include_tag(name, options = {}) ⇒ Object
Generate a ‘<script>` tag for Opal assets.
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, = {}) sprockets = .fetch(:sprockets) prefix = .fetch(:prefix) debug = .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.
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.
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 |