Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript plugin 4.0.0 Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) #287

Closed
hasangenc0 opened this issue Mar 28, 2020 · 65 comments · Fixed by #818

Comments

@hasangenc0
Copy link
Contributor

  • Rollup Plugin Name: typescript
  • Rollup Plugin Version: 4.0.0
  • Rollup Version: 2.2.0
  • Operating System (or Browser): macOsx
  • Node Version: 10.16.3

How Do We Reproduce?

import tsPlugin from '@rollup/plugin-typescript';
import pkg from './package.json';

export default {
	input: 'src/index.tsx',
	output: [
		{
			file: pkg.main,
			format: 'cjs',
			sourcemap: true,
		},
		{
			file: pkg.module,
			format: 'es',
			sourcemap: true,
		},
	],
	plugins: [
		tsPlugin(),
	],
};

Expected Behavior

src/index.tsx → dist/index.js, dist/index.es.js...
created dist/index.js, dist/index.es.js in 1.1s

Actual Behavior

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/index.tsx (1:25)
1: export function hasan(num: number): number {
                            ^

I hope I'm doing something wrong because it is so bad to cant use ts plugin.
Ts plugin version 3.0.0 works perfect by the way.

@shellscape
Copy link
Collaborator

Thanks for opening an issue. Citing the issue template:

Issues without minimal reproductions will be closed! Please provide one by:

  1. Using the REPL.it plugin reproduction template at https://repl.it/@rollup/rollup-plugin-repro
  2. Provide a minimal repository link (Read https://git.io/fNzHA for instructions).
    These may take more time to triage than the other options.
  3. Using the REPL at https://rollupjs.org/repl/

Please add a reproduction and we'll be happy to triage further.

@JohanSchobben
Copy link

I have the same error and made a small project to reproduce the error (see this project)

@JohanSchobben
Copy link

I switched back to @rollup/plugin-typescript version 3.1.1. In this version I don't have the problems. I use tsc to make the declarations.

@shellscape
Copy link
Collaborator

Thanks for the reproduction @JohanSchobben

@c1xf
Copy link

c1xf commented Mar 30, 2020

Quick fix for now is to manually set your rootDir compiler option to "src".

@talentlessguy
Copy link

@sssylvan I' getting this error:

Error: @rollup/plugin-typescript: 'dir' must be used when 'outDir' is specified.

@huww98
Copy link

huww98 commented Apr 9, 2020

@talentlessguy I resolved this problem by removing outDir, declarationDir, declaration from tsconfig.json. If you need them when you invoke tsc, specify them using command line arguments.

@talentlessguy
Copy link

@talentlessguy I resolved this problem by removing outDir, declarationDir, declaration from tsconfig.json. If you need them when you invoke tsc, specify them using command line arguments.

I have a case when I need to generate typings, so it won't work probably

@huww98
Copy link

huww98 commented Apr 9, 2020

I have a case when I need to generate typings, so it won't work probably

I run tsc separately to generate typings. i.e. in my package.json

{
   ...
   "scripts": {
        "build": "tsc --outDir dist/lib --declarationDir dist/types --declaration true && rollup -c",
        "prepare": "npm run build"
    }
}

@photonstorm

This comment has been minimized.

@VitorLuizC
Copy link
Contributor

VitorLuizC commented Apr 24, 2020

I fixed this by deleting /dist folder before running rollup.

[EDIT] Even better, seems like if I remove just .tsbuildinfo before running rollup this error never happen.

@htho
Copy link

htho commented May 11, 2020

I thought I had the same issue - maybe it is related.

I have a monorepo like this:

/projects/A/src/A.ts
/projects/A/rollup.config.js

/projects/B/src/B.ts

/projects/shared/src/somemod.ts
// somemod.ts
export const theAnswer: number = 42;
// A.ts
import {theAnswer} from "../../shared/src/somemod";
console.log(theAnswer);

When I run the build in /projects/A/, the build failes with the described error. Once I added ../shared/**/*.ts to my include list, it worked.

@Jack-Works
Copy link

Have the same problem, repo at https://github.com/Jack-Works/async-call/tree/rollup (rollup branch)

@askibinski

This comment has been minimized.

@Jack-Works
Copy link

Jack-Works commented Jun 6, 2020

I switched back to @rollup/plugin-typescript version 3.1.1.

Downgrade to 3.1 doesn't work for me

Quick fix for now is to manually set your rootDir compiler option to "src".

Doesn't work for me either

@Jack-Works
Copy link

Oh strange, rollup-plugin-typescript2 doesn't work either

@Jack-Works
Copy link

Oh sorry, my fault. I put my ts plugin in the output config, therefore, it's not working

@martinwebrant
Copy link

We had a problem where --preserveSymlinks solved it... hard to understand why though.

@wagedomain
Copy link

Adding the "rootDir": "src" node to my tsconfig solved the problem for me, just as @sssylvan suggested. That was the only change I needed.

@eMontielG
Copy link

I had to downgrade to version 3.1 as well, nothing else worked for me.

mmainz pushed a commit to ivx/react-skeletons that referenced this issue Jun 22, 2020
- update Node version
- update dependencies
- fixed rollup/plugin-typescript issue (rollup/plugins#287)
- ensure styled-components uses the same React type version as we do
@vazra
Copy link

vazra commented Jun 27, 2020

I use ver 5.0.0. None of the given solutions worked for me except this #287 (comment) and this #287 (comment).

I removed declaration from tsconfig and added manually generated it after building with rollup.

    "build": "rollup -c && tsc -d --emitDeclarationOnly --declarationDir build/types",

@Ethergeist
Copy link

Adding "rootDir": "src" and
"include": [ "**/*.ts" ],

To tsconfig.json fixed this issue for me.

@benmccann
Copy link
Contributor

@ivankoleda when I run tsc manually I don't see rootDirOfOptions getting called in my project. More importantly perhaps, I also don't see it calling getOutputFileNames, which seems to be the misbehaving function as @NotWoods mentioned. I do see tsc calling getSourceFilePathInNewDirWorker (via getSourceFilePathInNewDir) where newDirPath is the project root, currentDirectory is the project root, and commonSourceDirectory is src

@benmccann
Copy link
Contributor

I tried changing:

const emittedFileNames = ts.getOutputFileNames(parsedOptions, id, !ts.sys.useCaseSensitiveFileNames);
const codeFile = emittedFileNames.find(isCodeOutputFile);

To:

const codeFile = ts.getSourceFilePathInNewDir(id, formatHost, parsedOptions.outDir);

But got:

host.getCommonSourceDirectory is not a function

Perhaps you know if there are different types of hosts @NotWoods or how to get the appropriate host? I'm not sure I have a lot more time to spend on this at the moment, but hopefully this info helps move the investigation along a bit.

@NotWoods
Copy link
Member

NotWoods commented Dec 1, 2020

That seems like the right direction, thanks for the info! I'll investigate the host a bit more.

@NotWoods
Copy link
Member

NotWoods commented Dec 1, 2020

Ah, sadly that's a private TypeScript API so we can't use it in the plugin.

@benmccann
Copy link
Contributor

I've filed a bug over in the TypeScript repo for the getOutputFileNames issue: microsoft/TypeScript#41780. It's scheduled for 4.2.1, so probably won't be looked at until March

I also wrote a test in #682

@benmccann
Copy link
Contributor

Woohoo! It looks like this bug is getting fixed already! microsoft/TypeScript#41811

@benmccann
Copy link
Contributor

The fix has been merged in TypeScript and should be released with TypeScript 4.2.0. We can leave this open until then to track and make sure that we can enable the failing test when 4.2.0 is released

@shellscape
Copy link
Collaborator

@benmccann it looks like we can close this one out. thoughts?

@jesse-welnick-triad
Copy link

@shellscape I am still running into this issue, and I tried 4.2.0

@benmccann
Copy link
Contributor

I just confirmed that the test passes with typescript 4.2.0-beta (#789). I'm not quite sure if you need to upgrade the typescript version used in your project and/or the plugin

@jsbeckr
Copy link

jsbeckr commented Feb 3, 2021

Works on my machine with typescript 4.2.0-beta und plugin version 8.1.1.

@shellscape
Copy link
Collaborator

We've got a pending partial fix cued up in #794 as well. We'll do a preflight check to see if the tsconfig option was omitted, declaration is true, and none of the rootDir options were provided. It's a specific case when using declarations: true but it does cover some of the issues around the central issue.

@rollup rollup deleted a comment from aradalvand Feb 20, 2021
@shellscape
Copy link
Collaborator

PSA: "me too" replies that don't contain diagnostic info or info to assist in triage will be removed from the issue

@NIX-Pepe
Copy link

NIX-Pepe commented Feb 24, 2021

Seems like rollup is not capable of handling case insensitivity of the working directory on windows, specifically the drive letter:

c:\tfs\...\...Lib>npm run build
> tsc --version && npm --version && node --version && rollup --version && eslint src/** --ext .ts && rimraf build && rollup -c
Version 3.9.7
6.14.4
v12.18.0
rollup v2.33.1

src/index.ts → build/bundle.js...

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src\components\ComponentBase.ts (4:7)
2: import { STATE_MACHINE as stateMachine } from '../StateMachine';
3:
4: export abstract class ComponentBase extends HTMLElement {
          ^
5:   private _isRendered: boolean;
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:5252:30)
    at Module.error (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:9811:16)
    at tryParse (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:9692:23)
    at Module.setSource (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:10118:19)
    at ModuleLoader.addModuleSource (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:18279:20)
    at ModuleLoader.fetchModule (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:18335:9)
    at async Promise.all (index 5)
    at ModuleLoader.fetchStaticDependencies (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:18360:34)
    at async Promise.all (index 0)
    at ModuleLoader.fetchModule (c:\tfs\...Lib\node_modules\rollup\dist\shared\rollup.js:18337:9)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `tsc --version && npm --version && node --version && rollup --version && eslint src/** --ext .ts && rimraf build && rollup -c`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Same build, nothing changed except one thing: We now have a capital C: in our working dir:

c:\tfs\...Lib>cd C:\tfs\...Lib

c:\tfs\...Lib>U:

U:\>C:

C:\tfs\...Lib>npm run build

> tsc --version && npm --version && node --version && rollup --version && eslint src/** --ext .ts && rimraf build && rollup -c

Version 3.9.7
6.14.4
v12.18.0
rollup v2.33.1

src/index.ts → build/bundle.js...
created build/bundle.js in 3.3s

Please get this fixed. We could reproduce this behaviour on 15 machines now.

@shellscape
Copy link
Collaborator

@NIX-Pepe we'll welcome your contribution and look forward to your pull request to fix this.

@aradalvand
Copy link

aradalvand commented Feb 24, 2021

@shellscape This is a bug and as such it deserves your attention, It's THE most upvoted issue in the history of this repo.

@shellscape
Copy link
Collaborator

@AradAral you all really should read this https://liberamanifesto.com/

The maintainers here give their time freely to the community. Your demands for action are unwarranted, entitled, and just plain rude. If you'd like to see an issue resolved at a faster pace than maintainers are able to accomplish, your effort and time are required to make that happen. Thus is the nature of open source.

We're locking this issue to maintainers moving forward, and anyone viewing this issue unable to comment can thank @AradAral for that. If anyone would like to help the project and their fellow users out by submitting a fix for this issue, we'll gladly welcome it!

@rollup rollup locked and limited conversation to collaborators Feb 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.