-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add '.ts' and '.tsx' to default extensions #6027
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
Conversation
|
@Andy-MS, thanks for your PR! By analyzing the history of the files in this pull request, we identified @loganfsmyth, @existentialism and @hzoo to be potential reviewers. |
|
Is there existing tooling for TS as a Node |
|
I think ts-node does that. |
|
We have some existing issues in this repo about it I think |
|
Can presets/plugins modify the default extension set? Could this be added into a preset capability instead? I'm not sure how I feel about TS being pulled in by default any time babel is used (if I'm reading the PR correctly). |
|
TS won't be pulled in, it only tells babel to consider |
|
I'm definitely concerned with enabling these automatically for all usecases. I think for |
|
Ok then we just need to document what users need to do |
|
As mentioned in #6353 I ran into the situation where I expected that |
Can we modify the PR to just do this then? I don't know how we are differentiating that atm though, unless it is just not changing |
|
@loganfsmyth is the issue is what happens if someone happens to use both ts-node and babel-node, dono if we should handle that correctly or just error in some way? |
|
I'm good with at least letting babel-cli handle .ts automatically for now |
|
Currently I'm finding it impossible to use Typescript with babel directly - the only way is through webpack, but I'd preferably like to just use I'm busy with Node.js server code, so webpack is a bit of a round trip. Would love to see this pushed through as soon as possible. In the same boat as @alloy |
|
@lostpebble For now you need to tell |
|
Thanks @loganfsmyth ! Couldn't find anything on the babel site and tried using Would be great if we could also set this in the |
I agree with you, but unfortunately we're not at a point where it is feasible without other bigger changes to how Babel approaches compilation. |
|
I've been exploring this a bit today to see what seems workable. I think we're getting to a place where we can to have babel/core have a set of extensions that it handles, like The main place where I'd expect complaints if we did only that is babel-register (and thus babel-node), because it relies on Node's require hooks and that needs to know the extensions up front. Once #7358 lands though, I'm thinking we could make it so that if the Typescript preset is mentioned in |
xtuc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm 👍 with the idea
|
|
||
| All subsequent files required by node with the extensions `.es6`, `.es`, `.jsx`, | ||
| `.mjs`, and `.js` will be transformed by Babel. | ||
| `.mjs`, `.ts`, `.tsx`, and `.js` will be transformed by Babel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify could we add that it also includes .d.ts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to be careful with that; .d.ts files are metadata only; they're not code and cannot become code. Having code in them is a syntax error.
Even if there was some syntax you wanted to transform, having to transform it makes the .d.ts useless as that means TypeScript can't use its metadata as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR explicitly ignore them, and I just wanted to make it clear. While it doesn't really affect the user, it's a big implementation detail on our side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@loganfsmyth just made a suggestion in slack, maybe could make a new issue for it.
Logan Smyth [8:22 PM]
I've been thinking about #6027 and #3741 more generally. It seems like it'd be pretty easy to have babel-core include anextensionsobject in its options, so for example the typescript preset could just say:
extensions: {
".ts": true,
".tsx": true,
".d.ts": false,
},
in its body to tell Babel to start processing .ts files.
We could for instance make @babel/core essentially ignore a file if its extension isn't given in the config or Babel's default extension list.
My main question at that stage is, does it make sense to keep these as booleans, or to just go all the way and make the extensions be string | null with the string value being a sourceType? Then we could easily give .ts files a sourceType of its own sourceType which I've mentioned before as potentially being nice anyway, since we'll probably want that so that the CLI for instance could properly output files with .ts extensions if the user enabled the typescript syntax plugin but not the transform.
I know Webpack has more complex logic around file types, so maybe we could consider an approach that that over simple sourceTypes too, hard to say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I remember about that. Just an idea @loganfsmyth, what if we stored this configuration in the presets, like the React preset would enable the JSX syntax or TS the TS syntax.
|
Any updates on this? Would love to see this merged. At the moment, every downstream build tool (babelify, gulp-babel, babel-cli, babel-node, etc) has to be configured to look for these file extensions when using the typescript preset. Would be fantastic to have zero-config support for .ts and .tsx since there is a preset now. |
|
Just a small update. I have a project which I’ve upgraded to typescript and the test command was: Would be nice if I could use this same command with TypeScript. |
|
Any updates on this? I would love to see it merged. If there is anything that needs to be done, I would be happy to help out! |
|
I think where we've settled on this, for Babel 7.x anyway, is that this may need to remain opt-in, unfortunately. This PR as-is seems like it would have unwanted side-effects because if I have a sense of what I think Babel should consider for this issue, but it's likely something at this point that would land as an opt-in feature in Babel 7.x, and then we could explore enabling it by default in 8.x. This would involve making extensions part of user configuration and tied into the |
|
@loganfsmyth Those are good reasons; thanks for explaining in detail. I realize you noted issues with #3741 as well, but given where this discussion seems to be headed, I would appreciate fresh consideration of an |
|
Yeah, I think |
|
Meanwhile I've published a temporary |
|
Since this would be a breaking change to land in 7.x, I'm going to close it. I've filed #8652 with a link here as a place to continue this general discussion though. |
Adds
.tsand.tsxto the default list of extensions. Also avoids compiling.d.tsfiles, which are just declarations and should not transpile to anything.