Skip to content

Commit 9aa5449

Browse files
committed
only inject $$props when necessary
1 parent e4ca8eb commit 9aa5449

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,13 @@ export function client_component(source, analysis, options) {
392392

393393
append_styles();
394394

395-
if (
395+
const should_inject_context =
396396
analysis.needs_context ||
397397
analysis.reactive_statements.size > 0 ||
398398
component_returned_object.length > 0 ||
399-
options.dev
400-
) {
399+
options.dev;
400+
401+
if (should_inject_context) {
401402
component_block.body.unshift(b.stmt(b.call('$.push', ...push_args)));
402403

403404
component_block.body.push(
@@ -439,12 +440,32 @@ export function client_component(source, analysis, options) {
439440
component_block.body.unshift(b.const('$$slots', b.call('$.sanitize_slots', b.id('$$props'))));
440441
}
441442

443+
let should_inject_props =
444+
should_inject_context ||
445+
analysis.uses_props ||
446+
analysis.uses_rest_props ||
447+
analysis.uses_slots ||
448+
analysis.slot_names.size > 0;
449+
450+
if (!should_inject_props) {
451+
for (const declaration of analysis.instance.scope.declarations.values()) {
452+
if (
453+
declaration.kind === 'prop' ||
454+
declaration.kind === 'bindable_prop' ||
455+
declaration.kind === 'rest_prop'
456+
) {
457+
should_inject_props = true;
458+
break;
459+
}
460+
}
461+
}
462+
442463
const body = [
443464
...state.hoisted,
444465
...module.body,
445466
b.function_declaration(
446467
b.id(analysis.name),
447-
[b.id('$$anchor'), b.id('$$props')],
468+
should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')],
448469
component_block
449470
)
450471
];

packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import TextInput from './Child.svelte';
77
var root_1 = $.template(`Something`, 1);
88
var root = $.template(`<!> `, 1);
99

10-
function Bind_component_snippet($$anchor, $$props) {
10+
function Bind_component_snippet($$anchor) {
1111
let value = $.source('');
1212
const _snippet = snippet;
1313
var fragment_1 = root();

packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as $ from "svelte/internal/client";
55

66
var root = $.template(`<div></div> <svg></svg> <custom-element></custom-element> <div></div> <svg></svg> <custom-element></custom-element>`, 3);
77

8-
function Main($$anchor, $$props) {
8+
function Main($$anchor) {
99
// needs to be a snapshot test because jsdom does auto-correct the attribute casing
1010
let x = 'test';
1111
let y = () => 'test';

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import "svelte/internal/disclose-version";
44
import * as $ from "svelte/internal/client";
55

6-
function Function_prop_no_getter($$anchor, $$props) {
6+
function Function_prop_no_getter($$anchor) {
77
let count = $.source(0);
88

99
function onmouseup() {

packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function reset(_, str, tpl) {
1212

1313
var root = $.template(`<input> <input> <button>reset</button>`, 1);
1414

15-
function State_proxy_literal($$anchor, $$props) {
15+
function State_proxy_literal($$anchor) {
1616
let str = $.source('');
1717
let tpl = $.source(``);
1818
var fragment = root();

0 commit comments

Comments
 (0)