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

Can PHP send back data to AJAX using variables, possibly in an array format?

My goal is to transmit a datastring via AJAX to a PHP page, receive variables back, and have jQuery populate different elements with those variables. I envision being able to achieve this by simply writing: $('.elemA').html($variableA); $('. ...

Spinner displayed upon task completion

Currently, I am developing a project using Spring Boot. I would like to display a spinner on my page to indicate that a task involving heavy calculations is in progress. Even though the page may take up to 5 minutes to load, my spinner only appears for t ...

The issue of Rails time lagging by a day persists upon deployment on my server

When a user clicks on a day in my calendar, I pass a JavaScript time variable to my Rails controller using Ajax. Everything works perfectly when I test it locally, but once deployed on the server, the date appears to be one day behind the actual day click ...

Securing Your ExpressJs API Files from Prying Eyes in Developer Tools

Typically, when I utilize developer tools in Google and choose to open an API file in a new tab, I am able to view the data as illustrated below. However, there are occasions where upon attempting the same action on certain websites, a security precaution ...

differentiating between user click and jquery's .click() method

There are <a href=".. tags on a page with an inline onclick attribute. If a user clicks one without being logged in, they will be prompted to log in. After logging in and the page refreshes, jQuery will trigger .click() on the <a> element to redir ...

Stop the replication of HTML/CSS styles during the extraction of content from a div

Is there a way to prevent the copying of CSS properties, such as font styles and sizes, when content is copied from a div? I want only the plain text to be copied to the clipboard, without any formatting applied. ...

Tips for adjusting the vue-intro.js styles in a nuxt.js project

I need help changing the appearance of the tooltip button in my web app using vue-intro. I am struggling to update its color and themes within vue-intro.js. https://i.stack.imgur.com/yP7qI.png Specifically, I am looking to modify the Next button color. ...

Guide on sending images as props in a Vue.js component (using Vite instead of require due to compatibility issues)

This is the main component <template> <rooms-card roomImage="../../assets/images/room3.jpg" roomType="Duplex Room" roomDescription="Sami double bed 1 guest room 3 windows" roomPrice="$50/night" /> < ...

Converting an array to a JSON object using JavaScript

I need help with converting the following array into a JSON object: var input = [ 'animal/mammal/dog', 'animal/mammal/cat/tiger', 'animal/mammal/cat/lion', 'animal/mammal/elephant', 'animal/ ...

Adjust the button's color even after it has been clicked

My goal is to update the button's color when it's clicked. I found some examples that helped me achieve this, but there's an issue - once I click anywhere outside of the button, the CSS class is removed. These buttons are part of a form, and ...

Discovering numbers within a JSON object and extracting them - a step-by-step guide

If I have a JSON object coming from a random dataset and want to search through it to manipulate the number values, how can I achieve this? Looping through the object using for...of allows me to get the keys, but I'm unsure how to access every key-val ...

Step by step guide on showcasing live server information obtained from the user-end through AJAX technique in a Javascript Pie Chart, within an ASP.NET html template

I have successfully managed to transfer data from the Client Side (C# Back End) to Server Side (JavaScript HTML in aspx) using AJAX. I need help figuring out how to display my own data dynamically in a JavaScript Pie Chart instead of hardcoding values. He ...

What is the best way to retrieve the value of an input field in React when incorporating Material UI components?

I am working with a few radio input components that have been imported from material Ui react. Each radio input is wrapped in a FormControlLabel component. <FormControlLabel onClick={checkAnswerHandler} value={answer} control={<Radio color=&quo ...

Padding for the initial element in a carousel display

I am currently working on implementing owl carousel with stage padding. My goal is to use the carousel with loop:false, and have the first item displayed without left padding, while maintaining consistent padding for both items on the left and right after ...

The mark-compacts were unsuccessful due to reaching the heap limit, resulting in an allocation failure - the JavaScript heap ran out of memory during deployment

Whenever I try to deploy in npm, I encounter error code 134 along with a fatal error message: "Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory." I've set NODE_OPTIONS --max_old_space_size=2048, and also tr ...

Is it possible to manipulate a modal within a controller by utilizing a certain attribute in HTML, based on specific conditions (without relying on any bootstrap services), using AngularJS?

Within my application, I have a modal that is triggered by clicking on a button (ng-click) based on certain conditions. Here is the HTML code: <button type="button" class="btn btn-outlined" ng-click="vm.change()" data-modal-target="#add-save-all-alert ...

Transforming a circular data structure into JSON format within Firebase

The data returned from firebase is causing an issue when I try to stringify it: JSON.stringify(data) // where data represents the returned object This results in the error: TypeError: Converting circular structure to JSON What is the correct way to hand ...

Concealing scroll bars while still maintaining the ability to scroll by using overflow:scroll

Similar Question: How to hide the scrollbar while still allowing scrolling with mouse and keyboard I have created a sidebar for a web application that needs to enable users to scroll through it without displaying a scrollbar. The content is 500px high ...

Is it possible to halt the execution of a code or skip the remainder of it if a specific condition is met using jQuery?

Is it possible to prevent a code from completing or skipping the remaining parts based on a specific condition? For example, var stopCode = false; if (stopCode === true) break; .... .... blah blah blah the remaining part of the code .... ... Can this b ...

What is the best way to trigger a controller action using jQuery in your application.js file?

Currently, I am incorporating the jQuery autocomplete plugin into my project and looking to personalize a specific event: select: function(event, ui) { $('.topic_field').val(ui.item.topic.name); return false; This event es ...