Setting up webpack encore for async and await in a Symfony 4 and VueJs project

After setting up a VueJs project within Symfony 4, I encountered an unexpected error involving await and async (Uncaught ReferenceError: regeneratorRuntime is not defined)

I've come across plenty of resources for webpack, but nothing specifically for webpack encore

https://github.com/babel/babel/issues/5085

What configuration should be used for webpack.config.js with webpack encore in Symfony 4?

Answer №1

Utilizing a babel plugin is another option, and it can be done without making changes to the webpack configuration.

The specific babel plugin to consider is babel-plugin-transform-async-to-generator

Read more about this plugin here

For example, here is a snippet from our .babelrc configuration file:

{
  "presets": ["es2015"],
  "plugins": [
    "transform-async-to-generator"
  ]
}

Answer №3

Here is the solution:

    .configureBabel(function(babelConfig) {
        babelConfig.presets = ['es2015','stage-2']
        babelConfig.plugins = ['transform-runtime']
    })

Include all of this content in your file:

var Encore = require('@symfony/webpack-encore');

Encore
    // Set the output path for compiled assets
    .setOutputPath('public/build/')
    // Define the public path for web server access
    .setPublicPath('/build')
    .cleanupOutputBeforeBuild()
    //.createSharedEntry('assets', ['babel-polyfill'])
    .enableSourceMaps(!Encore.isProduction())
    // Uncomment to create hashed filenames
    // .enableVersioning(Encore.isProduction())

    // Define project assets
    .addEntry('js/app', './assets/js/app.js')
    .addEntry('vue', './assets/js/Vue/main.js')
    .addStyleEntry('css/app', './assets/scss/style.scss')
    .addStyleEntry('css/vue', './assets/scss/vue.scss')
    // Enable Sass/SCSS files
    .enableSassLoader()
    .autoProvidejQuery()
    .enableVueLoader()
    .enableSassLoader(function(sassOptions) {}, {
            resolveUrlLoader: false
     })
    .configureBabel(function(babelConfig) {
        babelConfig.presets = ['es2015','stage-2']
        babelConfig.plugins = ['transform-runtime']
    })
;

module.exports = Encore.getWebpackConfig();

Don't forget to install the necessary packages:

npm install babel-preset-stage-2
npm install babel-preset-es2015 (or es2017)

npm install babel-plugin-transform-runtime

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for aligning ticks to the left on a d3 bar chart

i recently finished creating a stacked bar graph with varying tick lengths on the y-axis side. my goal is to align the text within the ticks to the left, similar to this example: http://jsfiddle.net/2khbceut/2/ here is the HTML: <title>Diverging Sta ...

Attempting to retrieve and save data in a variable using React Native

click here for image referenceI am a beginner in react-native and I recently attempted to use fetch to access an API. While I successfully managed to print the result on the console, I encountered difficulty in displaying it on a mobile screen. Below is a ...

JavaScript: Identify the variable that has been updated

When checking for changes in the values of a couple of variables, I use the following condition: <% if (ctx.recipient.@firstName != ctx.recipient.@firstName_init || ctx.recipient.@lastName != ctx.recipient.@lastName_init || ctx.recipient.@emailPreferred ...

Begin by opening a single div for each instance

I want a functionality where opening one div closes all the others. I've searched around for solutions and only found jQuery options, not pure JavaScript ones. Here is my code: function openDescription(description_id) { var x = document.getEle ...

Importing a JS file within a JS script

Currently, I am facing a requirement to dynamically include a .js file (more jQuery code) within a working jQuery script. Specifically, when my page gets authenticated, I need to add a specific script file. I am looking to accomplish this in ASP.Net MVC. ...

Retrieve the image and insert it using an img tag

Working on a project, I want to include Instagram profile pictures by embedding them. Although I have the image URL, I am struggling to integrate it into my HTML page. Take a look at this sample: This provided link displays the Instagram profile picture. ...

How to reset or clear the RangePicker in Ant Design for React

I am working with a component similar to this one and I am looking for a way to make it automatically reset after the user selects a date. Currently, once a date is selected, it remains as is until manually cleared. Current behavior: Desired behavior: ...

Vue.js - Problem with loading image on screen

Although I have experience with React, I am currently facing the challenge of using Vue for a code assessment for the first time. My struggle lies in creating a reusable image component with WAI-ARIA accessibility. Despite my efforts, I cannot get the imag ...

Guide to altering JSON using Javascript

https://github.com/smelukov/loftschool-example i am currently working on my project in this environment. I have created a friends.json file in the main folder. friends.json { "name": "John", "lastName": & ...

What could possibly be the reason for this not returning null?

Consider this JavaScript snippet: var value = parseInt(""); console.log(value != Number.NaN ? value : null); Why does the console output Nan instead of null? Is there a way to modify the code so that it actually returns a null value? I attempted to wra ...

Can someone explain the process of adding a JavaScript state variable from one React component so that it can be utilized in other components?

In my Home.js component, I handle user sign up to the API and login. After receiving the token from the authorization header in the response, I save it in the state variable 'token'. This token is crucial for accessing the API in other component ...

Setting up Vue.js for development

I am in the process of creating a client application with Vue.js, but I am currently grappling with a crucial question: How can I obtain the address of my backend service? There is one major requirement for the solution to this issue: The final build arti ...

Is It Possible to Save Data to Local Storage Using Vue.js?

I am currently working on storing data using Vue.js and local storage. By writing localStorage.set('something', 5) in my main.js file, I can view the data in the Chrome dev tools under the 'Storage' section in the 'Application&apo ...

Having trouble making event listeners work with React useState in Next.js?

I'm currently working on a webpage where I want to have a fixed hamburger icon in the top-right corner that, when clicked, opens a navbar. The navbar should close if the user clicks outside of it, and the hamburger icon should reappear. Since Next.js ...

Managing arrayBuffer in hapi.js: A Comprehensive Guide

I am struggling to upload an arrayBuffer to my server and save it to a file. On the client side, I am using axios, and on the server side, I have implemented Hapi js. However, I am facing difficulties in extracting data from the request in the Hapi handler ...

Searching for a method in Vue to monitor values and updates from a distinct component or JavaScript file

I am working with a plain JavaScript file that includes various functions, one of which retrieves a value from either 1. local storage, 2. Vuex, or uses a default value if neither is present. These values can be updated throughout the entire application by ...

Comparing functions and function issues when using onClick in JavaScript

I have successfully created an Image Element on my web page dynamically using JavaScript. I am now trying to set an onClick event for the newly created image element. However, I am encountering a problem and I cannot figure out why?, This is the code I ...

Click on a div to switch between displaying an arrow pointing to the right and an arrow

Currently working on a design for an accordion-style layout and looking to implement toggling arrow icons when the div is clicked. I have managed to toggle the content of the div successfully, but now seeking assistance in achieving the same functionality ...

What is the correct way to properly wrap the div component in this code snippet?

I am currently working on implementing a Javascript component that displays pinned locations and related information on a map. I have made some progress with the implementation, but unfortunately, it does not appear correctly in the actual site. There is a ...

Troubleshooting Async Issues in Node.js

I am encountering an issue with my node.js app structure, which is as follows: async.forever( function(callback) { async.series([ function(callback) { SomeFunction1(function(err, results) { if (err) callback(err); ...