Skip to content

Commit ab1f7f4

Browse files
fix: transform everything that is not a selector inside :global (#14577)
Fixes #14568
1 parent 88184cd commit ab1f7f4

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

.changeset/red-worms-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: transform everything that is not a selector inside `:global`

packages/svelte/src/compiler/phases/3-transform/css/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const visitors = {
7878
context.state.code.addSourcemapLocation(node.end);
7979
context.next();
8080
},
81-
Atrule(node, { state, next }) {
81+
Atrule(node, { state, next, path }) {
8282
if (is_keyframes_node(node)) {
8383
let start = node.start + node.name.length + 1;
8484
while (state.code.original[start] === ' ') start += 1;
@@ -87,7 +87,7 @@ const visitors = {
8787

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

@@ -134,7 +134,7 @@ const visitors = {
134134
}
135135
}
136136
},
137-
Rule(node, { state, next, visit }) {
137+
Rule(node, { state, next, visit, path }) {
138138
if (state.minify) {
139139
remove_preceding_whitespace(node.start, state);
140140
remove_preceding_whitespace(node.block.end - 1, state);
@@ -154,7 +154,7 @@ const visitors = {
154154
return;
155155
}
156156

157-
if (!is_used(node)) {
157+
if (!is_used(node) && !is_in_global_block(path)) {
158158
if (state.minify) {
159159
state.code.remove(node.start, node.end);
160160
} else {
@@ -182,20 +182,20 @@ const visitors = {
182182
state.code.appendLeft(node.block.end, '*/');
183183
}
184184

185-
// don't recurse into selector or body
185+
// don't recurse into selectors but visit the body
186+
visit(node.block);
186187
return;
187188
}
188-
189-
// don't recurse into body
190-
visit(node.prelude);
191-
return;
192189
}
193190

194191
next();
195192
},
196193
SelectorList(node, { state, next, path }) {
197-
// Only add comments if we're not inside a complex selector that itself is unused
198-
if (!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)) {
194+
// Only add comments if we're not inside a complex selector that itself is unused or a global block
195+
if (
196+
!is_in_global_block(path) &&
197+
!path.find((n) => n.type === 'ComplexSelector' && !n.metadata.used)
198+
) {
199199
const children = node.children;
200200
let pruning = false;
201201
let prune_start = children[0].start;
@@ -359,6 +359,14 @@ const visitors = {
359359
}
360360
};
361361

362+
/**
363+
*
364+
* @param {Array<Css.Node>} path
365+
*/
366+
function is_in_global_block(path) {
367+
return path.some((node) => node.type === 'Rule' && node.metadata.is_global_block);
368+
}
369+
362370
/**
363371
* @param {Css.PseudoClassSelector} selector
364372
* @param {Css.Combinator | null} combinator

packages/svelte/tests/css/samples/global-block/expected.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,20 @@
6969
color: red;
7070
}
7171
}*/
72+
/* :global{*/
73+
.x{
74+
animation: svelte-xyz-test 1s;
75+
}
76+
77+
@keyframes test-in{
78+
to{
79+
opacity: 1;
80+
}
81+
}
82+
/*}*/
83+
84+
@keyframes svelte-xyz-test{
85+
to{
86+
opacity: 1;
87+
}
88+
}

packages/svelte/tests/css/samples/global-block/input.svelte

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@
7171
color: red;
7272
}
7373
}
74+
:global{
75+
.x{
76+
animation: test 1s;
77+
}
78+
79+
@keyframes test-in{
80+
to{
81+
opacity: 1;
82+
}
83+
}
84+
}
85+
86+
@keyframes test{
87+
to{
88+
opacity: 1;
89+
}
90+
}
7491
</style>

0 commit comments

Comments
 (0)