Skip to content

Commit 5e2bf13

Browse files
committed
fix
1 parent d62a904 commit 5e2bf13

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,7 @@ function is_global_block_selector(simple_selector) {
3636
*/
3737
function is_unscoped_global(path) {
3838
// remove every at rule or stylesheet and the current rule in case is passed in from `ComplexSelector`
39-
const parents = path.filter((parent) => parent.type !== 'Atrule' && parent.type !== 'StyleSheet');
40-
41-
let unscoped_global = true;
42-
43-
// no parents means we are on top
44-
if (parents.length > 0) {
45-
// let's check that everything in the path is not a global block
46-
unscoped_global = parents.every((parent) => {
47-
return parent.type !== 'Rule' || parent.metadata.is_global_block;
48-
});
49-
}
50-
51-
return unscoped_global;
39+
return path.filter((node) => node.type === 'Rule').every((node) => node.metadata.is_global);
5240
}
5341

5442
/**
@@ -203,14 +191,6 @@ const css_visitors = {
203191
Rule(node, context) {
204192
node.metadata.parent_rule = context.state.rule;
205193

206-
// if this rule has a ComplexSelector whose RelativeSelector children are all
207-
// `:global(...)`, and the rule contains declarations (rather than just
208-
// nested rules) then the component as a whole includes global CSS
209-
context.state.has_global.value ||=
210-
node.block.children.filter((child) => child.type === 'Declaration').length > 0 &&
211-
node.prelude.children.some((selector) => selector.children.every(is_global)) &&
212-
is_unscoped_global(context.path);
213-
214194
node.metadata.is_global_block = node.prelude.children.some((selector) => {
215195
let is_global_block = false;
216196

@@ -278,16 +258,29 @@ const css_visitors = {
278258
}
279259
}
280260

281-
context.next({
282-
...context.state,
283-
rule: node
284-
});
261+
const state = { ...context.state, rule: node };
285262

286-
node.metadata.has_local_selectors = node.prelude.children.some((selector) => {
287-
return selector.children.some(
288-
({ metadata }) => !metadata.is_global && !metadata.is_global_like
289-
);
290-
});
263+
// visit selector list first, to populate child selector metadata
264+
context.visit(node.prelude, state);
265+
266+
node.metadata.is_global = node.prelude.children.some((selector) =>
267+
selector.children.every(({ metadata }) => metadata.is_global || metadata.is_global_like)
268+
);
269+
270+
node.metadata.has_local_selectors = node.prelude.children.some((selector) =>
271+
selector.children.some(({ metadata }) => !metadata.is_global && !metadata.is_global_like)
272+
);
273+
274+
// if this rule has a ComplexSelector whose RelativeSelector children are all
275+
// `:global(...)`, and the rule contains declarations (rather than just
276+
// nested rules) then the component as a whole includes global CSS
277+
context.state.has_global.value ||=
278+
node.metadata.is_global &&
279+
node.block.children.filter((child) => child.type === 'Declaration').length > 0 &&
280+
is_unscoped_global(context.path);
281+
282+
// visit block list, so parent rule metadata is populated
283+
context.visit(node.block, state);
291284
},
292285
NestingSelector(node, context) {
293286
const rule = /** @type {AST.CSS.Rule} */ (context.state.rule);

packages/svelte/src/compiler/types/css.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export namespace _CSS {
3434
metadata: {
3535
parent_rule: null | Rule;
3636
has_local_selectors: boolean;
37+
/**
38+
* `true` if the rule contains a ComplexSelector whose RelativeSelectors are all global or global-like
39+
*/
40+
is_global: boolean;
3741
/**
3842
* `true` if the rule contains a `:global` selector, and therefore everything inside should be unscoped
3943
*/

0 commit comments

Comments
 (0)