require 'opal' require 'opal/sprockets/processor' require 'opal/sprockets/erb' require 'opal/sprockets/server' module Opal module Sprockets # Bootstraps modules loaded by sprockets on `Opal.modules` marking any # non-Opal asset as already loaded. # # @example # # Opal::Sprockets.load_asset('application') # # @example Will output the following JavaScript: # # Opal.loaded("jquery.self", "yet_another_carousel.self"); # Opal.require("opal", "application"); # # @param name [String] The logical name of the main asset to be loaded (without extension) # # @return [String] JavaScript code def self.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 # Mark an asset as already loaded. # This is useful for requiring JavaScript files which are not managed by Opal's laoding system. # # @param [String] name The "logical name" of the asset # @return [String] JavaScript code def self.loaded_asset(name) %{if (typeof(OpalLoaded) === 'undefined') OpalLoaded = []; OpalLoaded.push(#{name.to_json});} end # Generate a `} end else scripts << %{} end scripts << %{} scripts.join "\n" end end end