Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit c46bd8f

Browse files
committed
Remove the data-main attribute from the HTML generated by requirejs_include_tag, instead opting to manually invoke require on the given module
Fixes #182.
1 parent 32f66fb commit c46bd8f

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,27 @@ module RequirejsHelper
77
mattr_accessor :_priority
88
@@_priority = []
99

10-
def _requirejs_data(name, &block)
11-
{}.tap do |data|
12-
if name
13-
name += ".js" unless name =~ /\.js$/
14-
data['main'] = _javascript_path(name) \
15-
.sub(/\.js$/, '') \
16-
.sub(base_url(name), '') \
17-
.sub(/\A\//, '')
18-
end
19-
20-
data.merge!(yield controller) if block_given?
21-
end.map do |k, v|
22-
%Q{data-#{k}="#{v}"}
23-
end.join(" ")
24-
end
25-
2610
def requirejs_include_tag(name=nil, &block)
2711
requirejs = Rails.application.config.requirejs
2812

2913
if requirejs.loader == :almond
3014
name = requirejs.module_name_for(requirejs.build_config['modules'][0])
31-
return _almond_include_tag(name, &block)
15+
return almond_include_tag(name, &block)
3216
end
3317

3418
html = ""
3519

36-
_once_guard do
37-
html.concat <<-HTML
38-
<script #{_requirejs_data(name, &block)} src="#{_javascript_path 'require.js'}"></script>
39-
HTML
20+
once_guard do
21+
rjs_attributes = {
22+
src: javascript_path("require")
23+
}
24+
25+
rjs_attributes = rjs_attributes.merge(Hash[block.call(controller).map do |key, value|
26+
["data-#{key}", value]
27+
end]) \
28+
if block
29+
30+
html.concat(content_tag(:script, "", rjs_attributes))
4031

4132
unless requirejs.run_config.empty?
4233
run_config = requirejs.run_config.dup
@@ -52,7 +43,7 @@ def requirejs_include_tag(name=nil, &block)
5243

5344
# Generate digestified paths from the modules spec
5445
paths = {}
55-
modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/, '' }
46+
modules.each { |m| paths[m] = javascript_path(m).sub /\.js$/, '' }
5647

5748
if run_config.has_key? 'paths'
5849
# Add paths for assets specified by full URL (on a CDN)
@@ -67,16 +58,34 @@ def requirejs_include_tag(name=nil, &block)
6758
end
6859

6960
run_config['baseUrl'] = base_url(name)
70-
html.concat <<-HTML
71-
<script>require.config(#{run_config.to_json});</script>
72-
HTML
61+
62+
html.concat(content_tag(:script) do
63+
script = "require.config(#{run_config.to_json});"
64+
65+
# Pass an array to `require`, since it's a top-level module about to be loaded asynchronously (see
66+
# `http://requirejs.org/docs/errors.html#notloaded`).
67+
script.concat(" require([#{name.dump}]);") \
68+
if name
69+
70+
script.html_safe
71+
end)
7372
end
7473

7574
html.html_safe
7675
end
7776
end
7877

79-
def _once_guard
78+
def javascript_path(name)
79+
if defined?(super)
80+
super
81+
else
82+
"/assets/#{name}"
83+
end
84+
end
85+
86+
private
87+
88+
def once_guard
8089
if defined?(controller) && controller.requirejs_included
8190
raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page."
8291
end
@@ -87,16 +96,8 @@ def _once_guard
8796
retval
8897
end
8998

90-
def _almond_include_tag(name, &block)
91-
"<script src='#{_javascript_path name}'></script>\n".html_safe
92-
end
93-
94-
def _javascript_path(name)
95-
if defined?(javascript_path)
96-
javascript_path(name)
97-
else
98-
"/assets/#{name}"
99-
end
99+
def almond_include_tag(name, &block)
100+
content_tag(:script, "", src: javascript_path(name))
100101
end
101102

102103
def base_url(js_asset)

test/requirejs-rails_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def wrap(tag)
132132
test "requirejs_include_tag_with_param" do
133133
render text: wrap(requirejs_include_tag("application"))
134134

135-
assert_select "script:first-of-type[src=\"/javascripts/require.js\"]" \
136-
"[data-main=\"javascripts/application\"]", count: 1
135+
assert_select "script:first-of-type[src=\"/javascripts/require.js\"]", count: 1
137136
end
138137

139138
test "requirejs_include_tag_with_block" do
@@ -142,7 +141,6 @@ def wrap(tag)
142141
end)
143142

144143
assert_select "script:first-of-type[src=\"/javascripts/require.js\"]" \
145-
"[data-main=\"javascripts/application\"]" \
146144
"[data-class=\"TestController\"]", count: 1
147145
end
148146

0 commit comments

Comments
 (0)