sveltejs / svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
MIT License
1.76k stars 150 forks source link

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

Open dummdidumm opened 3 years ago

dummdidumm commented 3 years ago

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:

cbxp commented 3 years ago

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