Skip to content

Commit 914529f

Browse files
feat: smaller destructor chunk (#8763)
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>
1 parent 88504ee commit 914529f

File tree

83 files changed

+523
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+523
-243
lines changed

.changeset/eighty-tigers-rhyme.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+
feat: smaller minified output for destructor chunks

documentation/docs/05-misc/04-v4-migration-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ The order in which preprocessors are applied has changed. Now, preprocessors are
154154
- 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))
155155
- `derived` will now throw an error on falsy values instead of stores passed to it. ([#7947](https://github.com/sveltejs/svelte/issues/7947))
156156
- 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
157+
- 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))

packages/svelte/src/compiler/compile/render_dom/Block.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { b, x } from 'code-red';
22
import { is_head } from './wrappers/shared/is_head.js';
33
import { regex_double_quotes } from '../../utils/patterns.js';
4+
import { flatten } from '../../utils/flatten.js';
45

56
export default class Block {
67
/**
@@ -380,8 +381,27 @@ export default class Block {
380381
if (this.chunks.destroy.length === 0) {
381382
properties.destroy = noop;
382383
} else {
384+
const dispose_elements = [];
385+
// Coalesce if blocks with the same condition
386+
const others = flatten(this.chunks.destroy).filter(
387+
/** @param {import('estree').Node} node */
388+
(node) => {
389+
if (
390+
node.type === 'IfStatement' &&
391+
node.test.type === 'Identifier' &&
392+
node.test.name === 'detaching'
393+
) {
394+
dispose_elements.push(node.consequent);
395+
return false;
396+
} else {
397+
return true;
398+
}
399+
}
400+
);
401+
383402
properties.destroy = x`function #destroy(detaching) {
384-
${this.chunks.destroy}
403+
${dispose_elements.length ? b`if (detaching) { ${dispose_elements} }` : null}
404+
${others}
385405
}`;
386406
}
387407
if (!this.renderer.component.compile_options.dev) {

packages/svelte/test/js/samples/action-custom-event-handler/expected.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ function create_fragment(ctx) {
3636
i: noop,
3737
o: noop,
3838
d(detaching) {
39-
if (detaching) detach(button);
39+
if (detaching) {
40+
detach(button);
41+
}
42+
4043
mounted = false;
4144
dispose();
4245
}

packages/svelte/test/js/samples/action/expected.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function create_fragment(ctx) {
3535
i: noop,
3636
o: noop,
3737
d(detaching) {
38-
if (detaching) detach(a);
38+
if (detaching) {
39+
detach(a);
40+
}
41+
3942
mounted = false;
4043
dispose();
4144
}

packages/svelte/test/js/samples/bind-open/expected.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ function create_fragment(ctx) {
3737
i: noop,
3838
o: noop,
3939
d(detaching) {
40-
if (detaching) detach(details);
40+
if (detaching) {
41+
detach(details);
42+
}
43+
4144
mounted = false;
4245
dispose();
4346
}

packages/svelte/test/js/samples/bind-width-height/expected.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ function create_fragment(ctx) {
2929
i: noop,
3030
o: noop,
3131
d(detaching) {
32-
if (detaching) detach(div);
32+
if (detaching) {
33+
detach(div);
34+
}
35+
3336
div_resize_listener();
3437
}
3538
};

packages/svelte/test/js/samples/bindings-readonly-order/expected.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ function create_fragment(ctx) {
4646
i: noop,
4747
o: noop,
4848
d(detaching) {
49-
if (detaching) detach(input0);
50-
if (detaching) detach(t);
51-
if (detaching) detach(input1);
49+
if (detaching) {
50+
detach(input0);
51+
detach(t);
52+
detach(input1);
53+
}
54+
5255
mounted = false;
5356
run_all(dispose);
5457
}

packages/svelte/test/js/samples/capture-inject-dev-only/expected.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ function create_fragment(ctx) {
5252
i: noop,
5353
o: noop,
5454
d(detaching) {
55-
if (detaching) detach(p);
56-
if (detaching) detach(t1);
57-
if (detaching) detach(input);
55+
if (detaching) {
56+
detach(p);
57+
detach(t1);
58+
detach(input);
59+
}
60+
5861
mounted = false;
5962
dispose();
6063
}

packages/svelte/test/js/samples/capture-inject-state/expected.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ function create_fragment(ctx) {
7575
i: noop,
7676
o: noop,
7777
d: function destroy(detaching) {
78-
if (detaching) detach_dev(p);
78+
if (detaching) {
79+
detach_dev(p);
80+
}
7981
}
8082
};
8183

@@ -197,4 +199,4 @@ class Component extends SvelteComponentDev {
197199
}
198200

199201
export default Component;
200-
export { moduleLiveBinding, moduleConstantProps };
202+
export { moduleLiveBinding, moduleConstantProps };

0 commit comments

Comments
 (0)