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

API: Allow passing TypeScript.SourceFile / AST directly to transpileModule #28365

Open
4 tasks done
ryanelian opened this issue Nov 6, 2018 · 4 comments
Open
4 tasks done
Labels
API Relates to the public API for TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@ryanelian
Copy link

ryanelian commented Nov 6, 2018

Search Terms

transpileModule directly AST SourceFile source performance

Suggestion

Currently, TypeScript.transpileModule accepts string and option as parameters. It would be nice to have another method like this which can accept TypeScript.SourceFile directly.

Use Cases

Currently I'm building a parser which accepts JS for scanning whether ECMAScript syntax above user's project language level / tsconfig.json:target is used. (For example: Your project targets ES2015 but one of the library imported uses ES2017 async-await syntax)

When benchmarked, simply scanning the AST is faster (medium-sized project: 2s. no scan: 1 second) rather than straight-up transpiling every single library in node_modules (same project: 8s). Then, I only need to transpile JS files above project target level for optimal build performance!

It would be nice if I can pass the AST which I obtained from above process straight to transpileModule to prevent double parsing. (Especially since transpileModule uses some internal options such as suppressOutputPathCheck and allowNonTsExtensions)

Examples

Proposed API:

transpileModuleAst(source: TypeScript.SourceFile, transpileOptions: TypeScript.transpileOptions): TypeScript.TranspileOutput

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@weswigham weswigham added Suggestion An idea for TypeScript In Discussion Not yet reached consensus API Relates to the public API for TypeScript labels Nov 6, 2018
@dsherret
Copy link
Contributor

dsherret commented Apr 4, 2019

Thanks for opening this! I'm also wanting this feature.

Instead of creating a new function, what about changing the current function signature from...

function transpileModule(input: string, transpileOptions: TranspileOptions): TranspileOutput;

...to...

function transpileModule(input: string | SourceFile, transpileOptions: TranspileOptions): TranspileOutput;

Some questions with doing this:

  • Should the transpileOptions.compilerOptions.target take precedence over the passed in source file's target? What should happen when they don't match?
  • What should happen when transpileOptions.fileName is specified and doesn't match the source file's file name?

Perhaps the answer to both of these is that the sourceFile should have a shallow clone done when entering the function? Or perhaps the originally temporarily changed via try/finally to match what is specified in the transpileOptions?

@weswigham would you accept a PR for this? I'm eager to get this in.

@weswigham
Copy link
Member

I think we probably need to have a real discussion on our API layers, and what layers we wanna expose. Internally we've got stuff like a dedicated AST pretty-printer and a dedicated AST transformer - exposing them separately and then allowing the community to tie them together themselves might make more sense.

@dsherret
Copy link
Contributor

dsherret commented Apr 5, 2019

Sounds good! Isn't the pretty printer already exposed? (and maybe this is the transformer too?) I'm not too familiar with the internals so I'm not sure.

I agree though that a better way to take an AST and get one transformed to the target would be nice and from there people can decide to print it or not.

Also, seems slightly inefficient how ts.transpileModule creates a program every time considering how it's normally used (ex. projects like ts-node will call it for every file when using the --transpile-only flag). I'm not sure what the performance impact actually is though (I haven't measured it), but perhaps those projects should just be using the program and call emit with { noResolve: true, isolatedModules: true }... or does having all the source files in the project cause a full type check to happen regardless? I will try it sometime to see...

@timreichen
Copy link

timreichen commented Apr 24, 2021

Any news on this? This is still not solved.
In addition to this: Since the comment in the source code reads that ts.transpile is a shortcut for ts.transpileModule, why not get rid of ts.transpile and renaming ts.transpileModule to ts.transpile which takes a SourceFile? That would be clean and reflect how ts.transform works.

const sourceFile = ts.createSourceFile()
const transformedSourceFile = ts.transform(sourceFile, )
const code = ts.transpile(transformedSourceFile, )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants