plugins: Typescript plugin 4.0.0 Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
- 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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 135
- Comments: 65 (15 by maintainers)
Links to this issue
Commits related to this issue
- Do maintenance - update Node version - update dependencies - fixed rollup/plugin-typescript issue (https://github.com/rollup/plugins/issues/287) - ensure styled-components uses the same React type ve... — committed to ivx/react-skeletons by deleted user 4 years ago
- Update Typescript to v4; Fix build See https://github.com/rollup/plugins/issues/287#issuecomment-626517864 See https://github.com/microsoft/TypeScript/issues/15031 — committed to quantargo/react-layout by deleted user 4 years ago
- feat: Make full-text searching feature configurable The rollup configuration added here is not really necessary. At one point we looked at generating two separate bundle files but that proved really ... — committed to birchill/hikibiki-data by birtles 4 years ago
Quick fix for now is to manually set your rootDir compiler option to “src”.
@talentlessguy I resolved this problem by removing
outDir,declarationDir,declarationfromtsconfig.json. If you need them when you invoketsc, specify them using command line arguments.Thanks for opening an issue. Citing the issue template:
Please add a reproduction and we’ll be happy to triage further.
It is very strange that the official plugin cannot be fixed for a long time. In fact, it is not doing the work it should be doing. It is virtually impossible to use rollup to build an application written in vue.
@wellrus that’s not a constructive comment and it doesn’t add anything to triaging the issue at hand. I recommend that you read https://liberamanifesto.com, and consider your view of open source.
You might also consider being a part of the solution and contributing a fix. Rollup and its plugins are community supported.
Please refrain from additional replies that are not constructive or helpful to the original issue.
Replies that do not help to triage or resolve the issue with the plugin in this repo will be removed. Suggesting alternatives does not assist with the triage of the original issue reported.
@sssylvan I’ getting this error:
I have the same error and made a small project to reproduce the error (see this project)
I run
tscseparately to generate typings. i.e. in mypackage.jsonAdding the
"rootDir": "src"node to my tsconfig solved the problem for me, just as @sssylvan suggested. That was the only change I needed.I fixed this by deleting
/distfolder before running rollup.[EDIT] Even better, seems like if I remove just
.tsbuildinfobefore running rollup this error never happen.I use ver
5.0.0. None of the given solutions worked for me except this https://github.com/rollup/plugins/issues/287#issuecomment-611368317 and this https://github.com/rollup/plugins/issues/287#issuecomment-611621543.I removed declaration from tsconfig and added manually generated it after building with rollup.
Woohoo! It looks like this bug is getting fixed already! https://github.com/microsoft/TypeScript/pull/41811
I’m using a shared folder as common source for several typescript projects (monorepo style). I’ve had the same error message:
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)After debugging the source I’ve found that the
load(id)function filters out my../shared/src/foo.tsfile from transpilation.This is because
getPluginOptionscreates a default file filter for files in the current folder (cwd) only.As a solution I configure the plugin with a suiting
includeconfiguration:In
tsconfig.jsonI also had to add the shared sources in theincludesection:I hope this description can help to others having similar problem.
I also see as a possible improvement to respect the paths in tsconfig’s
include(instead using the default['*.ts+(|x)', '**/*.ts+(|x)']).This description could be maybe added to the README.md?
I thought I had the same issue - maybe it is related.
I have a monorepo like this:
When I run the build in
/projects/A/, the build failes with the described error. Once I added../shared/**/*.tsto myincludelist, it worked.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.
Seems like rollup is not capable of handling case insensitivity of the working directory on windows, specifically the drive letter:
Same build, nothing changed except one thing: We now have a capital C: in our working dir:
Please get this fixed. We could reproduce this behaviour on 15 machines now.
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
I’ll be taking another look at this next month, been busy with work over the summer. I think the solution lies in finding a better way to determine the output paths, as
ts.getOutputFileNamesis sometimes inaccurate.I have a case when I need to generate typings, so it won’t work probably
@NIX-Pepe we’ll welcome your contribution and look forward to your pull request to fix this.
Some leads for you:
Setting rootDir": “./src”, in tsconfig And changing to dir in rollup.config.js output: { dir: “dist”, solves it and creates both output and declaration. removing outDir and setting declaration:false in tsconfig also solves it without removing the single out file from rollup.
I just want to say that the above solution did not work in my case. Updating the
includeoption for the plugin doesn’t seem to fix the issue in my repos, nor does it fix the issue in the reproduction repo from https://github.com/rollup/plugins/issues/287#issuecomment-605909100.@shellscape This is a bug and as such it deserves your attention, It’s THE most upvoted issue in the history of this repo.
PSA: “me too” replies that don’t contain diagnostic info or info to assist in triage will be removed from the issue
Works on my machine with typescript 4.2.0-beta und plugin version 8.1.1.
I’ve filed a bug over in the TypeScript repo for the
getOutputFileNamesissue: https://github.com/microsoft/TypeScript/issues/41780. It’s scheduled for 4.2.1, so probably won’t be looked at until MarchI also wrote a test in https://github.com/rollup/plugins/pull/682
None of all the solutions worked for me. What saved my life : increasing
typescript.tsserver.maxTsServerMemoryin my settings.json (work on VSCode)The root of problem is in this line in ts repo
return configFile.options.rootDir || getDirectoryPath(Debug.checkDefined(configFile.options.configFilePath));While running cli
tscseem to be working withoutrootDiroption being provided. WithoutrootDirbeing set totsconfig.jsonpath and paths to generated files generated byts. getOutputFileNameslook like that:path-to-repo/lib/src/main.jsinstead of:path-to-repo/lib/main.js.I don’t think any changes should be made to computing file output paths. Maybe there should be a validation of incoming options, if the file specified as rollup
inputlies not in the top directory andcompilerOptions.rootDiroption is not provided. Also webpack’sts-loaderuses the same api if this function exists in typescriptThis error output might be also a bit confusing and changed so it would be clear that
diroption rollup specificI was trying to fix this few days earlier and was able to create failing test, which works well with
tscby fails with plugin. By I do struggle to attach debugger with pnpm with ava to dive deeper into typescript generating output file paths. If I will not fix I may add some info (failing test, function that seem to be not working). Will check more on weekend.