Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
Add bootstrapper.js and requirejs_run_config_json helper
Browse files Browse the repository at this point in the history
This allows us to cache the runtime configuration instead of duplicating it on every page.
You can also override bootstrapper.js to add other essential resources to be downloaded with require.js
and the configuration.

Bandwidth during the early stages of page loading can be better optimized by loading some scripts synchronously
because the browser can start loading these resources earlier before all images and other scripts start loading.
Because bandwidth isn't saturated early on, stuff like an image lazyloader would be good to load in bootstrapper.js.
  • Loading branch information
wuservices committed Jul 6, 2015
1 parent ee66558 commit 0dfd7e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
68 changes: 33 additions & 35 deletions app/helpers/requirejs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ def _requirejs_data(name, &block)
end.join(" ")
end

def requirejs_run_config_json(name=nil)
requirejs = Rails.application.config.requirejs
run_config = requirejs.run_config.dup
unless _priority.empty?
run_config = run_config.dup
run_config[:priority] ||= []
run_config[:priority].concat _priority
end
if Rails.application.config.assets.digest
modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m }

# Generate digestified paths from the modules spec
paths = {}
modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/,'' }

if run_config.has_key? 'paths'
# Add paths for assets specified by full URL (on a CDN)
run_config['paths'].each { |k,v| paths[k] = v if v =~ /^https?:/ }
end

# Override user paths, whose mappings are only relevant in dev mode
# and in the build_config.
run_config['paths'] = paths
end

run_config['baseUrl'] = base_url(name)
# Detect functions in JSON and unescape them so they can be evaluated by RequireJS
run_config_json = run_config.to_json.gsub(/"(function\(.*?\)\s*?{.*?}[\s\\n]*)"/) do |f|
eval(f).strip.delete("\n")
end
end

def requirejs_include_tag(name=nil, &block)
requirejs = Rails.application.config.requirejs

Expand All @@ -34,42 +66,8 @@ def requirejs_include_tag(name=nil, &block)
html = ""

_once_guard do
unless requirejs.run_config.empty?
run_config = requirejs.run_config.dup
unless _priority.empty?
run_config = run_config.dup
run_config[:priority] ||= []
run_config[:priority].concat _priority
end
if Rails.application.config.assets.digest
modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m }

# Generate digestified paths from the modules spec
paths = {}
modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/,'' }

if run_config.has_key? 'paths'
# Add paths for assets specified by full URL (on a CDN)
run_config['paths'].each { |k,v| paths[k] = v if v =~ /^https?:/ }
end

# Override user paths, whose mappings are only relevant in dev mode
# and in the build_config.
run_config['paths'] = paths
end

run_config['baseUrl'] = base_url(name)
# Detect functions in JSON and unescape them so they can be evaluated by RequireJS
run_config_json = run_config.to_json.gsub(/"(function\(.*?\)\s*?{.*?}[\s\\n]*)"/) do |f|
eval(f).strip.delete("\n")
end
html.concat <<-HTML
<script>var require = #{run_config_json};</script>
HTML
end

html.concat <<-HTML
<script #{_requirejs_data(name, &block)} src="#{_javascript_path 'require.js'}"></script>
<script #{_requirejs_data(name, &block)} src="#{_javascript_path 'bootstrapper.js'}"></script>
HTML

html.html_safe
Expand Down
2 changes: 1 addition & 1 deletion lib/requirejs/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Engine < ::Rails::Engine
### Configuration setup
config.before_configuration do |app|
config.requirejs = Requirejs::Rails::Config.new(app)
config.requirejs.precompile = [/require\.js$/]
config.requirejs.precompile = [/bootstrapper\.js$/]
config.requirejs.sprokets_js_compression = false
end

Expand Down
4 changes: 4 additions & 0 deletions vendor/assets/javascripts/bootstrapper.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Copy and override this file to include other essential depenencies with require.js so they get cached
*= require requirejs-config
*= require require
*/
3 changes: 3 additions & 0 deletions vendor/assets/javascripts/requirejs-config.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%# Allows us to use a helper method from the requirejs-rails gem from within the asset pipeline %>
<% environment.context_class.instance_eval { include RequirejsHelper } %>
var require = <%= requirejs_run_config_json %>;

0 comments on commit 0dfd7e4

Please sign in to comment.