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

finalize v1.0 component script and style behavior #41

Merged
merged 4 commits into from
Dec 14, 2021

Conversation

FredKSchott
Copy link
Member

@FredKSchott FredKSchott commented Dec 9, 2021

Summary

Finalize the component styling & scripting behavior for v1.0. Documents existing behavior and adds 2-3 new restrictions to prevent bugs.

<style>
  @import './dep.css'; /* Vite imports supported, including npm packages! */
  h1 { color: red; }
</style>

<style global>
  /* Same as <style>, but styles are applied globally to the page. */
  body { padding: 5px; }
</style>

<script>
  console.log('included as-is in the final HTML page output.')
</script>

<script hoist>
  import cowsay from 'cowsay';
  console.log(cowsay('I am bundled with the rest of the website JS!'));
</script>

<h1>Hello!</h1>

Links

@FredKSchott FredKSchott changed the title refocus RFC on documenting behavior without API change finalize v1.0 script and style behavior Dec 9, 2021
@FredKSchott FredKSchott changed the title finalize v1.0 script and style behavior finalize v1.0 component script and style behavior Dec 9, 2021
@FredKSchott
Copy link
Member Author

FredKSchott commented Dec 9, 2021

Capturing some other feedback from @jasikpark in #12:

  • A use case that I would not want to lose is scoped styles + define:vars, which it sounds like this RFC removes w/ scoped styles being behind @component + define:vars only supported via static markup. Is this not a use case that's supported today?

@jasikpark @matthewp take a look at https://github.com/withastro/rfcs/blob/style-script-rfc/active-rfcs/0000-style-script-behavior.md#deep-dive-definevars - I believe that we can keep define:vars behavior for component-level styles, with the caveat that multiple component renders may override the value globally.

  • Is it a requirement that all attributes are dropped from a tag with @component? Could those attributes not be copied over to the browser?

It is currently the case that all attributes are dropped from <style> and <script hoist>. This is because they are all bundled together into the final optimized CSS and JS bundles for the page, so it's not 1:1 how many <script> tags you finally get on the page.

Copy link
Contributor

@jasikpark jasikpark left a comment

Choose a reason for hiding this comment

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

This looks good as a way to solidify what the current behavior is so we can work off of it towards a new rfc!

active-rfcs/0000-style-script-behavior.md Outdated Show resolved Hide resolved
active-rfcs/0000-style-script-behavior.md Outdated Show resolved Hide resolved
- ESM import will not be resolved or bundled, sent untouched/raw to the browser.
- Will be output in the final component render(), useful for adding global JS.
- May be duplicated if this component appears on the page multiple times.
- In the future, we could add some kind of of `is:unique` support to prevent duplicate scripts.
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this provided by hoist?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, good point. This is trying to say that we could do is:unique seperately from hoist. But I guess none of that is really in scope for this RFC anymore, so maybe best to remove entirely.

### New RFC Behavior

- `<script hoist>` contents must be static, therefore `define:vars` is not supported. Because the script is bundled ahead-of-time, dynamic values won't exist. This is currently easy to break in v0.21, so this RFC proposes removing the support entirely for `<script hoist>` (still available for `<script>` and `<style>`).
- Cannot be nested within a template expression or conditional. Bundled scripts must be scanned by the compiler. This is currently easy to break in v0.21 with something like `{alwaysFalse && <script hoist>...` so this RFC moves to remove support for this. This change shouldn't impact many users.
Copy link
Contributor

Choose a reason for hiding this comment

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

How can we test for whether it will impact many users? Once usecase I would personally use for supporting LaTeX formatting would be conditionally adding in the CSS and JS for processing LaTeX markup only on pages with "hasMath" in the front-matter. I don't know how often that's a thing, but I definitely think it's a valid usecase!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah you're right, this may work given how the bundler works today. I'd assumed most use-cases for this were buggy. Let me address this in the RFC, but regardless this will be a pretty strict requirement for the build perf + SSR-readiness improvements that we need to make.

There could be other ways to solve this though, by treating that logic more like a component to be added conditionally.

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.

2 participants