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

Add Append StyleSheet Pack Tag Helper #144

Merged
merged 10 commits into from
Jun 30, 2022
19 changes: 19 additions & 0 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,26 @@ def preload_pack_asset(name, **options)
def stylesheet_pack_tag(*names, **options)
return "" if Webpacker.inlining_css?

if @stylesheet_pack_tag_loaded
raise "To prevent duplicated stylesheet on the page, you should call stylesheet_pack_tag only once on the page. " \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rather not bring this raise back at this point. Duplicate stylesheets are less problematic and I don't want to bring in a breaking change for any users that relied on this raise not being here.

We'd be shipping this PR in a minor release so avoiding breaking changes is preferable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @tomdracz! I am removing the raise for now :) But Still keeping it as a warning in logs :)

"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
end

stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
appended_stylesheets = sources_from_manifest_entrypoints(@stylesheet_pack_tag_queue || [], type: :stylesheet)

@stylesheet_pack_tag_loaded = true

stylesheet_link_tag(*appended_stylesheets, **options)
end

def append_stylesheet_pack_tag(*names)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Think we need more than just names here. If you look at original issue around multiple pack tags, one of the requests was supporting different "media" attributes.
Wondering if we should queue up files based on media attribute in the same way we queue and organise js packs on defer attr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can Queue Up the files based on media attributes as a separate Feature Pull Request as, unlike deferred, it's not a boolean value and can have and, not or a comma.

Example: media="screen and (max-aspect-ratio:16/9)".

Copy link
Member

Choose a reason for hiding this comment

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

@tomdracz I'm not convinced how many people would need both the flexibility of the append_stylesheet_pack_tag with different media attributes.

Let's wait for feedback.

If necessary, we release an update where the *names arg can be either:

  1. String of the pack name
  2. Hash, where the keys are the media and the values are arrays of packs.

@tomdracz do you agree that this would be a backwards compatible update if needed?

@pulkitkkr are we ready to merge and release this one?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@justin808 If I remember correctly we've removed the raise in the first place to support different media attrs. Agree that if we've got that in place, then having it in append_ helper is less crucial.

The update if needed should be backwards-compatible, yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tomdracz should we document the ordering for appended_packs and requested_packs ?

Copy link
Member

Choose a reason for hiding this comment

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

@pulkitkkr we need to document what happens when this happens:

<%= stylesheet_pack_tag "application" %>
<%= stylesheet_pack_tag "print", media: "print" %>

See #39 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already have :

While this also generally applies to `stylesheet_pack_tag`, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple <style> tags for different output media:

``` erb
<%= stylesheet_pack_tag 'application', media: 'screen' %>
<%= stylesheet_pack_tag 'print', media: 'print' %>

if @stylesheet_pack_tag_loaded
raise "You can only call append_stylesheet_pack_tag before stylesheet_pack_tag helper. " \
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
Copy link
Member

Choose a reason for hiding this comment

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

Please refer to the documentation for stylesheet_pack_tag for more information.

end

@stylesheet_pack_tag_queue = names
end

def append_javascript_pack_tag(*names, defer: true)
Expand Down