Skip to content

Commit 81b32d8

Browse files
fix: hoist imports on top (#13082)
Fixes #13081
1 parent 0d51474 commit 81b32d8

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

.changeset/hip-beers-shake.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+
fix: hoist imports on top

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

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

460-
const body = [...module.body, ...state.hoisted];
460+
// we hoist all the import declarations to the top of the file
461+
const body = [...module.body, ...state.hoisted].sort((body_a, body_b) =>
462+
body_a.type === 'ImportDeclaration' && body_b.type !== 'ImportDeclaration' ? -1 : 1
463+
);
461464

462465
const component = b.function_declaration(
463466
b.id(analysis.name),

packages/svelte/tests/snapshot/samples/inline-module-vars/_expected/client/index.svelte.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import "svelte/internal/disclose-version";
2+
import * as $ from "svelte/internal/client";
23

34
const __ENHANCED_IMG_1__ = "__VITE_ASSET__2AM7_y_a__";
45
const __ENHANCED_IMG_2__ = "__VITE_ASSET__2AM7_y_b__";
56
const __ENHANCED_IMG_3__ = "__VITE_ASSET__2AM7_y_c__";
67
const __ENHANCED_IMG_4__ = "__VITE_ASSET__2AM7_y_d__";
78
const __ENHANCED_IMG_5__ = "__VITE_ASSET__2AM7_y_e__";
89
const __ENHANCED_IMG_6__ = "__VITE_ASSET__2AM7_y_f__";
9-
10-
import * as $ from "svelte/internal/client";
11-
1210
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>`);
1311

1412
export default function Inline_module_vars($$anchor) {

0 commit comments

Comments
 (0)