Skip to content

Commit 5facd5b

Browse files
chore: put module variables above template for later inlining (#13048)
* chore: put module variables above template for later inlining * regenerate types * update comment * update comment * Update packages/svelte/src/compiler/types/template.d.ts Co-authored-by: Rich Harris <[email protected]> * Update packages/svelte/src/compiler/types/template.d.ts Co-authored-by: Rich Harris <[email protected]> * update types --------- Co-authored-by: Rich Harris <[email protected]>
1 parent a1d1012 commit 5facd5b

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export function client_component(analysis, options) {
457457
analysis.uses_slots ||
458458
analysis.slot_names.size > 0;
459459

460-
const body = [...state.hoisted, ...module.body];
460+
const body = [...module.body, ...state.hoisted];
461461

462462
const component = b.function_declaration(
463463
b.id(analysis.name),

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ export type Block = EachBlock | IfBlock | AwaitBlock | KeyBlock | SnippetBlock;
457457
export interface Attribute extends BaseNode {
458458
type: 'Attribute';
459459
name: string;
460+
/**
461+
* Quoted/string values are represented by an array, even if they contain a single expression like `"{x}"`
462+
*/
460463
value: true | ExpressionTag | Array<Text | ExpressionTag>;
461464
metadata: {
462465
expression: ExpressionMetadata;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import "svelte/internal/disclose-version";
2+
3+
const __ENHANCED_IMG_1__ = "__VITE_ASSET__2AM7_y_a__";
4+
const __ENHANCED_IMG_2__ = "__VITE_ASSET__2AM7_y_b__";
5+
const __ENHANCED_IMG_3__ = "__VITE_ASSET__2AM7_y_c__";
6+
const __ENHANCED_IMG_4__ = "__VITE_ASSET__2AM7_y_d__";
7+
const __ENHANCED_IMG_5__ = "__VITE_ASSET__2AM7_y_e__";
8+
const __ENHANCED_IMG_6__ = "__VITE_ASSET__2AM7_y_f__";
9+
10+
import * as $ from "svelte/internal/client";
11+
12+
var root = $.template(`<picture><source type="image/avif"> <source type="image/webp"> <source type="image/png"> <img alt="production test" width="1440" height="1440"></picture>`);
13+
14+
export default function Inline_module_vars($$anchor) {
15+
var picture = root();
16+
var source = $.child(picture);
17+
18+
$.set_attribute(source, "srcset", __ENHANCED_IMG_1__ + " 1440w, " + __ENHANCED_IMG_2__ + " 960w");
19+
20+
var source_1 = $.sibling(source, 2);
21+
22+
$.set_attribute(source_1, "srcset", __ENHANCED_IMG_3__ + " 1440w, " + __ENHANCED_IMG_4__ + " 960w");
23+
24+
var source_2 = $.sibling(source_1, 2);
25+
26+
$.set_attribute(source_2, "srcset", __ENHANCED_IMG_5__ + " 1440w, " + __ENHANCED_IMG_6__ + " 960w");
27+
28+
var img = $.sibling(source_2, 2);
29+
30+
$.set_attribute(img, "src", __ENHANCED_IMG_5__);
31+
$.reset(picture);
32+
$.append($$anchor, picture);
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as $ from "svelte/internal/server";
2+
3+
const __ENHANCED_IMG_1__ = "__VITE_ASSET__2AM7_y_a__";
4+
const __ENHANCED_IMG_2__ = "__VITE_ASSET__2AM7_y_b__";
5+
const __ENHANCED_IMG_3__ = "__VITE_ASSET__2AM7_y_c__";
6+
const __ENHANCED_IMG_4__ = "__VITE_ASSET__2AM7_y_d__";
7+
const __ENHANCED_IMG_5__ = "__VITE_ASSET__2AM7_y_e__";
8+
const __ENHANCED_IMG_6__ = "__VITE_ASSET__2AM7_y_f__";
9+
10+
export default function Inline_module_vars($$payload) {
11+
$$payload.out += `<picture><source${$.attr("srcset", __ENHANCED_IMG_1__ + " 1440w, " + __ENHANCED_IMG_2__ + " 960w")} type="image/avif"> <source${$.attr("srcset", __ENHANCED_IMG_3__ + " 1440w, " + __ENHANCED_IMG_4__ + " 960w")} type="image/webp"> <source${$.attr("srcset", __ENHANCED_IMG_5__ + " 1440w, " + __ENHANCED_IMG_6__ + " 960w")} type="image/png"> <img${$.attr("src", __ENHANCED_IMG_5__)} alt="production test" width="1440" height="1440"></picture>`;
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<svelte:options runes={true} />
2+
3+
<script module>
4+
const __ENHANCED_IMG_1__ = "__VITE_ASSET__2AM7_y_a__";
5+
const __ENHANCED_IMG_2__ = "__VITE_ASSET__2AM7_y_b__";
6+
const __ENHANCED_IMG_3__ = "__VITE_ASSET__2AM7_y_c__";
7+
const __ENHANCED_IMG_4__ = "__VITE_ASSET__2AM7_y_d__";
8+
const __ENHANCED_IMG_5__ = "__VITE_ASSET__2AM7_y_e__";
9+
const __ENHANCED_IMG_6__ = "__VITE_ASSET__2AM7_y_f__";
10+
</script>
11+
12+
<picture>
13+
<source srcset={__ENHANCED_IMG_1__ + " 1440w, " + __ENHANCED_IMG_2__ + " 960w"} type="image/avif" />
14+
<source srcset={__ENHANCED_IMG_3__ + " 1440w, " + __ENHANCED_IMG_4__ + " 960w"} type="image/webp" />
15+
<source srcset={__ENHANCED_IMG_5__ + " 1440w, " + __ENHANCED_IMG_6__ + " 960w"} type="image/png" />
16+
<img src={__ENHANCED_IMG_5__} alt="production test" width=1440 height=1440 />
17+
</picture>

packages/svelte/types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,9 @@ declare module 'svelte/compiler' {
19181918
interface Attribute extends BaseNode {
19191919
type: 'Attribute';
19201920
name: string;
1921+
/**
1922+
* Quoted/string values are represented by an array, even if they contain a single expression like `"{x}"`
1923+
*/
19211924
value: true | ExpressionTag | Array<Text | ExpressionTag>;
19221925
metadata: {
19231926
expression: ExpressionMetadata;

0 commit comments

Comments
 (0)