diff --git a/.changeset/olive-pillows-fetch.md b/.changeset/olive-pillows-fetch.md new file mode 100644 index 0000000000..c671a2dc13 --- /dev/null +++ b/.changeset/olive-pillows-fetch.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: account for global block in `is_empty` diff --git a/packages/svelte/src/compiler/phases/3-transform/css/index.js b/packages/svelte/src/compiler/phases/3-transform/css/index.js index 75b63260d5..c5255c4bc3 100644 --- a/packages/svelte/src/compiler/phases/3-transform/css/index.js +++ b/packages/svelte/src/compiler/phases/3-transform/css/index.js @@ -142,7 +142,7 @@ const visitors = { // keep empty rules in dev, because it's convenient to // see them in devtools - if (!dev && is_empty(node)) { + if (!dev && is_empty(node, is_in_global_block(path))) { if (state.minify) { state.code.remove(node.start, node.end); } else { @@ -398,8 +398,11 @@ function remove_preceding_whitespace(end, state) { if (start < end) state.code.remove(start, end); } -/** @param {Css.Rule} rule */ -function is_empty(rule) { +/** + * @param {Css.Rule} rule + * @param {boolean} is_in_global_block + */ +function is_empty(rule, is_in_global_block) { if (rule.metadata.is_global_block) { return rule.block.children.length === 0; } @@ -410,7 +413,9 @@ function is_empty(rule) { } if (child.type === 'Rule') { - if (is_used(child) && !is_empty(child)) return false; + if ((is_used(child) || is_in_global_block) && !is_empty(child, is_in_global_block)) { + return false; + } } if (child.type === 'Atrule') { diff --git a/packages/svelte/tests/css/samples/global-with-root/expected.css b/packages/svelte/tests/css/samples/global-with-root/expected.css new file mode 100644 index 0000000000..0c812ea27d --- /dev/null +++ b/packages/svelte/tests/css/samples/global-with-root/expected.css @@ -0,0 +1,8 @@ + + /* :global{*/ + :root{ + .use{ + color: red; + } + } + /*}*/ diff --git a/packages/svelte/tests/css/samples/global-with-root/input.svelte b/packages/svelte/tests/css/samples/global-with-root/input.svelte new file mode 100644 index 0000000000..47ed8b4dc0 --- /dev/null +++ b/packages/svelte/tests/css/samples/global-with-root/input.svelte @@ -0,0 +1,9 @@ +