-
Notifications
You must be signed in to change notification settings - Fork 87
feat(styleLoader): aggregate stylesheets and dedupe if already loaded #1099
Conversation
Size Change: 0 B Total Size: 689 kB ℹ️ View Unchanged
|
__tests__/server/utils/__snapshots__/renderModuleStyles.spec.js.snap
Outdated
Show resolved
Hide resolved
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.
Cool feature. apart from the small refactoring looks good!!
…com/americanexpress/one-app into feature/aggregate-ssr-styles-loader
Thanks! and thanks for the feedback, addressed all comments |
@@ -1,7 +1,11 @@ | |||
// Jest Snapshot v1, https://goo.gl/fbAQLP | |||
|
|||
exports[`renderModuleStyles should handle a mix of modules with and without SSR styles 1`] = `"<style class="ssr-css">.test-module-b_class { color: red; }</style><style class="ssr-css">.test-module-d_class { color: red; }</style>"`; | |||
exports[`renderModuleStyles should deduplicate aggregatedStyles that have the same digest hash 1`] = `"<style id="shared_hash_deps" data-ssr="true">.test-module-a_deps { color: blue; }</style><style id="shared_hash_local" data-ssr="true">.test-module-a_local { color: rebeccapurple; }</style><style id="unique-hash_deps" data-ssr="true">.test-module-c_deps { color: blue; }</style><style id="unique-hash_local" data-ssr="true">.test-module-c_local { color: rebeccapurple; }</style>"`; |
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.
What is the difference between data-ssr="true'
and class="ssr-css"
?
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.
strictly speaking none - although a style tag ought not have a class name because we're not applying styles onto the element. The idea here was more of a debug step to allow us to more easily track which styles are being hydrated and which are not. We could dispense with this entirely, but it felt like that might still be a useful step for more easily debugging / visualising the changes.
Lift and shift of #1099 for v5.
Description
This PR:
Motivation and Context
When using a component library (or any dependency) that imports styles via CSS Modules, the bundler output generates a server bundle that is inconsistent with the client side bundle, as well as resulting in it being difficult to provide style overrides without resorting to CSS hacks (like !important).
In order to resolve this and match the client side bundle output it's necessary to split the styles into multiple style tags, and then order them to allow for cascade overrides. However, the OneApp styles loader currently wraps all SSR styles in a single style tag.
This change retains the existing functionality in order to provide backwards compatibility for modules generated with older bundlers, whilst allowing for bundlers to provide a new key:
aggregatedStyles
.How Has This Been Tested?
Generated a root module and three child modules that share a component library dependency, with each child module rendering two instances of the same component. The first one unchanged, the second with style overrides applied.
Multiple modules helped to prove that deduplication happened as intended, with only a single instance of the component library styles being rendered, with overrides applying with the expected cascade priority.
This was additionally run with an older version of OneApp, and an older bundle to prove backwards compatibility is unaffected.
Types of Changes
Checklist:
What is the Impact to Developers Using One App?
This allows developers to provide overrides to styles imported via CSS modules within their applications without resorting to CSS hacks and forcing specificity.