square/es6-module-transpiler — pull request #112 by thomasboyt (2014-03-19 03:05:55+00:00)
Title: Rewrite using Recast

*This is a PR I'm keeping open to track rewrite progress, do not merge!*

Currently, the transpiler architecture looks like this:

* Parse the source string to an AST with Esprima
* Walk the source, keeping track of the ranges of import/export/module declarations
* Mutate the source string by range replacement

The downside of this architectures:

* No source map support
* Range replacement is brittle and annoying to write, even with ES6 template strings

The solution: [Recast](https://github.com/benjamn/recast), a fantastic library by @benjamn used in the [Regenerator](https://github.com/facebook/regenerator) compiler. Recast allows you to parse your source to an AST, mutate the tree *as you walk it* to replace nodes, and then print out a nicely-formatted result along with source maps.

What I thought would be relatively minor changes to support cycles has turned into this rewrite, as I've found it much more pleasant to use than source replacement. It's easy to maintain, tweak, and most importantly, has source maps out of the box.

With this rewrite, I'm totally dumping the old parse/compiler classes, as well as the CLI (which was a bit unwieldy). I've changed up the CLI API a bit, but I'd like to bring it back to at least feature parity with the original CLI (notably, it's missing stdin/out support right now).

**I'd love to get feedback as I continue work on this!** This is going to become fully "usable" hopefully within the next week or two.

Current status (check means "implemented," but not necessarily "done"):

- [x] CJS rewriter
- [ ] AMD rewriter
- [ ] YUI rewriter
- [x] CLI
- [x] Syntax test ES6 fixtures
- [ ] Syntax test CJS/AMD/YUI expected fixtures
- [ ] Other feature test coverage (registry, cycles - would like to actually automatically test the stuff in the `cycles` folder)
- [ ] New test harness for changed API