Skip to content

Commit

Permalink
Attending rubocop and improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vtamara committed Mar 30, 2022
1 parent c833bc9 commit 1d2c425
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 6 additions & 4 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,18 @@ def javascript_pack_tag(*names, defer: true, **options)
@emitted = {}
end
includetags = "".html_safe
newline = "".html_safe
sources = sources_from_manifest_entrypoints(names, type: :javascript)
sources.each do |source|
if @emitted.keys.include?(source) && @emitted[source][:defer] != defer
if @emitted.key?(source) && @emitted[source][:defer] != defer
raise "Chunk #{source} already emitted with defer value "\
"#{@emitted[source][:defer]}. Trying to emit with different "\
"defer value is not allowed."
elsif !@emitted.keys.include?(source)
includetags += javascript_include_tag(source, options)
elsif !@emitted.key?(source)
includetags += newline + javascript_include_tag(source, options)
end
@emitted[source] = {defer: defer}
@emitted[source] = { defer: defer }
newline = "\n".html_safe
end
return includetags
end
Expand Down
20 changes: 8 additions & 12 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ def test_javascript_pack_tag
javascript_pack_tag("application", "bootstrap")
end

def test_javascript_pack_with_no_defer_tag
assert_equal \
%(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
SCRIPTS_APPLICATION_BOOTSTRAP = %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
%(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>\n) +
%(<script src="/packs/application-k344a6d59eef8632c9d1.js"></script>\n) +
%(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
%(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>)

def test_javascript_pack_with_no_defer_tag
assert_equal SCRIPTS_APPLICATION_BOOTSTRAP,
javascript_pack_tag("application", "bootstrap", defer: false)
end

Expand All @@ -132,15 +133,10 @@ def test_javascript_pack_tag_symbol
end

def test_javascript_pack_tag_multiple_invocations
error = assert_raises do
javascript_pack_tag(:application)
javascript_pack_tag(:bootstrap)
end
r = javascript_pack_tag(:application, defer: false)
r += "\n".html_safe + javascript_pack_tag(:bootstrap, defer: false)

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
assert_equal SCRIPTS_APPLICATION_BOOTSTRAP, r
end

def application_stylesheet_chunks
Expand Down

0 comments on commit 1d2c425

Please sign in to comment.