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

Conversation

pulkitkkr
Copy link
Contributor

Summary

To add stylesheets for the new filesystem-based Component Registry API, We need a stylesheet to append helper. This PR Adds the stylesheet_append_tag helper to Shakapacker Codebase

@@ -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 :)

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' %>

@justin808
Copy link
Member

@pulkitkkr you have CI issues.

@justin808
Copy link
Member

@pulkitkkr we need:

  1. Tests
  2. Documentation

Copy link
Collaborator

@tomdracz tomdracz left a comment

Choose a reason for hiding this comment

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

Couple more comments added

end

requested_packs = sources_from_manifest_entrypoints(names, type: :stylesheet)
appended_packs = available_sources_from_manifest_entrypoints(@stylesheet_pack_tag_queue || [], type: :stylesheet)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need any deduplication? Can names passed and what's in @stylesheet_pack_tag_queue reference the same files that we would load twice?

Copy link
Contributor Author

@pulkitkkr pulkitkkr Jun 17, 2022

Choose a reason for hiding this comment

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

That's a good catch, Handled it by using stylesheet_link_tag(*(requested_packs | appended_packs), **options) now.

@@ -184,6 +207,10 @@ def sources_from_manifest_entrypoints(names, type:)
names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name.to_s, type: type) }.flatten.uniq
end

def available_sources_from_manifest_entrypoints(names, type:)
names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks(name.to_s, type: type) }.flatten.compact.uniq
Copy link
Collaborator

Choose a reason for hiding this comment

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

@pulkitkkr What's the rationale for adding this? Why would we use non-raising method that does not indicate pack missing?

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 We are introducing a new feature to react_on_rails that automatically loads the packs associated with the requested component.

Eg: Developer used react_component("MyComponent", props )

This will automatically load, generated/MyComponent pack using append_javascript_pack and append_stylesheet_pack

While testing this on one of the projects, we discovered there were few component packs with js only, i.e. No stylesheets. We want to use a non-error-raising lookup to handle such a scenario.

@pulkitkkr pulkitkkr requested review from justin808 and tomdracz June 17, 2022 14:58
README.md Outdated
However, you typically can't do that in the main layout, as the view and partial codes will depend on the route.

Thus, you can distribute the logic of what packs are needed for any route. All the magic of splitting up the code was automatic!

Copy link
Member

Choose a reason for hiding this comment

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

The fully copy/paste replace is a bit awkward.

All the magic of splitting up the code was automatic!

Maybe:
All the magic of splitting up the CSS was automatic!

However, I think the docs can be more concise by discussing both helpers together.

Plus the docs for the append_stylesheet_pack_tag and stylesheet_pack_tag should mention the media type issue.

stylesheet_link_tag(*appended_stylesheets, **options)
end

def append_stylesheet_pack_tag(*names)
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)

assert_equal \
(application_stylesheet_chunks + hello_stimulus_stylesheet_chunks).uniq.map { |chunk| stylesheet_link_tag(chunk) }.join("\n"),
stylesheet_pack_tag(:application)
end
Copy link
Member

Choose a reason for hiding this comment

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

Tests don't address:

  • stylesheet_pack_tag can be called multiple times with different media attributes, while javascript_pack_tag can be called only once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test test_multiple_stylesheet_pack_with_different_media_attr and for javascript_pack_tag we already have test test_javascript_pack_tag_multiple_invocations

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is assert_nothing_raised enough there? Can we also test what happens?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(and possibly both with the same and different stylesheet names).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexeyr We already have a one for different names test_stylesheet_pack_tag_multiple_invocations_are_allowed

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant with different names and different media attributes, which that test doesn't do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated The Test to handle this scenario :)

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/add-stylesheet-append-tag branch from 4e6a46b to 1d2157e Compare June 29, 2022 23:10
@pulkitkkr pulkitkkr requested a review from justin808 June 29, 2022 23:12
README.md Outdated
```


Copy link
Collaborator

Choose a reason for hiding this comment

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

Trivial, but this extra line was probably added accidentally. If you do change tests, may as well remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it! My bad.

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/add-stylesheet-append-tag branch 2 times, most recently from 6b7ea3e to 1e1e099 Compare June 30, 2022 15:33
@pulkitkkr pulkitkkr requested a review from alexeyr June 30, 2022 15:34
README.md Outdated
```

is the same as using this in the main layout:

```erb
<%= javascript_pack_tag 'calendar', 'map', application' %>
<%= stylesheet_pack_tag 'calendar', 'map', application' %>
```

However, you typically can't do that in the main layout, as the view and partial codes will depend on the route.

Thus, you can distribute the logic of what packs are needed for any route. All the magic of splitting up the code was automatic!
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, there is something left over from @justin808's comments here: "splitting up code and CSS"

Copy link
Collaborator

Choose a reason for hiding this comment

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

or "code and stylesheets"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it 😄

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/add-stylesheet-append-tag branch from 1e1e099 to dd6d56a Compare June 30, 2022 15:40
@@ -158,7 +158,27 @@ def preload_pack_asset(name, **options)
def stylesheet_pack_tag(*names, **options)
return "" if Webpacker.inlining_css?

stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
if @stylesheet_pack_tag_loaded
puts "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.

Wait, won't you get the warning here even for the perfectly fine case of

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:

Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed over DM, removed the warning for now 😄

@alexeyr alexeyr merged commit 7bf7b0f into master Jun 30, 2022
@alexeyr alexeyr deleted the pulkitkkr/add-stylesheet-append-tag branch June 30, 2022 17:04
alexeyr-ci1 pushed a commit that referenced this pull request Jun 30, 2022
This PR just adds Changelog Text for append_stylesheet_pack_tag added in #144

**Important:** `append_javascript_pack_tag` can be used anywhere in your application as long as it is executed BEFORE the `javascript_pack_tag`. If you attempt to call `append_javascript_pack_tag` helper after `javascript_pack_tag`, an error will be raised. You should have only a single `javascript_pack_tag` invocation in your page load.

**Important** Similar to `append_javascript_pack_tag`, the `append_stylesheet_pack_tag` should be used anywhere in your application as long as it is executed BEFORE the `stylesheet_pack_tag`. However, if you attempt to call `append_stylesheet_pack_tag` helper after `stylesheet_pack_tag`, an error will be **NOT** be raised.
Copy link
Member

Choose a reason for hiding this comment

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

Incorrect.

def append_stylesheet_pack_tag(*names)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants