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

babel-node should add '.ts', '.tsx' as a default extensions to hook into #11577

Closed
stnor opened this issue May 18, 2020 · 5 comments
Closed

babel-node should add '.ts', '.tsx' as a default extensions to hook into #11577

stnor opened this issue May 18, 2020 · 5 comments

Comments

@stnor
Copy link

stnor commented May 18, 2020

Bug Report

Current Behavior

I'm running jasmine tests in babel-node in my front-end typescript project, and I got

SyntaxError: Cannot use import statement outside a module

It took me four hours to understand that *.ts files aren't included by default in babel-node. Google/SE was no help.

As Typescript is supported by babel since a while back, it seems resonable that babel-node should hook into '.ts'-files by default? Perhaps '.tsx' too.

See https://babeljs.io/docs/en/next/babel-node.html

Input Code
DOESN'T WORK
package.json:

  "scripts": {
    "test": "npx babel-node spec/run.ts",
  },

DOES WORK
package.json:

  "scripts": {
    "test": "npx babel-node -x '.ts' spec/run.ts",
  },
@babel-bot
Copy link
Collaborator

Hey @stnor! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@stnor stnor changed the title babel-node should add '.ts' as a default extensions to hook into babel-node should add '.ts', '.tsx' as a default extensions to hook into May 18, 2020
@dilyanpalauzov
Copy link

dilyanpalauzov commented May 20, 2020

Do you mean what is described at microsoft/TypeScript#38546?

In particular that import ./b in typescript means all of import ./b.ts, import ./b.js or import the directory ./b/ as package, whichever exists, while in ES6 syntax, one has to be explicit import ./b.js, import ./b.ts or import ./b. Sadly, I had no luck in communicating the problem to the TypeScript team and the documentation, how alternative typescript parsers should behave, is vague.

Moreover, typescript does accept as input syntax import ./b.js when it means b.ts, but preset-typescript on that input says b.js does not exist (because b.ts exists). preset-babel would accept as input import ./b.ts, but this is not accepted by typescript. On input import ./b typescript with target es2020 generates output import ./b, but this is not valid ES6 when the author wants to import the generated ./b.ts -> ./b.js. So there is no input that works accross all combinations.

The problem is, that one cannot use the same input for preset-typescript and typescript, when the target of the latter is es2020, and to get the result of both parsers working at the same time.

@dilyanpalauzov
Copy link

I edited the comment above several times.

@nicolo-ribaudo
Copy link
Member

Related - #8652

@stnor
Copy link
Author

stnor commented May 21, 2020

@dilyanpalauzov no. It's what @nicolo-ribaudo ref'ed..

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

No branches or pull requests

4 participants