【问题标题】:Why babelConfig not working in webpack encore?为什么 babelConfig 在 webpack encore 中不起作用?
【发布时间】:2019-05-07 01:03:50
【问题描述】:

我想为我的项目添加对 async / await 函数的支持。

我安装

    "@babel/core": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.2.0",
    "@babel/preset-es2015": "^7.0.0-beta.53",
    "@babel/preset-stage-2": "^7.0.0",
    "@babel/runtime": "^7.2.0",

这是我的webpack.config.js

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

Encore
    .setOutputPath('public/build')

    .setPublicPath('/build')

    .addEntry('app', './assets/app.js')

    .enableSourceMaps(!Encore.isProduction())

    .cleanupOutputBeforeBuild()

    .enableBuildNotifications()

    .enableVueLoader()

    .configureBabel(function(babelConfig) {
       babelConfig.presets.push('@babel/preset-env');
       babelConfig.presets.push('@babel/preset-stage-2');
       babelConfig.plugins.push('@babel/plugin-transform-runtime');
    })

;

const config = Encore.getWebpackConfig();

config.externals = {

    mode: 'development',
    // global app config object
    config: JSON.stringify({
        apiUrl: 'http://localhost:80',
        devServer: {
            public: 'http://localhost:3000',
            disableHostCheck: true,
        },
    })
};

config.node = {
    fs: "empty"
};


module.exports = config;

当我运行服务器开发时,出现错误。

    Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Duplicate plugin/preset detected.
If you'd like to use two separate instances of a plugin,
they need separate names, e.g.

  plugins: [
    ['some-plugin', {}],
    ['some-plugin', {}, 'some unique name'],
  ]

我不明白问题出在哪里。

我还创建了一个 .babelrc 文件并在其中编写了相同的配置。但不幸的是,这并没有帮助(

【问题讨论】:

  • 你使用的是哪个版本的 babel ?

标签: vue.js webpack babeljs webpack-encore


【解决方案1】:

使用.babelrc 像这样更改 babel 配置。这个文件应该在你的项目根目录中

{
  "plugins": ["@babel/plugin-transform-runtime"],
  "presets": [
    [
      "@babel/preset-env",
      ...
    ],
    ...
  ]
}

然后将其从您的 webpack.config.js 中删除

.configureBabel(function(babelConfig) {
   babelConfig.presets.push('@babel/preset-env');
   babelConfig.presets.push('@babel/preset-stage-2');
   babelConfig.plugins.push('@babel/plugin-transform-runtime');
})

【讨论】:

  • 如果 Babel 版本大于 7,则不应该使用预设。 ``` .configureBabel(function(babelConfig) { babelConfig.plugins = ['@babel/plugin-transform-runtime', '@babel/plugin-syntax-dynamic-import']; })```
  • @Alexandr 如果 Babel 版本超过 7,您可以使用预设事件。我正在使用带有预设和 Webpack Encore 的 Babel 7,一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2016-12-16
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多