alangpierce / sucrase

Super-fast alternative to Babel for when you can target modern JS runtimes
https://sucrase.io
MIT License
5.7k stars 143 forks source link

Collect imports and exports during transform #435

Open ggoodman opened 5 years ago

ggoodman commented 5 years ago

Would it be possible to provide some mechanism to obtain the list of observed imports and exports (CJS / TS / ESM) during transformation? Even better would be the ability to hook into and transform relative import filenames.

I noticed that there doesn't seem to be an intermediate AST representation, which prevents someone like me from obtaining imports / exports from that. Of course, I bet that is a big part why it is so fast.

Since this project is light-weight (in bytesize) and is fast, I can imagine it being an ideal way to smooth the interop challenges between module formats for in-browser or edge-worker use-cases. From my perspective, I'm thinking of a tool like https://next.plnkr.co/edit/ where the dependency graph is determined and executed at run-time but currently uses the behemoth that is TypeScript. Of course I love TypeScript but its current structure makes it hard to pick and chose only those pieces you need.

Looking forward to hearing your thoughts!

alangpierce commented 5 years ago

Hey @ggoodman! I've actually thought about very similar ideas for how to make webpack faster when using Sucrase. (Webpack spends a bunch of time re-parsing the Sucrase output to gather information that Sucrase might have been able to provide without another parse.) But the webpack API is all centered around ASTs, so it would be a significant (but not impossible) change to get all that to work.

At the moment, I'd rather not add complexity to the Sucrase API unless there's a clear use case and it doesn't slow down other use cases. I'm certainly willing to make a Sucrase change, but I'd want to see a good prototype to motivate it. I can think of two approaches that might work here:

Let me know if you try going down one of these approaches, I'm interested to hear how it goes!