Skip to content

Commit 2c16bf3

Browse files
committed
fix: shrink custom element baseline a bit
- use shorter class property names as they are not minified - reuse some dom helper methods #8826
1 parent e3422e1 commit 2c16bf3

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

.changeset/happy-cows-warn.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: shrink custom element baseline a bit

packages/svelte/src/runtime/internal/Component.js

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
start_hydrating,
1414
end_hydrating,
1515
get_custom_elements_slots,
16-
insert
16+
insert,
17+
element,
18+
attr
1719
} from './dom.js';
1820
import { transition_in } from './transitions.js';
1921

@@ -157,23 +159,23 @@ export let SvelteElement;
157159

158160
if (typeof HTMLElement === 'function') {
159161
SvelteElement = class extends HTMLElement {
160-
$$componentCtor;
161-
$$slots;
162-
$$component;
163-
$$connected = false;
164-
$$data = {};
165-
$$reflecting = false;
162+
$$ctor;
163+
$$s;
164+
$$c;
165+
$$cn = false;
166+
$$d = {};
167+
$$r = false;
166168
/** @type {Record<string, CustomElementPropDefinition>} */
167-
$$props_definition = {};
169+
$$p_d = {};
168170
/** @type {Record<string, Function[]>} */
169-
$$listeners = {};
171+
$$l = {};
170172
/** @type {Map<Function, Function>} */
171-
$$listener_unsubscribe_fns = new Map();
173+
$$l_u = new Map();
172174

173175
constructor($$componentCtor, $$slots, use_shadow_dom) {
174176
super();
175-
this.$$componentCtor = $$componentCtor;
176-
this.$$slots = $$slots;
177+
this.$$ctor = $$componentCtor;
178+
this.$$s = $$slots;
177179
if (use_shadow_dom) {
178180
this.attachShadow({ mode: 'open' });
179181
}
@@ -183,42 +185,42 @@ if (typeof HTMLElement === 'function') {
183185
// We can't determine upfront if the event is a custom event or not, so we have to
184186
// listen to both. If someone uses a custom event with the same name as a regular
185187
// browser event, this fires twice - we can't avoid that.
186-
this.$$listeners[type] = this.$$listeners[type] || [];
187-
this.$$listeners[type].push(listener);
188-
if (this.$$component) {
189-
const unsub = this.$$component.$on(type, listener);
190-
this.$$listener_unsubscribe_fns.set(listener, unsub);
188+
this.$$l[type] = this.$$l[type] || [];
189+
this.$$l[type].push(listener);
190+
if (this.$$c) {
191+
const unsub = this.$$c.$on(type, listener);
192+
this.$$l_u.set(listener, unsub);
191193
}
192194
super.addEventListener(type, listener, options);
193195
}
194196

195197
removeEventListener(type, listener, options) {
196198
super.removeEventListener(type, listener, options);
197-
if (this.$$component) {
198-
const unsub = this.$$listener_unsubscribe_fns.get(listener);
199+
if (this.$$c) {
200+
const unsub = this.$$l_u.get(listener);
199201
if (unsub) {
200202
unsub();
201-
this.$$listener_unsubscribe_fns.delete(listener);
203+
this.$$l_u.delete(listener);
202204
}
203205
}
204206
}
205207

206208
async connectedCallback() {
207-
this.$$connected = true;
208-
if (!this.$$component) {
209+
this.$$cn = true;
210+
if (!this.$$c) {
209211
// We wait one tick to let possible child slot elements be created/mounted
210212
await Promise.resolve();
211-
if (!this.$$connected) {
213+
if (!this.$$cn) {
212214
return;
213215
}
214216
function create_slot(name) {
215217
return () => {
216218
let node;
217219
const obj = {
218220
c: function create() {
219-
node = document.createElement('slot');
221+
node = element('slot');
220222
if (name !== 'default') {
221-
node.setAttribute('name', name);
223+
attr(node, 'name', name);
222224
}
223225
},
224226
/**
@@ -239,74 +241,64 @@ if (typeof HTMLElement === 'function') {
239241
}
240242
const $$slots = {};
241243
const existing_slots = get_custom_elements_slots(this);
242-
for (const name of this.$$slots) {
244+
for (const name of this.$$s) {
243245
if (name in existing_slots) {
244246
$$slots[name] = [create_slot(name)];
245247
}
246248
}
247249
for (const attribute of this.attributes) {
248250
// this.$$data takes precedence over this.attributes
249-
const name = this.$$get_prop_name(attribute.name);
250-
if (!(name in this.$$data)) {
251-
this.$$data[name] = get_custom_element_value(
252-
name,
253-
attribute.value,
254-
this.$$props_definition,
255-
'toProp'
256-
);
251+
const name = this.$$g_p(attribute.name);
252+
if (!(name in this.$$d)) {
253+
this.$$d[name] = get_custom_element_value(name, attribute.value, this.$$p_d, 'toProp');
257254
}
258255
}
259-
this.$$component = new this.$$componentCtor({
256+
this.$$c = new this.$$ctor({
260257
target: this.shadowRoot || this,
261258
props: {
262-
...this.$$data,
259+
...this.$$d,
263260
$$slots,
264261
$$scope: {
265262
ctx: []
266263
}
267264
}
268265
});
269-
for (const type in this.$$listeners) {
270-
for (const listener of this.$$listeners[type]) {
271-
const unsub = this.$$component.$on(type, listener);
272-
this.$$listener_unsubscribe_fns.set(listener, unsub);
266+
for (const type in this.$$l) {
267+
for (const listener of this.$$l[type]) {
268+
const unsub = this.$$c.$on(type, listener);
269+
this.$$l_u.set(listener, unsub);
273270
}
274271
}
275-
this.$$listeners = {};
272+
this.$$l = {};
276273
}
277274
}
278275

279276
// We don't need this when working within Svelte code, but for compatibility of people using this outside of Svelte
280277
// and setting attributes through setAttribute etc, this is helpful
281278
attributeChangedCallback(attr, _oldValue, newValue) {
282-
if (this.$$reflecting) return;
283-
attr = this.$$get_prop_name(attr);
284-
this.$$data[attr] = get_custom_element_value(
285-
attr,
286-
newValue,
287-
this.$$props_definition,
288-
'toProp'
289-
);
290-
this.$$component?.$set({ [attr]: this.$$data[attr] });
279+
if (this.$$r) return;
280+
attr = this.$$g_p(attr);
281+
this.$$d[attr] = get_custom_element_value(attr, newValue, this.$$p_d, 'toProp');
282+
this.$$c?.$set({ [attr]: this.$$d[attr] });
291283
}
292284

293285
disconnectedCallback() {
294-
this.$$connected = false;
286+
this.$$cn = false;
295287
// In a microtask, because this could be a move within the DOM
296288
Promise.resolve().then(() => {
297-
if (!this.$$connected) {
298-
this.$$component.$destroy();
299-
this.$$component = undefined;
289+
if (!this.$$cn) {
290+
this.$$c.$destroy();
291+
this.$$c = undefined;
300292
}
301293
});
302294
}
303295

304-
$$get_prop_name(attribute_name) {
296+
$$g_p(attribute_name) {
305297
return (
306-
Object.keys(this.$$props_definition).find(
298+
Object.keys(this.$$p_d).find(
307299
(key) =>
308-
this.$$props_definition[key].attribute === attribute_name ||
309-
(!this.$$props_definition[key].attribute && key.toLowerCase() === attribute_name)
300+
this.$$p_d[key].attribute === attribute_name ||
301+
(!this.$$p_d[key].attribute && key.toLowerCase() === attribute_name)
310302
) || attribute_name
311303
);
312304
}
@@ -371,7 +363,7 @@ export function create_custom_element(
371363
const Class = class extends SvelteElement {
372364
constructor() {
373365
super(Component, slots, use_shadow_dom);
374-
this.$$props_definition = props_definition;
366+
this.$$p_d = props_definition;
375367
}
376368
static get observedAttributes() {
377369
return Object.keys(props_definition).map((key) =>
@@ -382,16 +374,14 @@ export function create_custom_element(
382374
Object.keys(props_definition).forEach((prop) => {
383375
Object.defineProperty(Class.prototype, prop, {
384376
get() {
385-
return this.$$component && prop in this.$$component
386-
? this.$$component[prop]
387-
: this.$$data[prop];
377+
return this.$$c && prop in this.$$c ? this.$$c[prop] : this.$$d[prop];
388378
},
389379
set(value) {
390380
value = get_custom_element_value(prop, value, props_definition);
391-
this.$$data[prop] = value;
392-
this.$$component?.$set({ [prop]: value });
381+
this.$$d[prop] = value;
382+
this.$$c?.$set({ [prop]: value });
393383
if (props_definition[prop].reflect) {
394-
this.$$reflecting = true;
384+
this.$$r = true;
395385
const attribute_value = get_custom_element_value(
396386
prop,
397387
value,
@@ -403,15 +393,15 @@ export function create_custom_element(
403393
} else {
404394
this.setAttribute(props_definition[prop].attribute || prop, attribute_value);
405395
}
406-
this.$$reflecting = false;
396+
this.$$r = false;
407397
}
408398
}
409399
});
410400
});
411401
accessors.forEach((accessor) => {
412402
Object.defineProperty(Class.prototype, accessor, {
413403
get() {
414-
return this.$$component?.[accessor];
404+
return this.$$c?.[accessor];
415405
}
416406
});
417407
});

0 commit comments

Comments
 (0)