Skip to content

lib: expose tsParse as util.stripTypescriptTypes - #54250

Closed
stagas wants to merge 1 commit into
nodejs:mainfrom
stagas:expose-tsparse
Closed

lib: expose tsParse as util.stripTypescriptTypes#54250
stagas wants to merge 1 commit into
nodejs:mainfrom
stagas:expose-tsparse

Conversation

@stagas

@stagasstagas commented Aug 7, 2024

Copy link
Copy Markdown
Contributor

This PR exposes internal/modules/helpers/tsParse as util.stripTypescriptTypes in order to transpile/strip types from TypeScript code without bringing in another dependency, since it's already there.

All details are in flux, I just picked the quickest solution I could come up with, but I wanted to bring it here for discussion.

Primary use-case would be transpiling modules to serve to the client from a pure node server without bringing in dependencies.

@nodejs-github-bot

nodejs-github-bot commented Aug 7, 2024

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/startup
  • @nodejs/typescript

@nodejs-github-botnodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Aug 7, 2024

@anonriganonrig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should land this at it's current state, or since it is based on an experimental feature.

  1. The function shouldnt live under process and exposed globally. Ideally, it should live in node:util
  2. tsParse function name is not DX friendly. parseTypescript() or stripTypescriptTypes() might be a better naming.
  3. I think we should wait until strip types experimental feature becomes stable.

@stagas

Copy link
Copy Markdown
ContributorAuthor

@anonrig I agree with 1 and 2 but I think this should be included in the experimental feature. It is simply so useful and it's already there. Even at stable we should still have a way to transpile without a dependency, so this isn't going away. We simply need to expose it at the right location. I can modify the PR according to those suggestions.

@aduh95aduh95 added semver-minor PRs that contain new features and should be released in the next minor version. experimental Issues and PRs related to experimental features. labels Aug 7, 2024
@aduh95

Copy link
Copy Markdown
Contributor

We should also document it, and expose it only when --experimental-strip-types is passed

@codecov

codecovBot commented Aug 7, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.11%. Comparing base (e0634f5) to head (19d3296).
Report is 4 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #54250 +/- ##
=======================================
Coverage 87.10% 87.11% =======================================
Files 647 647 Lines 181739 181740 +1 Branches 34887 34882 -5 =======================================
+ Hits 158310 158316 +6 
Misses 16738 16738 + Partials 6691 6686 -5 
FilesCoverage Δ
lib/internal/bootstrap/node.js99.77% <100.00%> (+<0.01%)⬆️

... and 27 files with indirect coverage changes

@avivkelleravivkeller added the strip-types Issues or PRs related to strip-types support label Aug 7, 2024
@stagasstagas changed the title lib: expose tsParselib: expose tsParse as util.stripTypescriptTypesAug 8, 2024

@legendecaslegendecas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a user should explicit depend on an amaro version if they want to use it programmatically. The --experimental-strip-types flag should only provide stability guarantee where the .ts files are transpiled.

The API is another topic of stability guarantee. An independently installed amaro dependnecy can reduce the chance the API is break at this stage.

/cc @nodejs/typescript

@marco-ippolitomarco-ippolito left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm -1 on exposing the internal of an experimental feature that its gonna change.
You can install amaro and use the transformSync function that its exactly the one that its used in core https://github.com/nodejs/amaro
Agree that tsParse is horrible and changing it would be great, PR welcome 😄

@stagas

Copy link
Copy Markdown
ContributorAuthor

@anonrig@aduh95 trying to use getOptionValue under node:util brings an error that it hasn't been bootstrapped yet and can't query for options. So it has to go after bootstrap or some other solution is needed. Need help here.

@legendecas if we are to bring a dependency to do it then that defeats the purpose of having no dependency programatic transpile in node. This is a useful feature to have in core IMO. So many projects need this and now they pick whatever dependency to transpile and it becomes an incompatible mess, it would be cool if all were using the same and tools were built on top of that.

@stagas

Copy link
Copy Markdown
ContributorAuthor

@marco-ippolito but it's part of the same feature, it's the mechanism of stripping TS types, in module importing and manually. TS is here to stay, projects are going to need to strip the types in the backend, why make everyone bring in their dependency when we already have one and we're going to have one. The details don't matter, whether it's amaro or something else in the future, the goal is we want to use TS and strip the TS types to serve to the client. This is the feature.

@marco-ippolito

marco-ippolito commented Aug 8, 2024

Copy link
Copy Markdown
Member

@marco-ippolito but it's part of the same feature, it's the mechanism of stripping TS types, in module importing and manually. TS is here to stay, projects are going to need to strip the types in the backend, why make everyone bring in their dependency when we already have one and we're going to have one. The details don't matter, whether it's amaro or something else in the future, the goal is we want to use TS and strip the TS types to serve to the client. This is the feature.

In the past we have made the mistake of exposing internals and users have started to rely on them, making it very hard to iterate. Actually if its behind the --experiemental-strip-types it would be enough.
This PR is still missing documentation. Also can rename tsParse to something more reasonable.

@benjamingrbenjamingr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs docs and more tests but otherwise lgtm (expose behind the same flag)

@khaosdoctor

Copy link
Copy Markdown
Member

I also agree on the point that we shouldn't expose internal APIs, in my view it's a good thing to have everyone using less deps and relying more on Node directly, but this should only be the case when Node actually starts supporting TS out of the box, otherwise, at least for me it gets a bit confusing.

I'm not against having it behind the flag though, it would be useful eventually, but still, to me it looks like something that's dangling, like it doesn't have an actual home.

@avivkeller

Copy link
Copy Markdown
Member

If #54287 lands, when a non-string is passed, it'll throw an assertion. I don't think this function, if exposed to users, should throw an internal assertion.

@marco-ippolito

Copy link
Copy Markdown
Member

This needs a rethink after the change of #54283

@sosoba

Copy link
Copy Markdown
Contributor

This needs a rethink after the change of #54283

util.transformTypescriptTypes ?

@jdalton

Copy link
Copy Markdown
Member

This needs a rethink after the change of #54283

util.transformTypescriptTypes ?

minor nit: TS => TypeScript not Typescript

@marco-ippolito

Copy link
Copy Markdown
Member

Superseded by #55282

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimentalIssues and PRs related to experimental features.lib / srcIssues and PRs related to general changes in the lib or src directory.needs-ciPRs that need a full CI run.semver-minorPRs that contain new features and should be released in the next minor version.strip-typesIssues or PRs related to strip-types support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

11 participants

@stagas@nodejs-github-bot@aduh95@marco-ippolito@khaosdoctor@avivkeller@sosoba@jdalton@benjamingr@anonrig@legendecas