-
Notifications
You must be signed in to change notification settings - Fork 4
Proposal: Ecosystem: Babel plugin? #76
Comments
tl;dr, no, not really. The CoffeeScript compiler doesn't produce a clean, babel-compatible AST. You could dig into the CS internals, parse out the AST, write a translator that makes it babel compatible, and wrap that in a babel plugin. That sounds pretty hard to me, and I wouldn't be surprised if there were basically unresolvable ambiguities. Probably the easiest way would be to parse CS entirely into JS, re-parse it into a babel AST, and use that in the babel plugin. It'd be slow, and the sourcemaps would be worthless, but it'd save a build step. |
Wow, that sounds already painful. |
That's somewhat of a bummer. One of the reasons I'm hesitant to use CS2 is because of the need for an additional build step on unsupported ES6 environments. |
fwiw, I built LightScript from the ground up around Babel; you might give it a try. I'd be eager for feedback over on gitter |
Yes, I was imagining the CS → JS → reparsing as being simple and possible. It's exactly what we're currently doing in CS2 with |
Worth keeping in mind that That said, here's roughly how I'd do it: import { compile } from 'coffeescript'
import { parse } from 'babylon'
export default function (babel) {
return {
manipulateOptions(opts, parserOpts, file) {
// don't process .js files
// (this would need to be refined)
if (file.opts.filename.endsWith('.js')) return;
opts.parserOpts.parser = (input, options) => {
const jsFromCoffee = compile(input);
return parse(jsFromCoffee, options);
};
}
}
} I haven't tried it, treat that as pseudocode, but happy to help if anyone decides to give it a shot |
This strikes me as a great idea. I don’t think the current compiler should be converted into a Babel plugin, but a Babel plugin that imports it like @rattrayalex’s example would surely make things more convenient for many people. On a current project I use Browserify to merge together |
See #92 for more discussion of these issues. |
Migrated to jashkenas/coffeescript#4969 |
I'm no expert on Babel, but I saw this comment by @ArmorDarks:
Is it feasible / interesting to make CoffeeScript 2 into a Babel syntax plugin?
The text was updated successfully, but these errors were encountered: