-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
scoped style doesn't work if compile component with css option 'none' #8465
Comments
Rich's comment in that closed PR still is correct - why not just ignore the output? I also don't understand how this is related to bundle size in a web worker. Setting the option to |
I created a repository to demonstrate the issue since it's difficult to explain without code. My workflow look like
Since Now, let's talk about
Compare ssr_none/step2.html with ssr_external/step2.html, the scoped class name is missed. Returning to my proposal, it is just an optimization for my particular scenario. |
Normally, the generated CSS requires post-processing by a build system to rewrite all links to assets. However, when building with Vite with I doubt the current usefulness of CSS in the SSR build result. I notice that SvelteKit does not use the content in |
The svelte playground has been upgraded, enabling us to illustrate the problem more conveniently now. This is the matrix illustrating how the option affects the JS output.
As you can see, My proposal is to remove css from the JS output when we use The goal is very similar to what we did with #7914, which introduced From my perspective, I believe that this change is not a significant breaking change. cc: @dummdidumm |
@bluwy / @dominikg / @Conduitry is anyone of you aware if any tooling uses the CSS code and map the SSR renderer appends to the Example of what I'm talking about: // SSR compilation output:
/* App.svelte generated by Svelte v4.0.0 */
import { create_ssr_component } from "svelte/internal";
// do we need this css object?
const css = {
code: "p.svelte-urs9w7{color:purple;font-family:'Comic Sans MS', cursive;font-size:2em}",
map: "{\"version\":3,\"file\":\"App.svelte\",\"sources\":[\"App.svelte\"],\"sourcesContent\":[\"<p>Styled!</p>\\n\\n<style>\\n\\tp {\\n\\t\\tcolor: purple;\\n\\t\\tfont-family: 'Comic Sans MS', cursive;\\n\\t\\tfont-size: 2em;\\n\\t}\\n</style>\\n\"],\"names\":[],\"mappings\":\"AAGC,eAAE,CACD,KAAK,CAAE,MAAM,CACb,WAAW,CAAE,eAAe,CAAC,CAAC,OAAO,CACrC,SAAS,CAAE,GACZ\"}"
};
const App = create_ssr_component(($$result, $$props, $$bindings, slots) => {
$$result.css.add(css); // can we omit this (and therefore the above object) if css: 'external' ? Who uses this?
return `<p class="svelte-urs9w7">Styled!</p>`;
});
export default App; |
I don't think any tooling is using that (the
Re svelte/packages/svelte/src/runtime/internal/ssr.js Lines 168 to 173 in c0d9262
|
I'm tempted to make the change that when css is set to |
I remember having that same thought for Vite 4.0.1, made the change, and made a few people unhappy 😄 But since this is more internal stuff, and |
I prefer a strict view on semver. Unless this is completely broken, has no usecase and changing it fixes a bug, it should wait for 5.0. Also if you move ahead, check with ecosystem-ci and users who possibly enabled devSourcemaps in Vite. I remember at least one bugfix we made in vps to accomodate |
So how about this: If SSR source maps are disabled AND the css is set to |
@dummdidumm, thank you for your efforts to address this issue. Currently, both Introducing the I'm confused by the sourcemap concern, how could it be related to the
I'm attempting to run tests with a patched svelte on svelte-kit and vite-svelte-plugin. |
There are sourcemaps for CSS too, not just JS. If users enable these, they should work. Not emitting them in that case would break their usecase |
But shouldn't these people use the |
I found this topic has been discussed before, #3604. Rich Harris said that it's not designed to have any effect in SSR mode. But I didn't find any strong objection to removing it from the generated js.code. |
Closing since CSS is no longer included in JS output in Svelte 5 |
Describe the bug
As #7914 said:
However,
{css:"none"}
does not work well with scoped styles.Reproduction
Currently, the REPL doesn't support
none
(and I created a PR to support it sveltejs/sites#460).Let's just consider the following code snippet:
We can compile this code using
{generate: "dom", css: "external"}
to obtain two output that are used for CSR:And then we re-compile this code using
{generate: "ssr", css: "none"}
, there will be no CSS or scoped class name generated.As a result, the
output.css
file will not be applied to the SSR HTML:With
{css: "none"}
, the style is completely ignored, which means that Svelte cannot calculate the hash for the scoped class name.A simple solution to this problem is to use
{generate: "ssr", css: "external"}
to generate the HTML for SSR.But, as you can see in the REPL, all CSS are contained in the JS output, resulting in a larger bundle size.
Although bundle size for SSR might not be a problem in many cases, it could be beneficial to remove these unused CSS in JS output. (TBH, in my case, I run svelte inside the service worker, which means bundle size is critical.)
I propose that we reopen #2360 as it provides a simple and effective solution to remove CSS from the JS output.
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: