babel/generator-babel-boilerplate

ES7 Async Functions

Closed this issue · 4 comments

Attempting to use ES7 async/away functionality, throws a SyntaxError: Unexpected token when running the build gulp task (full error shown below).

I have tried modifying the babelify configuration with the following:

bundler.transform(babelify.configure({
    sourceMapRelative: __dirname + '/src',
    optional: ["es7.asyncFunctions"],
    stage:2
  }));

And I also tried adding the following lines in the file I am attempting to use async/await in:

require('babel-core/register')({
  optional: ['es7.asyncFunctions']
});

It seems that it might be a problem with esperanto/acorn when trying to build. After looking into acorn, it seems like it might not have support for this yet. Any plans for including ES7 functionality or maybe a work around?

Async Usage Code

Line 36 (a function in my library named signup that uses axios.post(), which returns a promise):

async signup(signupData) {
    try {
        return await axios.post(`${serverUrl}/signup`, signupData);
    } catch (err) {
        throw new Error(err);
    }
}

Full Error (project folder replaced with ~):

SyntaxError: Unexpected token (36:7)
    at Parser.pp.raise (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:1745:13)
    at Parser.pp.unexpected (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2264:8)
    at Parser.pp.expect (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2258:26)
    at Parser.pp.parseMethod (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:1376:8)
    at Parser.pp.parseClassMethod (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2827:23)
    at Parser.pp.parseClass (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2820:10)
    at Parser.pp.parseStatement (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2423:19)
    at Parser.pp.parseTopLevel (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:2379:21)
    at Object.parse (~/node_modules/esperanto/node_modules/acorn/dist/acorn.js:101:12)
    at getModule (~/node_modules/esperanto/dist/esperanto.js:1653:30)

Did you turn on the async-await experimental flag?

o nvm sorry. Gotta learn2read the issue :) I'll look into this soon!

@prescottprue, as you suggested this is a problem with Esperanto depending on Acorn. The plan to get rid of Esperanto won't solve this problem, though, because its replacement, Rollup, also uses Acorn.

There's talk of supporting this feature in Acorn, but it hasn't landed yet.

I once tried to use Webpack for this boilerplate, and I forget why I went with Esperanto instead. Maybe it's time to revisit using a different system entirely to try to land this feature.

If you want to try your hand at alternatives I'd be happy to consider them with ya. For now, what I'm going to do is:

  1. close this issue as wontfix, because it depends on upsteam dependencies
  2. create a new issue about looking into solutions that don't depend on Acorn / support all of Babel's functionality (maybe webpack?)
  3. add a note in the README about Acorn limitations

How's that sound?

fwiw, I'm thinking Webpack might be the solution here. I'm investigating supporting this in v6.0.0

In my preliminary webpack tests, webpack supports this but JSCS does not : P

Once this lands, I'll update the README to let people know about the tradeoff.

nvm – got it to work!