Skip to content

Commit 9a887f8

Browse files
chore: directly export function in non-HMR mode (#11333)
* chore: directly export function in non-HMR mode * remove some more noise * update snapshots * Update sites/svelte-5-preview/src/lib/workers/compiler/index.js Co-authored-by: Simon H <[email protected]> --------- Co-authored-by: Simon H <[email protected]>
1 parent 8e17428 commit 9a887f8

File tree

23 files changed

+33
-103
lines changed

23 files changed

+33
-103
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,18 +430,17 @@ export function client_component(source, analysis, options) {
430430
component_block.body.unshift(b.const('$$slots', b.call('$.sanitize_slots', b.id('$$props'))));
431431
}
432432

433-
const body = [
434-
...state.hoisted,
435-
...module.body,
436-
b.function_declaration(
437-
b.id(analysis.name),
438-
[b.id('$$anchor'), b.id('$$props')],
439-
component_block
440-
)
441-
];
433+
const body = [...state.hoisted, ...module.body];
434+
435+
const component = b.function_declaration(
436+
b.id(analysis.name),
437+
[b.id('$$anchor'), b.id('$$props')],
438+
component_block
439+
);
442440

443441
if (options.hmr) {
444442
body.push(
443+
component,
445444
b.if(
446445
b.id('import.meta.hot'),
447446
b.block([
@@ -459,12 +458,14 @@ export function client_component(source, analysis, options) {
459458
)
460459
)
461460
])
462-
)
461+
),
462+
463+
b.export_default(b.id(analysis.name))
463464
);
465+
} else {
466+
body.push(b.export_default(component));
464467
}
465468

466-
body.push(b.export_default(b.id(analysis.name)));
467-
468469
if (options.dev) {
469470
if (options.filename) {
470471
let filename = options.filename;

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ export function transform_component(analysis, source, options) {
3030
? server_component(analysis, options)
3131
: client_component(source, analysis, options);
3232

33-
const basename = (options.filename ?? 'Component').split(/[/\\]/).at(-1);
34-
if (program.body.length > 0) {
35-
program.body[0].leadingComments = [
36-
{
37-
type: 'Line',
38-
value: ` ${basename} (Svelte v${VERSION})`
39-
},
40-
{
41-
type: 'Line',
42-
value: ' Note: compiler output will change before 5.0 is released!'
43-
}
44-
];
45-
}
46-
4733
const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte');
4834
const js = print(program, {
4935
// include source content; makes it easier/more robust looking up the source map code

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53
import TextInput from './Child.svelte';
64

75
var root_1 = $.template(`Something`, 1);
86
var root = $.template(`<!> `, 1);
97

10-
function Bind_component_snippet($$anchor, $$props) {
8+
export default function Bind_component_snippet($$anchor, $$props) {
119
$.push($$props, true);
1210

1311
let value = $.source('');
@@ -36,6 +34,4 @@ function Bind_component_snippet($$anchor, $$props) {
3634
$.render_effect(() => $.set_text(text, ` value: ${$.stringify($.get(value))}`));
3735
$.append($$anchor, fragment_1);
3836
$.pop();
39-
}
40-
41-
export default Bind_component_snippet;
37+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42
import TextInput from './Child.svelte';
53

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

6-
function Bind_this($$anchor, $$props) {
4+
export default function Bind_this($$anchor, $$props) {
75
$.push($$props, false);
86
$.init();
97

@@ -13,6 +11,4 @@ function Bind_this($$anchor, $$props) {
1311
$.bind_this(Foo(node, {}), ($$value) => foo = $$value, () => foo);
1412
$.append($$anchor, fragment);
1513
$.pop();
16-
}
17-
18-
export default Bind_this;
14+
}

packages/svelte/tests/snapshot/samples/bind-this/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Bind_this($$payload, $$props) {

packages/svelte/tests/snapshot/samples/class-state-field-constructor-assignment/_expected/client/index.svelte.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

6-
function Class_state_field_constructor_assignment($$anchor, $$props) {
4+
export default function Class_state_field_constructor_assignment($$anchor, $$props) {
75
$.push($$props, true);
86

97
class Foo {
@@ -26,6 +24,4 @@ function Class_state_field_constructor_assignment($$anchor, $$props) {
2624
}
2725

2826
$.pop();
29-
}
30-
31-
export default Class_state_field_constructor_assignment;
27+
}

packages/svelte/tests/snapshot/samples/class-state-field-constructor-assignment/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Class_state_field_constructor_assignment($$payload, $$props) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
// main.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

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

8-
function Main($$anchor, $$props) {
6+
export default function Main($$anchor, $$props) {
97
$.push($$props, true);
108

119
// needs to be a snapshot test because jsdom does auto-correct the attribute casing
@@ -35,6 +33,4 @@ function Main($$anchor, $$props) {
3533

3634
$.append($$anchor, fragment);
3735
$.pop();
38-
}
39-
40-
export default Main;
36+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// main.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Main($$payload, $$props) {

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

6-
function Each_string_template($$anchor, $$props) {
4+
export default function Each_string_template($$anchor, $$props) {
75
$.push($$props, false);
86
$.init();
97

@@ -19,6 +17,4 @@ function Each_string_template($$anchor, $$props) {
1917

2018
$.append($$anchor, fragment);
2119
$.pop();
22-
}
23-
24-
export default Each_string_template;
20+
}

packages/svelte/tests/snapshot/samples/each-string-template/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Each_string_template($$payload, $$props) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

6-
function Function_prop_no_getter($$anchor, $$props) {
4+
export default function Function_prop_no_getter($$anchor, $$props) {
75
$.push($$props, true);
86

97
let count = $.source(0);
@@ -30,6 +28,4 @@ function Function_prop_no_getter($$anchor, $$props) {
3028

3129
$.append($$anchor, fragment);
3230
$.pop();
33-
}
34-
35-
export default Function_prop_no_getter;
31+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Function_prop_no_getter($$payload, $$props) {
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

64
var root = $.template(`<h1>hello world</h1>`);
75

8-
function Hello_world($$anchor, $$props) {
6+
export default function Hello_world($$anchor, $$props) {
97
$.push($$props, false);
108
$.init();
119

1210
var h1 = root();
1311

1412
$.append($$anchor, h1);
1513
$.pop();
16-
}
17-
18-
export default Hello_world;
14+
}

packages/svelte/tests/snapshot/samples/hello-world/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Hello_world($$payload, $$props) {

packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

@@ -25,4 +23,4 @@ if (import.meta.hot) {
2523
});
2624
}
2725

28-
export default Hmr;
26+
export default Hmr;

packages/svelte/tests/snapshot/samples/hmr/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Hmr($$payload, $$props) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

@@ -12,7 +10,7 @@ function reset(_, str, tpl) {
1210

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

15-
function State_proxy_literal($$anchor, $$props) {
13+
export default function State_proxy_literal($$anchor, $$props) {
1614
$.push($$props, true);
1715

1816
let str = $.source('');
@@ -35,6 +33,4 @@ function State_proxy_literal($$anchor, $$props) {
3533
$.pop();
3634
}
3735

38-
export default State_proxy_literal;
39-
4036
$.delegate(["click"]);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function State_proxy_literal($$payload, $$props) {
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import "svelte/internal/disclose-version";
42
import * as $ from "svelte/internal/client";
53

6-
function Svelte_element($$anchor, $$props) {
4+
export default function Svelte_element($$anchor, $$props) {
75
$.push($$props, true);
86

97
let tag = $.prop($$props, "tag", 3, 'hr');
@@ -13,6 +11,4 @@ function Svelte_element($$anchor, $$props) {
1311
$.element(node, tag, false);
1412
$.append($$anchor, fragment);
1513
$.pop();
16-
}
17-
18-
export default Svelte_element;
14+
}

packages/svelte/tests/snapshot/samples/svelte-element/_expected/server/index.svelte.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// index.svelte (Svelte VERSION)
2-
// Note: compiler output will change before 5.0 is released!
31
import * as $ from "svelte/internal/server";
42

53
export default function Svelte_element($$payload, $$props) {

sites/svelte-5-preview/src/lib/workers/compiler/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ function compile({ id, source, options, return_ast }) {
5959
const compiled = svelte.compile(source, {
6060
filename: options.filename,
6161
generate: options.generate,
62-
dev: options.dev
62+
dev: options.dev,
63+
discloseVersion: false // less visual noise in the output tab
6364
});
6465

6566
const { js, css, warnings, metadata } = compiled;

0 commit comments

Comments
 (0)