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

feat(engine-core): programmatic style injection #3123

Merged
merged 7 commits into from
Dec 13, 2022

Conversation

nolanlawson
Copy link
Collaborator

@nolanlawson nolanlawson commented Oct 20, 2022

Does this pull request introduce a breaking change?

Fixes #2917. Implements most of this RFC.

The implementation is behind a runtime flag: ENABLE_PROGRAMMATIC_STYLESHEETS.

This doesn't implement the full RFC. In particular, it doesn't implement LWC stylesheet modules or #3122. Those are TODOs for later.

Details

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ⚠️ Yes, it does include an observable change.

Th engine will look for the static stylesheets property in the component. If that property is an array containing functions, it will attempt to use those functions as stylesheet factories. This seems very unlikely to break anyone, but there is a small chance that someone set an array of functions as the static stylesheets property and those functions throw an error when invoked with empty arguments. (Seems very unlikely to me though.)

GUS work item

W-10183295
W-10183388

@@ -43,6 +44,9 @@ module.exports = function LwcClassTransform(api) {

// Validate the usage of LWC decorators.
validateImportedLwcDecoratorUsage(engineImportSpecifiers);

// Add ?scoped=true to *.scoped.css imports
scopeCssImports(api, path);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This compiler change is not behind the flag, but I think the impact is very small so it shouldn't matter. Any bundler capable of dealing with ?scoped=true imports from CSS should be able to deal with them from JS as well.

Technically, though, this does allow people to use scoped styles with the template.stylesheets = [...] hack. But people are also able to do this with arbitrary functions anyway. 😛 #3122

@rui-rayqiu rui-rayqiu force-pushed the nolan/programmatic-stylesheets-squashed branch from 9c8e2ef to 8e0cdd0 Compare December 8, 2022 18:14
@rui-rayqiu rui-rayqiu force-pushed the nolan/programmatic-stylesheets-squashed branch from 8e0cdd0 to ae95828 Compare December 8, 2022 18:19
@rui-rayqiu rui-rayqiu marked this pull request as ready for review December 8, 2022 18:19
document.body.appendChild(elm);
expect(elm.shadowRoot.querySelector('h1')).toBeTruthy(); // still renders the template correctly
});

Copy link
Collaborator Author

@nolanlawson nolanlawson Dec 9, 2022

Choose a reason for hiding this comment

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

Suggested change
// TODO [#3122]: Disallow treating arbitrary functions as stylesheet functions

export default class extends LightningElement {
static stylesheets = [
() => {
return null;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm amazed this doesn't break LWC anywhere. 😅 We should probably have a separate PR to test how users could attempt to break stylesheets in zany ways, e.g. by having a function that returns numbers, undefined, arrays, etc. (Related: #3122)

Copy link
Collaborator Author

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

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

I am not allowed to approve my own PR, but overall this LGTM, just a few small suggestions @rui-rayqiu. 👍

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.

CSS file for all templates in a component (rather than one-per template)
2 participants