Skip to content

Commit e006ac5

Browse files
committed
always inject styles when compiling as a custom element
1 parent 59a3795 commit e006ac5

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

.changeset/wise-hairs-pay.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: always inject styles when compiling as a custom element

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ export function analyze_component(root, source, options) {
451451
}
452452
}
453453

454+
const is_custom_element = !!options.customElementOptions || options.customElement;
455+
454456
// TODO remove all the ?? stuff, we don't need it now that we're validating the config
455457
/** @type {ComponentAnalysis} */
456458
const analysis = {
@@ -500,13 +502,13 @@ export function analyze_component(root, source, options) {
500502
needs_props: false,
501503
event_directive_node: null,
502504
uses_event_attributes: false,
503-
custom_element: options.customElementOptions ?? options.customElement,
504-
inject_styles: options.css === 'injected' || options.customElement,
505-
accessors: options.customElement
506-
? true
507-
: (runes ? false : !!options.accessors) ||
508-
// because $set method needs accessors
509-
options.compatibility?.componentApi === 4,
505+
custom_element: is_custom_element,
506+
inject_styles: options.css === 'injected' || is_custom_element,
507+
accessors:
508+
is_custom_element ||
509+
(runes ? false : !!options.accessors) ||
510+
// because $set method needs accessors
511+
options.compatibility?.componentApi === 4,
510512
reactive_statements: new Map(),
511513
binding_groups: new Map(),
512514
slot_names: new Map(),

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,9 @@ export function client_component(analysis, options) {
621621
);
622622
}
623623

624-
if (analysis.custom_element) {
625-
const ce = analysis.custom_element;
624+
const ce = options.customElementOptions ?? options.customElement;
625+
626+
if (ce) {
626627
const ce_props = typeof ce === 'boolean' ? {} : ce.props || {};
627628

628629
/** @type {ESTree.Property[]} */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<svelte:options customElement={{ tag: 'my-thing' }} />
2+
3+
<h1>hello</h1>
4+
5+
<style>
6+
h1 {
7+
color: red;
8+
}
9+
</style>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
mode: ['client'],
6+
async test({ assert, target }) {
7+
const thing = /** @type HTMLElement & { object: { test: true }; } */ (
8+
target.querySelector('my-thing')
9+
);
10+
11+
await tick();
12+
13+
assert.include(thing.shadowRoot?.innerHTML, 'red');
14+
}
15+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import './Thing.svelte';
3+
</script>
4+
5+
<my-thing></my-thing>

0 commit comments

Comments
 (0)