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

javascript_pack_tag handles repetition of individual chunks. Closes #39 #91

Closed
wants to merge 3 commits into from
Closed
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
61 changes: 48 additions & 13 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,58 @@ def favicon_pack_tag(name, **options)
# <script src="/packs/map~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
# <script src="/packs/map-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
#
# DO:
#
# <%= javascript_pack_tag 'calendar', 'map' %>
#
# DON'T:
#
# <%= javascript_pack_tag 'calendar' %>
# <%= javascript_pack_tag 'map' %>
# There should be at least one javascript_pack_tag in the main template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main template, main one ==> main view

partials, can be used on any views, including:

  1. Other partials
  2. Main view for the controller action
  3. Layout for the controller action

Order:

  1. Main View => includes partials, content_for blocks
  2. Layout => will use the main view and any content_for blocks, and any partials directly on the layout

# ActionView will call first partial templates and after the main one
# We don't emit chunks in partials, just in the main template
# In partial we just queue what has to be emitted in the main

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"

options[:defer] = defer
if !defined?(@emitted)
@emitted = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

@emitted ||= {}

end
if !defined?(@queue)
@queue = []
end

@includetags = "".html_safe
@newline = "".html_safe

def emit_without_repetition(source, defer, options)
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 a conflict."
elsif [email protected]?(source)
@includetags += @newline + javascript_include_tag(source, options)
@newline = "\n".html_safe
@emitted[source] = { defer: defer }
end
end

# Determine if we are in a partial template or in the main one
partial = caller.filter{|c| c.include?('partial_renderer')}.count > 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtamara i think we're getting closer but don't quite like this bit. I'll try and figure out if there are some better ways to do it.

Alternatively, it's more overhead and bit more difficult for users to use but if we split the implementation here into a helper that populates a queue and then javascript_pack_tag is still only callable once (and uses the queue), I think we will have an implementation that solves the main problems we're facing with this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has issues on several levels:

  1. What if the method name filtered for changes?
  2. Performance?
  3. Completely unconventional to have production code using the call stack to figure something out.



#puts names, partial;
#debugger
sources = sources_from_manifest_entrypoints(names, type: :javascript)

@javascript_pack_tag_loaded = true
if partial
sources.each do |source|
@queue |= [source]
end
else # main template (layout?)
sources.each do |source|
emit_without_repetition(source, defer, options)
end
@queue.each do |source|
emit_without_repetition(source, defer, options)
end
end

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

# Creates a link tag, for preloading, that references a given Webpacker asset.
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