svelte-preprocess icon indicating copy to clipboard operation
svelte-preprocess copied to clipboard

TypeScript with importHelpers may throw errors when instance and module script present

Open dummdidumm opened this issue 3 years ago • 1 comments

Describe the bug When using TypeScript and setting the tsconfig setting importHelpers to true, TypeScript will not inline transpilation helpers and instead import them from tslib. Since the instance and module script are transpiled independently, these imports may get duplicated which results in a "X was already declared" error.

This came up in https://github.com/sveltejs/kit/issues/1577

To Reproduce tsconfig:

// ...
"importHelpers": true,
"target": "es2019"
// ...

Svelte file:

<script lang="ts" context="module">
  export async function foo() {}
</script>

<script lang="ts" context="module">
  export async function bar() {}
</script>

Expected behavior Imports are deduplicated somehow. Since this is a special case I think it's okay to have special code for it. For example doing a regex for check for tslib imports, store them somewhere and filter out duplicated imports in the next transformation.

Information about your project:

  • Your browser and the version: irrelevant

  • Your operating system: irrelevant

  • svelte-preprocess version 4.7.3

  • Whether your project uses Webpack or Rollup: Rollup/Vite

dummdidumm avatar Jun 14 '21 08:06 dummdidumm

IMO, there should not be awaiter for es2019 target. In fact, svelte-preprocess hardcodes ts target to es6, but even in this case ts should output plain async/await without tslib generator hacks

cbxp avatar Jul 09 '21 09:07 cbxp