developit / developit/microbundle

Generates Broken CommonJS From Typescript

Offen
#1,079 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
8.1k
Forks
358
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Microbundle creates broken CommonJS code when compiling from Typescript.

The `continue` keyword in the following code is not respected in the compiled CommonJS code.

```ts
async function brokenContinue() {
let j = 0;

while (j <= 3) {
j++;
console.log("AT THE TOP");

if (j === 1) {
console.log("\t- j === 1");
console.log("\t- waiting for 100ms");
await new Promise((resolve) => setTimeout(resolve, 100));
console.log('\t- continue, next log will be "AT THE TOP"');
continue;
}

console.log("AT THE BOTTOM");
}
}

void brokenContinue();
```

The output is:

```
$ node dist/index.cjs
AT THE TOP
- j === 1
- waiting for 100ms
- continue, next log will be "AT THE TOP"
AT THE BOTTOM <------- THIS SHOULD READ "AT THE TOP"
AT THE TOP
AT THE BOTTOM
AT THE TOP
AT THE BOTTOM
AT THE TOP
AT THE BOTTOM
```

The problem is caused by the usage of `async/await` within the while loop. Commenting out: `await new Promise((resolve) => setTimeout(resolve, 100));` produces the expected result.

**Notes:**
- Compiling using `tsc` generates JS that works correctly.
- Compiling to modern JS generates code that works correctly.

Here's a repo containing the setup that caused the issue: https://github.com/luxo-ai/breakjs

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.