Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise on multiple invocations of javascript_pack_tag and stylesheet_pack_tag helpers #19

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
shakapacker (6.0.0.rc.6)
shakapacker (6.0.0.rc.13)
activesupport (>= 5.2)
rack-proxy (>= 0.6.1)
railties (>= 5.2)
Expand Down
14 changes: 14 additions & 0 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def favicon_pack_tag(name, **options)
# <%= javascript_pack_tag 'calendar' %>
# <%= javascript_pack_tag 'map' %>
def javascript_pack_tag(*names, defer: true, **options)
if @javascript_pack_tag_loaded
raise "To prevent duplicated chunks on the page, you should call javascript_pack_tag only once on the page. " \
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
end

@javascript_pack_tag_loaded = true

javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options.tap { |o| o[:defer] = defer })
end

Expand Down Expand Up @@ -141,6 +148,13 @@ def preload_pack_asset(name, **options)
# <%= stylesheet_pack_tag 'calendar' %>
# <%= stylesheet_pack_tag 'map' %>
def stylesheet_pack_tag(*names, **options)
if @stylesheet_pack_tag_loaded
raise "To prevent duplicated chunks on the page, you should call stylesheet_pack_tag only once on the page. " \
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
end

@stylesheet_pack_tag_loaded = true

return "" if Webpacker.inlining_css?

stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
Expand Down
27 changes: 27 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def base_url
"https://example.com"
end
end.new

@javascript_pack_tag_loaded = nil
@stylesheet_pack_tag_loaded = nil
end

def test_asset_pack_path
Expand Down Expand Up @@ -129,6 +132,18 @@ def test_javascript_pack_tag_symbol
javascript_pack_tag(:application)
end

def test_javascript_pack_tag_multiple_invocations
error = assert_raises do
javascript_pack_tag(:application)
javascript_pack_tag(:bootstrap)
end

assert_equal \
"To prevent duplicated chunks on the page, you should call javascript_pack_tag only once on the page. " +
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide",
error.message
end

def application_stylesheet_chunks
%w[/packs/1-c20632e7baf2c81200d3.chunk.css /packs/application-k344a6d59eef8632c9d1.chunk.css]
end
Expand Down Expand Up @@ -156,4 +171,16 @@ def test_stylesheet_pack_tag_splat
(application_stylesheet_chunks).map { |chunk| stylesheet_link_tag(chunk, media: "all") }.join("\n"),
stylesheet_pack_tag("application", media: "all")
end

def test_stylesheet_pack_tag_multiple_invocations
error = assert_raises do
stylesheet_pack_tag(:application)
stylesheet_pack_tag(:hello_stimulus)
end

assert_equal \
"To prevent duplicated chunks on the page, you should call stylesheet_pack_tag only once on the page. " +
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide",
error.message
end
end