sweet-core
sweet-core copied to clipboard
Hand off an AST to babel instead of string
Currently we are doing a codegen of the expanded AST before invoking babel. This is obviously sub-ideal since babel now has to do a bunch of parsing when it just needs to run transformations over an AST.
Babel has a transformFromAst function we can call but it only works over their AST, a modified variant of ESTree, and we are emitting a shift ast. The shift project includes a shift-to-estree converter which will probably get us most of the way there. May need a few changes to handle babel specific differences.
This isn't as straightforward as it appears. The the shift-to-estree converter has to be updated to match the current versions of both the shift and estree specs. Then you'd have to write an estree-to-babel converter (which could be a useful tool by itself as the specs diverge).
Another option is to convert directly to the Babel AST spec.
@disnet I updated shift-spidermonkey-converter to convert the current version of Shift to the current version of Esprima. I've also made a branch that converts to Babel instead
https://github.com/gabejohnson/shift-spidermonkey-converter-js/tree/babel
Tried it out w/ transformFromAst and it appears to work. It's available here
https://github.com/gabejohnson/sweet.js/tree/issue-515
@disnet, I've done some preliminary benchmarking (added a simple script) and there does appear to be a modest performance increase for files < 100KB. That's when the babel compact option starts to come into play.
I expect the speedup to be more noticeable as the change is integrated into load-syntax.js.