Skip to content

chore: code-golf a bit #10893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/svelte/scripts/check-treeshakeability.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const bundle = await bundle_code(
).js.code
);

if (!bundle.includes('current_hydration_fragment')) {
if (!bundle.includes('hydrate_nodes')) {
// eslint-disable-next-line no-console
console.error(`✅ Hydration code treeshakeable`);
} else {
Expand Down
23 changes: 11 additions & 12 deletions packages/svelte/src/internal/client/dom/blocks/css-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ export function css_props(anchor, is_html, props, component) {
hydrate_block_anchor(anchor);

/** @type {HTMLElement | SVGElement} */
let tag;
let element;

/** @type {Text | Comment} */
let component_anchor;

if (hydrating) {
// Hydration: css props element is surrounded by a ssr comment ...
tag = /** @type {HTMLElement | SVGElement} */ (hydrate_nodes[0]);
element = /** @type {HTMLElement | SVGElement} */ (hydrate_nodes[0]);
// ... and the child(ren) of the css props element is also surround by a ssr comment
component_anchor = /** @type {Comment} */ (tag.firstChild);
component_anchor = /** @type {Comment} */ (element.firstChild);
} else {
if (is_html) {
tag = document.createElement('div');
tag.style.display = 'contents';
element = document.createElement('div');
element.style.display = 'contents';
} else {
tag = document.createElementNS(namespace_svg, 'g');
element = document.createElementNS(namespace_svg, 'g');
}

anchor.before(tag);
component_anchor = empty();
tag.appendChild(component_anchor);
anchor.before(element);
component_anchor = element.appendChild(empty());
}

component(component_anchor);
Expand All @@ -48,18 +47,18 @@ export function css_props(anchor, is_html, props, component) {

for (const key in current_props) {
if (!(key in next_props)) {
tag.style.removeProperty(key);
element.style.removeProperty(key);
}
}

for (const key in next_props) {
tag.style.setProperty(key, next_props[key]);
element.style.setProperty(key, next_props[key]);
}

current_props = next_props;
});

effect.ondestroy = () => {
remove(tag);
remove(element);
};
}
10 changes: 3 additions & 7 deletions packages/svelte/src/internal/client/dom/blocks/svelte-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function head(render_fn) {
update_hydrate_nodes(document.head.firstChild);
}

var anchor = document.head.appendChild(empty());

try {
/** @type {import('#client').Dom | null} */
var dom = null;
Expand All @@ -28,13 +30,7 @@ export function head(render_fn) {
head_effect.dom = dom = null;
}

let anchor = null;
if (!hydrating) {
anchor = empty();
document.head.appendChild(anchor);
}

dom = render_fn(anchor) ?? null;
dom = render_fn(hydrating ? null : anchor) ?? null;
});

head_effect.ondestroy = () => {
Expand Down
18 changes: 5 additions & 13 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ export function createRoot() {
* @returns {Exports}
*/
export function mount(component, options) {
const anchor = empty();
options.target.appendChild(anchor);
const anchor = options.target.appendChild(empty());
// Don't flush previous effects to ensure order of outer effects stays consistent
return flush_sync(() => _mount(component, { ...options, anchor }), false);
}
Expand Down Expand Up @@ -148,28 +147,21 @@ export function hydrate(component, options) {
const nodes = update_hydrate_nodes(first_child, true);
set_hydrating(true);

/** @type {null | Text} */
let anchor = null;

if (nodes === null) {
anchor = empty();
container.appendChild(anchor);
}

let finished_hydrating = false;
let hydrated = false;

try {
// Don't flush previous effects to ensure order of outer effects stays consistent
return flush_sync(() => {
const anchor = nodes === null ? container.appendChild(empty()) : null;
const instance = _mount(component, { ...options, anchor });
// flush_sync will run this callback and then synchronously run any pending effects,
// which don't belong to the hydration phase anymore - therefore reset it here
set_hydrating(false);
finished_hydrating = true;
hydrated = true;
return instance;
}, false);
} catch (error) {
if (!finished_hydrating && options.recover !== false && nodes !== null) {
if (!hydrated && options.recover !== false && nodes !== null) {
// eslint-disable-next-line no-console
console.error(
'ERR_SVELTE_HYDRATION_MISMATCH' +
Expand Down