-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
# 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 = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has issues on several levels:
|
||
|
||
|
||
#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. | ||
|
There was a problem hiding this comment.
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:
Order: