为async配置webpack encore并等待(Symfony 4和VueJs)

时间:2018-04-27 07:32:35

标签: javascript symfony vue.js symfony4 webpack-encore

我在Symfony 4中安装了一个VueJs项目,但是当我遇到await和async错误时我感到很惊讶(Uncaught ReferenceError:未定义了regeneratorRuntime)

我找到了很多关于webpack的信息,但没有找到webpack安可

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

带有webpack encore(Symfony 4)的webpack.config.js的配置是什么?

3 个答案:

答案 0 :(得分:1)

您也可以使用babel插件,无需更改webpack config

  

巴别-插件 - 转换 - 异步到发电机

https://babeljs.io/docs/plugins/transform-async-to-generator/

e.g。我们的.babelrc配置的一部分:

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

答案 1 :(得分:0)

解决方案是

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

文件的所有内容

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

Encore
    // the project directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // the public path used by the web server to access the previous directory
    .setPublicPath('/build')
    .cleanupOutputBeforeBuild()
    //.createSharedEntry('assets', ['babel-polyfill'])
    .enableSourceMaps(!Encore.isProduction())
    // uncomment to create hashed filenames (e.g. app.abc123.css)
    // .enableVersioning(Encore.isProduction())

    // uncomment to define the assets of the project
    .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')
    // uncomment if you use 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();

当然你必须安装软件包

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

npm install babel-plugin-transform-runtime

答案 2 :(得分:0)

根据https://babeljs.io/docs/en/babel-polyfill

import "core-js/stable";
import "regenerator-runtime/runtime";

这非常适合Webpack Encore