Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rollup fails on non-transformed async functions #548

Closed
infrahead opened this issue Mar 8, 2016 · 8 comments
Closed

rollup fails on non-transformed async functions #548

infrahead opened this issue Mar 8, 2016 · 8 comments

Comments

@infrahead
Copy link

Without enabling babel plugin (and transpiling with regenerator runtime or transform-async-to-generators), the following simple code will fail to bundle with rollup, throwing unexpected token error...

const fn = async () => {
 return 'promise me this'
}

(async () => {
 const msg = await fn()
 console.log(msg)
})()

ran into this issue when trying to build app targeting chakracore.  my first step was to replace all import/export with require calls and strip client-side stuff and see it running successfully without bundling on node-chakra (which supports async/await as well)

Guess my understanding was that could use whatever your runtime supported and rollup could handle the bundling/tree shaking of ES6 modules without requiring babel transpilation?  Wonder how to track down full list of "next-generation" (ES6/7) language constructs that are supported without plugging in Babel?

@infrahead
Copy link
Author

Looking into this further, thinking comes down to limitation of acorn parser?
acornjs/acorn#309

so maybe need to include and enable this plugin?
https://github.com/MatAtBread/acorn-es7-plugin

@Victorystick
Copy link
Contributor

@rustyedges Rollup currently only targets ES6, although it'll likely get extended to ES7 shortly. For now, if you use >ES6 features, they'll need to be transpiled for Rollup to process them.

@infrahead
Copy link
Author

@Victorystick thanks for confirming and the work on rollup. I realized I often conflate ES6 and ES7

@alippai
Copy link
Contributor

alippai commented May 25, 2016

FYI Chrome Canary and Edge Preview has async/await support now. Safari/Webkit will ship it in the next Technical Previews and Firefox is working on this as well.

@js-choi
Copy link

js-choi commented Apr 23, 2017

Mozilla Firefox, Microsoft Edge, Apple Safari (both macOS and iOS), and the Blink platforms Google Chrome, Opera Browser, and Node have all now shipped built-in async functions to their stable releases. Async functions without transpilation are now available to all modern web browsers and to Node. This issue should be revisited.

@regou
Copy link

regou commented Jun 20, 2017

Any update about this?
Some project that only running on modern browsers don't need transpile async functions

@Rich-Harris Rich-Harris reopened this Jun 20, 2017
@fregante
Copy link

I don't think this is an issue in rollup 0.43. Rollup bundles the demo successfully:

$ cat a.js
const fn = async () => {
 return 'promise me this'
}

(async () => {
 const msg = await fn()
 console.log(msg)
})()                                                                               

$ rollup a.js --format iife
(function () {
'use strict';

const fn = async () => {
 return 'promise me this'
};

(async () => {
 const msg = await fn();
 console.log(msg);
})();

}());

Also shown in acorn's own tests: https://github.com/ternjs/acorn/blob/master/test/tests-asyncawait.js

@regou can you open a separate issue describing your problem, if any?

@kevinforrestconnors
Copy link

Installing rollup-plugin-async makes the external dependency warning go away, but I'm still getting Uncaught ReferenceError: regeneratorRuntime is not defined. How to fix?

rollup.config.js:

import babel from 'rollup-plugin-babel'
import resolve from 'rollup-plugin-node-resolve'
import pkg from "./package.json";
import async from 'rollup-plugin-async';

const external = [
    ...Object.keys(pkg.peerDependencies || {}),
    ...Object.keys(pkg.dependencies || {}),
];

const makeExternalPredicate = externalArr => {
    if (externalArr.length === 0) {
        return () => false;
    }
    const pattern = new RegExp(`^(${externalArr.join("|")})($|/)`);
    return id => pattern.test(id);
};

export default {
    input: 'src/cronreader.js',
    output: {
        file: 'dist/bundle.js',
        format: 'iife',
        globals: {
            "@babel/runtime/regenerator": "regeneratorRuntime",
            "@babel/runtime/helpers/asyncToGenerator": "asyncToGenerator"
        }
    },
    external: makeExternalPredicate(external),
    plugins: [
        async(),
        resolve({
            customResolveOptions: {
                moduleDirectory: 'src'
            }
        }),
        babel({
            runtimeHelpers: true,
            exclude: 'node_modules/**', // only transpile our source code
            presets: ["@babel/env"],
            plugins: [
                "@babel/transform-runtime",
                "@babel/transform-async-to-generator",
            ]
        })
    ]
}

I was referred to this page from here: https://stackoverflow.com/questions/39902076/babel-rollup-errors-transpiling-and-bundling-es2017/39936968#39936968

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants