developit / developit/microbundle

Generates Broken CommonJS From Typescript

未關閉
#1,079 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
JavaScript
星號
8.1k
分支
358
PR 合併指標
30 天內沒有已合併 PR

描述

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

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。