fix: avoid css tree-shaking for exported Snippet (#18540)

fix #16404 

Currently one file can have multiple exports but only one `cssScopeTo` variable, which is hard coded to `default` inside vite-plugin-svelte.

Therefore set hasGlobal to `true` in case we export a snippet to not have this scoping enabled in v-p-s

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
pull/14594/merge
JY Wey 14 hours ago committed by GitHub
parent 6f5dd04a00
commit b20b2ee85e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: avoid css tree-shaking for exported Snippet

@ -832,6 +832,11 @@ export function analyze_component(root, source, options) {
} else {
e.export_undefined(specifier, name);
}
} else if (binding.initial?.type === 'SnippetBlock') {
// If a snippet is exported, a consumer could only import this named export and not the default export (the component).
// In this case we need to set hasGlobal of our output to true so that e.g. vite-plugin-svelte does not tell Vite to
// tree-shake the CSS if the default export is not used.
analysis.css.has_global = true;
}
}
}

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
hasGlobal: true
});

@ -0,0 +1,13 @@
<script module>
export { outer };
</script>
{#snippet outer()}
<p class="inner"></p>
{/snippet}
<style>
.inner {
color: red;
}
</style>
Loading…
Cancel
Save