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: support ?inline query on svelte style virtual modules #1024

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Nov 15, 2024

In Vite 6, importing CSS in SSR now no longer return a default export of the transformed CSS file, you'd need to use ?inline instead. This was already pushed for in Vite 5 but we forgot to remove support in SSR.

That means frameworks who do CSS crawling to prevent FOUC will now have to use ?inline to get the transformed CSS, which most already did. However, there's a quirk where v-p-s always strips out the ?inline here:

if (svelteRequest.query.type === 'style' && !svelteRequest.raw) {
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
log.debug(
`resolveId resolved virtual css module ${svelteRequest.cssId}`,
undefined,
'resolve'
);
return svelteRequest.cssId;
}

This was fine for Vite 5 as mentioned above, Vite still supported default exports of CSS files in SSR, but with Vite 6 it is now empty. For v-p-s to properly support ?inline, it needs to leave the query as is, similar to ?raw handling.


In practice, this isn't a breaking change, however due to some unintended reliance of the old behaviour, the SvelteKit tests here will fail due to HMR (added a workaround here for now). I sent a PR upstream to fix this sveltejs/kit#13007 and if we want to play it safe for now, we can merge this only in the next major.

With this merged, it should unblock Vite 6 support for CSS FOUC for both SvelteKit and Astro.

optimizeDeps: {
// eagerly include these, otherwise vite optimizer might interfere with restarting while the test is running
include: ['svelte-i18n', 'e2e-test-dep-svelte-api-only']
}
};

export default config;
/**
* Workaround until https://github.com/sveltejs/kit/pull/13007 is merged
Copy link
Member

Choose a reason for hiding this comment

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

lets merge the kit PR first then, really not a fan of extra code in tests to work around issues in the setup being tested. users don't have the same luxury so this is just hiding an issue.

would rather merge this with a failed test than hiding it if we can't get the kit PR released in time

@@ -162,4 +162,11 @@ describe.runIf(!isBuild)('ssrLoadModule', () => {
const result = await ssrLoadDummy('?raw&svelte&type=style');
await expect(result).toMatchFileSnapshot(snapshotFilename('ssr-style'));
});
test('?inline&svelte&type=style&lang.css', async () => {
// Preload Dummy.svelte first so its CSS is processed in the module graph, otherwise loading
Copy link
Member

Choose a reason for hiding this comment

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

wait, wouldn't this also affect real use if the inline style is explicitly asked for without the .svelte being processed first? The solution here would be to compile the file if cache is empty instead of just bailing.

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