Skip to content

Commit 3707cbb

Browse files
committed
sibling stuff
1 parent 7b6aae1 commit 3707cbb

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,17 +844,17 @@ function get_element_parent(node) {
844844
}
845845

846846
/**
847-
* @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement} element
847+
* @param {Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.RenderTag | Compiler.AST.Component | Compiler.AST.SvelteComponent | Compiler.AST.SvelteSelf} node
848848
* @param {boolean} adjacent_only
849849
* @returns {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.SlotElement | Compiler.AST.RenderTag, NodeExistsValue>}
850850
*/
851-
function get_possible_element_siblings(element, adjacent_only) {
851+
function get_possible_element_siblings(node, adjacent_only) {
852852
/** @type {Map<Compiler.AST.RegularElement | Compiler.AST.SvelteElement | Compiler.AST.SlotElement | Compiler.AST.RenderTag, NodeExistsValue>} */
853853
const result = new Map();
854-
const path = element.metadata.path;
854+
const path = node.metadata.path;
855855

856856
/** @type {Compiler.SvelteNode} */
857-
let current = element;
857+
let current = node;
858858

859859
let i = path.length;
860860

@@ -896,7 +896,20 @@ function get_possible_element_siblings(element, adjacent_only) {
896896

897897
current = path[i];
898898

899-
if (!current || !is_block(current)) break;
899+
if (!current) break;
900+
901+
if (current.type === 'SnippetBlock') {
902+
for (const site of current.metadata.sites) {
903+
const siblings = get_possible_element_siblings(site, adjacent_only);
904+
add_to_map(siblings, result);
905+
906+
if (adjacent_only && current.metadata.sites.size === 1 && has_definite_elements(siblings)) {
907+
return result;
908+
}
909+
}
910+
}
911+
912+
if (!is_block(current)) break;
900913

901914
if (current.type === 'EachBlock' && fragment === current.body) {
902915
// `{#each ...}<a /><b />{/each}` — `<b>` can be previous sibling of `<a />`

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { mark_subtree_dynamic } from './fragment.js';
1616
* @param {Context} context
1717
*/
1818
export function visit_component(node, context) {
19+
node.metadata.path = [...context.path];
20+
1921
// link this node to all the snippets that it could render, so that we can prune CSS correctly
2022
node.metadata.snippets = new Set();
2123

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ export namespace AST {
284284
/** The set of locally-defined snippets that this component tag could render,
285285
* used for CSS pruning purposes */
286286
snippets: Set<SnippetBlock>;
287+
path: SvelteNode[];
287288
};
288289
}
289290

@@ -327,6 +328,7 @@ export namespace AST {
327328
/** The set of locally-defined snippets that this component tag could render,
328329
* used for CSS pruning purposes */
329330
snippets: Set<SnippetBlock>;
331+
path: SvelteNode[];
330332
};
331333
}
332334

@@ -381,6 +383,7 @@ export namespace AST {
381383
/** The set of locally-defined snippets that this component tag could render,
382384
* used for CSS pruning purposes */
383385
snippets: Set<SnippetBlock>;
386+
path: SvelteNode[];
384387
};
385388
}
386389

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
h1.svelte-xyz + p:where(.svelte-xyz) {
2-
color: green;
3-
}
1+
2+
h1.svelte-xyz + p:where(.svelte-xyz) {
3+
color: green;
4+
}

0 commit comments

Comments
 (0)