Skip to content

fix: transform everything that is not a selector inside :global #14577

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

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-worms-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: transform everything that is not a selector inside `:global`
30 changes: 19 additions & 11 deletions packages/svelte/src/compiler/phases/3-transform/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const visitors = {
context.state.code.addSourcemapLocation(node.end);
context.next();
},
Atrule(node, { state, next }) {
Atrule(node, { state, next, path }) {
if (is_keyframes_node(node)) {
let start = node.start + node.name.length + 1;
while (state.code.original[start] === ' ') start += 1;
Expand All @@ -87,7 +87,7 @@ const visitors = {

if (node.prelude.startsWith('-global-')) {
state.code.remove(start, start + 8);
} else {
} else if (!is_in_global_block(path)) {
state.code.prependRight(start, `${state.hash}-`);
}

Expand Down Expand Up @@ -134,7 +134,7 @@ const visitors = {
}
}
},
Rule(node, { state, next, visit }) {
Rule(node, { state, next, visit, path }) {
if (state.minify) {
remove_preceding_whitespace(node.start, state);
remove_preceding_whitespace(node.block.end - 1, state);
Expand All @@ -154,7 +154,7 @@ const visitors = {
return;
}

if (!is_used(node)) {
if (!is_used(node) && !is_in_global_block(path)) {
if (state.minify) {
state.code.remove(node.start, node.end);
} else {
Expand Down Expand Up @@ -182,20 +182,20 @@ const visitors = {
state.code.appendLeft(node.block.end, '*/');
}

// don't recurse into selector or body
// don't recurse into selectors but visit the body
visit(node.block);
return;
}

// don't recurse into body
visit(node.prelude);
return;
}

next();
},
SelectorList(node, { state, next, path }) {
// Only add comments if we're not inside a complex selector that itself is unused
if (!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)) {
// Only add comments if we're not inside a complex selector that itself is unused or a global block
if (
!is_in_global_block(path) &&
!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)
) {
const children = node.children;
let pruning = false;
let prune_start = children[0].start;
Expand Down Expand Up @@ -359,6 +359,14 @@ const visitors = {
}
};

/**
*
* @param {Array<Css.Node>} path
*/
function is_in_global_block(path) {
return path.some((node) => node.type === 'Rule' && node.metadata.is_global_block);
}

/**
* @param {Css.PseudoClassSelector} selector
* @param {Css.Combinator | null} combinator
Expand Down
17 changes: 17 additions & 0 deletions packages/svelte/tests/css/samples/global-block/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@
color: red;
}
}*/
/* :global{*/
.x{
animation: svelte-xyz-test 1s;
}

@keyframes test-in{
to{
opacity: 1;
}
}
/*}*/

@keyframes svelte-xyz-test{
to{
opacity: 1;
}
}
17 changes: 17 additions & 0 deletions packages/svelte/tests/css/samples/global-block/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@
color: red;
}
}
:global{
.x{
animation: test 1s;
}

@keyframes test-in{
to{
opacity: 1;
}
}
}

@keyframes test{
to{
opacity: 1;
}
}
</style>
Loading