Skip to content

Commit

Permalink
feat: smaller destructor chunk (#8763)
Browse files Browse the repository at this point in the history
technically a breaking change because someone with a mutation observer could rely on the order of operations

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
  • Loading branch information
gtm-nayan and dummdidumm authored Jun 20, 2023
1 parent 88504ee commit 914529f
Show file tree
Hide file tree
Showing 83 changed files with 523 additions and 243 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-tigers-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: smaller minified output for destructor chunks
1 change: 1 addition & 0 deletions documentation/docs/05-misc/04-v4-migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ The order in which preprocessors are applied has changed. Now, preprocessors are
- people implementing their own stores from scratch using the `StartStopNotifier` interface (which is passed to the create function of `writable` etc) from `svelte/store` now need to pass an update function in addition to the set function. This has no effect on people using stores or creating stores using the existing Svelte stores. ([#6750](https://github.com/sveltejs/svelte/issues/6750))
- `derived` will now throw an error on falsy values instead of stores passed to it. ([#7947](https://github.com/sveltejs/svelte/issues/7947))
- type definitions for `svelte/internal` were removed to further discourage usage of those internal methods which are not public API. Most of these will likely change for Svelte 5
- Removal of DOM nodes is now batched which slightly changes its order, which might affect the order of events fired if you're using a MutationObserver on these elements ([#8763](https://github.com/sveltejs/svelte/pull/8763))
22 changes: 21 additions & 1 deletion packages/svelte/src/compiler/compile/render_dom/Block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { b, x } from 'code-red';
import { is_head } from './wrappers/shared/is_head.js';
import { regex_double_quotes } from '../../utils/patterns.js';
import { flatten } from '../../utils/flatten.js';

export default class Block {
/**
Expand Down Expand Up @@ -380,8 +381,27 @@ export default class Block {
if (this.chunks.destroy.length === 0) {
properties.destroy = noop;
} else {
const dispose_elements = [];
// Coalesce if blocks with the same condition
const others = flatten(this.chunks.destroy).filter(
/** @param {import('estree').Node} node */
(node) => {
if (
node.type === 'IfStatement' &&
node.test.type === 'Identifier' &&
node.test.name === 'detaching'
) {
dispose_elements.push(node.consequent);
return false;
} else {
return true;
}
}
);

properties.destroy = x`function #destroy(detaching) {
${this.chunks.destroy}
${dispose_elements.length ? b`if (detaching) { ${dispose_elements} }` : null}
${others}
}`;
}
if (!this.renderer.component.compile_options.dev) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(button);
if (detaching) {
detach(button);
}

mounted = false;
dispose();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/test/js/samples/action/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(a);
if (detaching) {
detach(a);
}

mounted = false;
dispose();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/test/js/samples/bind-open/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(details);
if (detaching) {
detach(details);
}

mounted = false;
dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(div);
if (detaching) {
detach(div);
}

div_resize_listener();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(input0);
if (detaching) detach(t);
if (detaching) detach(input1);
if (detaching) {
detach(input0);
detach(t);
detach(input1);
}

mounted = false;
run_all(dispose);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(p);
if (detaching) detach(t1);
if (detaching) detach(input);
if (detaching) {
detach(p);
detach(t1);
detach(input);
}

mounted = false;
dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d: function destroy(detaching) {
if (detaching) detach_dev(p);
if (detaching) {
detach_dev(p);
}
}
};

Expand Down Expand Up @@ -197,4 +199,4 @@ class Component extends SvelteComponentDev {
}

export default Component;
export { moduleLiveBinding, moduleConstantProps };
export { moduleLiveBinding, moduleConstantProps };
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ function create_fragment(ctx) {
current = false;
},
d(detaching) {
if (detaching) detach(div);
if (detaching) detach(t);
if (detaching) {
detach(div);
detach(t);
}

destroy_component(component, detaching);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(p);
if (detaching) {
detach(p);
}
}
};
}
Expand All @@ -60,4 +62,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ function create_fragment(ctx) {
current = false;
},
d(detaching) {
if (detaching) {
detach(t0);
detach(t1);
detach(input);
}

destroy_component(foo, detaching);
if (detaching) detach(t0);
destroy_component(bar, detaching);
if (detaching) detach(t1);
if (detaching) detach(input);
mounted = false;
dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(h1);
if (detaching) {
detach(h1);
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(h1);
if (detaching) detach(t1);
if (detaching) detach(button);
if (detaching) {
detach(h1);
detach(t1);
detach(button);
}

mounted = false;
dispose();
}
Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(div);
if (detaching) {
detach(div);
}
}
};
}
Expand All @@ -42,4 +44,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(div);
if (detaching) {
detach(div);
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(h1);
if (detaching) {
detach(h1);
}
}
};
}
Expand Down
8 changes: 5 additions & 3 deletions packages/svelte/test/js/samples/data-attribute/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(div0);
if (detaching) detach(t);
if (detaching) detach(div1);
if (detaching) {
detach(div0);
detach(t);
detach(div1);
}
}
};
}
Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/test/js/samples/debug-empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d: function destroy(detaching) {
if (detaching) detach_dev(h1);
if (detaching) detach_dev(t3);
if (detaching) {
detach_dev(h1);
detach_dev(t3);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ function create_each_block(ctx) {
}
},
d: function destroy(detaching) {
if (detaching) detach_dev(span);
if (detaching) detach_dev(t1);
if (detaching) {
detach_dev(span);
detach_dev(t1);
}
}
};

Expand Down Expand Up @@ -152,9 +154,12 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d: function destroy(detaching) {
if (detaching) {
detach_dev(t0);
detach_dev(p);
}

destroy_each(each_blocks, detaching);
if (detaching) detach_dev(t0);
if (detaching) detach_dev(p);
}
};

Expand Down
13 changes: 9 additions & 4 deletions packages/svelte/test/js/samples/debug-foo/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ function create_each_block(ctx) {
}
},
d: function destroy(detaching) {
if (detaching) detach_dev(span);
if (detaching) detach_dev(t1);
if (detaching) {
detach_dev(span);
detach_dev(t1);
}
}
};

Expand Down Expand Up @@ -146,9 +148,12 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d: function destroy(detaching) {
if (detaching) {
detach_dev(t0);
detach_dev(p);
}

destroy_each(each_blocks, detaching);
if (detaching) detach_dev(t0);
if (detaching) detach_dev(p);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ function create_each_block(ctx) {
},
p: noop,
d: function destroy(detaching) {
if (detaching) detach_dev(t0);
if (detaching) detach_dev(t1);
if (detaching) {
detach_dev(t0);
detach_dev(t1);
}
}
};

Expand Down Expand Up @@ -119,8 +121,11 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d: function destroy(detaching) {
if (detaching) {
detach_dev(each_1_anchor);
}

destroy_each(each_blocks, detaching);
if (detaching) detach_dev(each_1_anchor);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function create_each_block(ctx) {
if (dirty & /*createElement*/ 1 && t_value !== (t_value = /*node*/ ctx[1] + "")) set_data(t, t_value);
},
d(detaching) {
if (detaching) detach(span);
if (detaching) {
detach(span);
}
}
};
}
Expand Down Expand Up @@ -98,8 +100,11 @@ function create_fragment(ctx) {
i: noop,
o: noop,
d(detaching) {
if (detaching) {
detach(each_1_anchor);
}

destroy_each(each_blocks, detaching);
if (detaching) detach(each_1_anchor);
}
};
}
Expand Down
Loading

0 comments on commit 914529f

Please sign in to comment.