Does Laravel Mix support ES8?
Asked Answered
D

1

7

I'm compiling my javascript files with Laravel mix, and as I am not very knowledgeable yet on Babel and package.json, I want to ask if Laravel Mix supports ES8, especially async/await?

If I try it, I can't tell if Mix is transpiling async/await to ES5 or if async/await is simply supported by my browser, which is the latest version. I still want it to be transpiled to ES2015 so the application will still works on browsers that only support ES5.

Depurative answered 26/8, 2017 at 11:19 Comment(2)
Laravel Mix uses babel under the hood, if the correct plugins are added to babel the async/await syntax is supported. I'm currently investigating how to do this, I'll let you know.Angelika
@Angelika great, any help would be appreciated. Excited to know how your investigation goes, I'm stumped as I don't know where to start looking, I've Googled a lot but to no avail.Depurative
A
20

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.

Aggappe answered 9/9, 2017 at 19:8 Comment(1)
It's recommended that you use babel-preset-env. It contains both es2015 & stage-3. Then you just need ["env"]Dozier

© 2022 - 2024 — McMap. All rights reserved.