Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Typescript in .ts files throws error #244

Open
zookini opened this issue May 11, 2021 · 5 comments · May be fixed by #285
Open

Typescript in .ts files throws error #244

zookini opened this issue May 11, 2021 · 5 comments · May be fixed by #285

Comments

@zookini
Copy link

zookini commented May 11, 2021

When I try to build with a .ts file containing typescript I get an error e.g.:

src/main.ts → public/build/bundle.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src\main.ts (10:5)
 8: });
 9:
10: let x: string = "oops";
         ^
11:
12: export default app;
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:5309:30)
    at Module.error (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:9765:16)
    at Module.tryParse (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:10169:25)
    at Module.setSource (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:10068:24)
    at ModuleLoader.addModuleSource (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:18451:20)
    at ModuleLoader.fetchModule (C:\Users\kl730355\Code\threat-intel\node_modules\rollup\dist\shared\rollup.js:18507:9)
    at async Promise.all (index 0)

This does not seem to happen when I remove resolve from plugins, but that obviously introduces an unresolved dependencies issue

Reproduction

  1. Run node scripts/setupTypeScript.js as suggested in docs
  2. Add any typescript to src/main.ts
  3. npm run build

Environment

  • Windows 10
  • Node 14.16.1
@lukasdiegelmann
Copy link

Do you have any solution for this error @zookini or maybe a workaround? If so please share. I need this error to be gone to properly make progress.

@VeselovAlex
Copy link

Try to add extensions: ['.ts', '.mjs', '.js', '.json', '.node'] to resolve options in rollup.config.js

resolve({
  browser: true,
  dedupe: ['svelte'],
  extensions: ['.ts', '.mjs', '.js', '.json', '.node']
}),

@KiKiKi-KiKi
Copy link

I was in the same error, when import .ts in .svelte file.

  • "svelte": "^3.0.0"
  • "rollup": "^2.3.4"
  • "typescript": "^4.0.0"
// /src/Component.svelte
<script lang="ts">
  import { myScript } from './myScript';
</script>

=> [!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

I resolve the error by add rootDir to rollup.config's typescript plugin.

rollup.config.js

export default {
  // …
  plugins: [
    // … 
    typescript({
      sourceMap: !production,
      inlineSources: !production,
+     rootDir: "./src",
    }),

But I can't decide its good way or not. Thank you 🙏

@Azarattum
Copy link

Can confirm the issue. This really is needed to be fixed!

@austinwitherspoon
Copy link

austinwitherspoon commented May 20, 2022

Having the same exact issue. Unfortunately adding rootDir: "./src" doesn't fix it for me.

Removing the resolve plugin as suggested at the top does seem to fix the issue, but I'm too new to javascript to understand the implications of doing that.
Edit: Nope, removing the resolve plugin makes this error go away but breaks svelte.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants