sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.77k stars 4.23k forks source link

[Svelte 5] Regression in JS output - script context module variable declarations before import statements #13135

Closed xeho91 closed 2 months ago

xeho91 commented 2 months ago

Describe the bug

Hi awesome Svelte maintainers!

There's a regression in JS output. I happen to notice it while upgrading svelte to latest version. Below is the screenshot from when I was trying to inspect as to why the build isn't working anymore. image

I'll summarise shortly. Variable declarations inside the <script module> tag in the JS output are above the import declarations. Which is... invalid, right? Right?!

With the help of @JReinhold and his amazingly quick search skills, we've managed to narrow down when this regression happened.

So, latest working version is 5.0.0-next.240

Reading the changelogs, I couldn't figure out which one of PR is the culprit, sorry! 😦

Reproduction

A quick comparison in the JS output:

[`5.0.0-next.240`](https://github.com/sveltejs/svelte/releases/tag/svelte%405.0.0-next.240) [`5.0.0-next.243`](https://github.com/sveltejs/svelte/releases/tag/svelte%405.0.0-next.243)
```js import * as $ from "svelte/internal/client"; import { confetti } from '@neoconfetti/svelte'; var root = $.template(`
`); const result = confetti(); export default function App($$anchor, $$props) { $.push($$props, false); $.init(); var div = root(); $.append($$anchor, div); $.pop(); } ```
```js const result = confetti(); import * as $ from "svelte/internal/client"; import { confetti } from '@neoconfetti/svelte'; var root = $.template(`
`); export default function App($$anchor, $$props) { $.push($$props, false); $.init(); var div = root(); $.append($$anchor, div); $.pop(); } ```

Diff overview, because I'm nice (nah, I love Svelte).

```js
+ const result = confetti();
+ 
import * as $ from "svelte/internal/client";
import { confetti } from '@neoconfetti/svelte';

var root = $.template(`<div></div>`);
- const result = confetti();

export default function App($$anchor, $$props) {
    $.push($$props, false);
    $.init();

    var div = root();

    $.append($$anchor, div);
    $.pop();
}

Logs

No response

System Info

next

Severity

blocking an upgrade

xeho91 commented 2 months ago

My search skills needs some improvement... a possible duplicate of: https://github.com/sveltejs/svelte/issues/13081

7nik commented 2 months ago

yeah, it was fixed in #13082, but the fix wasn't good and was reverted. For now #13132 will fix.