auth0 / webtask-bundle

Module and CLI to bundle your code for use on https://webtask.io
28 stars 6 forks source link

add support for async/await #23

Closed mitel closed 6 years ago

mitel commented 7 years ago

Hello, it would be nice to have support for async/await in Webtasks. If we use babel/transform-async-to-generator, async functions will be compiled to generator functions, which are supported natively by node 4.4.5. the only drawback would be that Uglify.js will not be able to minify code with generator functions.

presets: [
    // includes transform-async-to-generator that will transform async to 
    // generator functions supported natively on node v4
    require.resolve('babel-preset-es2017'),
    require.resolve('babel-preset-es2016')
],
// babel-preset-es2015 without transform-regenerator as
// we're going to use the native suport of node for generator functions
plugins: [
    require.resolve('babel-plugin-check-es2015-constants'),
    require.resolve('babel-plugin-transform-es2015-arrow-functions'),
    require.resolve('babel-plugin-transform-es2015-block-scoping'), 
    require.resolve('babel-plugin-transform-es2015-block-scoped-functions'), 
    require.resolve('babel-plugin-transform-es2015-classes'), 
    require.resolve('babel-plugin-transform-es2015-computed-properties'), 
    require.resolve('babel-plugin-transform-es2015-destructuring'), 
    require.resolve('babel-plugin-transform-es2015-duplicate-keys'),  
    require.resolve('babel-plugin-transform-es2015-for-of'), 
    require.resolve('babel-plugin-transform-es2015-function-name'), 
    require.resolve('babel-plugin-transform-es2015-literals'),
    require.resolve('babel-plugin-transform-es2015-modules-commonjs'),
    require.resolve('babel-plugin-transform-es2015-object-super'),
    require.resolve('babel-plugin-transform-es2015-parameters'),
    require.resolve('babel-plugin-transform-es2015-shorthand-properties'),
    require.resolve('babel-plugin-transform-es2015-spread'),
    require.resolve('babel-plugin-transform-es2015-sticky-regex'),
    require.resolve('babel-plugin-transform-es2015-template-literals'),
    require.resolve('babel-plugin-transform-es2015-typeof-symbol'),
    require.resolve('babel-plugin-transform-es2015-unicode-regex'),
    require.resolve('babel-plugin-transform-object-rest-spread')
]
mitel commented 7 years ago

UPDATE: using Babili minification i'm now able to minify ES6 code with generator functions:

if (options.minify) {
        const minifyOptions = typeof options.minify === 'object'
            ?   options.minify
            :   {};

        // config.plugins.push(new Webpack.optimize.UglifyJsPlugin(minifyOptions));
        const babiliPluginOptions = { 
            babili:  [
                require.resolve('babel-preset-babili'), {
                    "mangle": {
                        "blacklist": { 
                            "ctx": true, 
                            "cb": true
                        },
                        // "keepFnName": true
                    },
                    // disables babel-plugin-minify-dead-code-elimination
                    // Inlines bindings when possible. Tries to evaluate expressions 
                    // and prunes unreachable as a result.
                    "deadcode": false,
                }
            ]
        }
        const opts = Object.assign({}, minifyOptions, babiliPluginOptions);
        config.plugins.push(new BabiliPlugin(opts));
    }
pencilcheck commented 7 years ago

How do you setup with webtask-bundle? Is there a tutorial on this?

ggoodman commented 7 years ago

@pencilcheck webtask-bundle exists primarily as a library that provides the --bundle functionality to wt-cli and is not intended to be directly consumed.

mrwillis commented 7 years ago

Yes this would be incredibly helpful. I've just realized that I don't think I can use regeneratorRuntime either :( from here.

heymartinadams commented 7 years ago

Also, here: https://github.com/auth0/wt-cli/issues/141

dbismut commented 6 years ago

@ggoodman hey - any plans to add the transform-runtime to the webpack config as suggested in the issue mentioned by @heymartinadams above?

Also @mrwillis if you use the sandbox environment from Webtask as explained here you can use Node 8 ;)

NotMyself commented 6 years ago

Hi Everyone,

Webtask.io now runs on Node 8 directly. Async/Away is supported. I am going to close this issue. Feel free to respond and reopen if you have any questions.

wearhere commented 6 years ago

Hey @NotMyself, should this be supported now?

module.exports = async function(context) {
  return { hello: context.query.name || 'Jeff' };
};

It seems to time out.

NotMyself commented 6 years ago

Hi @wearhere,

I think the bare minimum async webtask would look like this:

module.exports = async function(context, cb) {
  const example = await Promise.resolve({
    hello: context.query.name || 'Anonymous'
  });
  cb(null, example);
};

In your example code, the context parameter will actually be a callback function not the webtask context. See the programming models documentation for details.

In my example, we use the async keyword to enable us to use await sematics within the body of the function. You still need to resolve the function using the callback function.

wearhere commented 6 years ago

Ah thanks for clarifying current support. I filed https://github.com/auth0/webtask-bundle/issues/31 as a follow-up.

mortezae commented 5 years ago

I found a mistake regarding this issue in webtask web interface: Create new > Select a template > Async function You'll see:

Instructions: Simply export an async function and return a result directly to indicate completion. Code:

/**
* @param context {WebtaskContext}
*/
module.exports = async function (context) {
return { hello: context.query.name || 'Anonymous' };
};

But this doesn't wirk and we still need call cb rather just returning