Skip to content

Commit 6f81a98

Browse files
committed
preserve import order and fix regression
1 parent 20d98a5 commit 6f81a98

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

.changeset/proud-lobsters-lie.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'svelte': patch
33
---
44

5-
fix: revert #13048, leave instance-scope imports above other statements
5+
fix: Ensure imports are above other statements

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,22 @@ 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+
// Merge hoisted statements into module body.
461+
// Ensure imports are on top, with the order preserved, then module body, then hoisted statements
462+
/** @type {ESTree.ImportDeclaration[]} */
463+
const imports = [];
464+
/** @type {ESTree.Program['body']} */
465+
let body = [];
466+
467+
for (const entry of [...module.body, ...state.hoisted]) {
468+
if (entry.type === 'ImportDeclaration') {
469+
imports.push(entry);
470+
} else {
471+
body.push(entry);
472+
}
473+
}
474+
475+
body = [...imports, ...body];
461476

462477
const component = b.function_declaration(
463478
b.id(analysis.name),

0 commit comments

Comments
 (0)