async / await with Laravel Mix:
If you use Laravel Mix out of the Box and use async and await you get the following error message:
Uncaught ReferenceError: regeneratorRuntime is not defined
But Laravel Mix uses Babel to support ES2015. We can customise the compilation if we need to.
To get async / await working, add the file .babelrc
to your root directory with this content:
{
"presets": ["es2015", "stage-3"],
"plugins": [
"transform-runtime"
]
}
And install the needed npm packages:
npm install babel-preset-es2015 babel-preset-stage-3 babel-plugin-transform-runtime --save-dev
The important thing (which caused the error) is the transform-runtime plugin. It is not shipped with Laravel Mix, but you need it in order to get the async / await feature working.
ES8:
As you have seen above, you can use different preset stages in Babel. With them you can use features that are included in ES8 or later. For example stage-3 brings the async / await feature.
They have an overview of the stages on their website.