简体   繁体   English

在 webpack.config.js 中使用扩展运算符的 SyntaxError

[英]SyntaxError using spread operator in webpack.config.js

In my webpack.config.js i am importing several JS objects for the configuration from other files.在我的 webpack.config.js 中,我从其他文件中导入了几个用于配置的 JS 对象。 These object are composed using spread operator.这些对象是使用扩展运算符组成的。 Ex.前任。 in build.js在 build.js 中

const otherObj = { a:[], b: [] }
const conf = {
  prop1: [],
  ...otherObj
}

Then i want to use these objects in the webpack.config.js in this way:然后我想以这种方式在 webpack.config.js 中使用这些对象:

const { prop1, a, b } = require('./build.js');
module.exports = {
    entry: { prop1, a, b },
    rules: [
      {
        test: jsRegex,
        exclude: /node_modules/,
        use: ["babel-loader"]
      },
    ]
}

When i launch the build script i get a SyntaxError:当我启动构建脚本时,我收到一个 SyntaxError:

    ...otherObj,
^^^
SyntaxError: Unexpected token ...

NOTE: Here follows the .babelrc注意:这里遵循 .babelrc

    {
    "presets": [
        "@babel/preset-env"
    ],
    "plugins": [
        "@babel/syntax-object-rest-spread"
    ] 
}

That depends on the version of node that you are using.这取决于您使用的node版本。 For example, using node v6.15.1 (Boron) will return the same error you have, but when running on v8.14.0 (Carbon) it compiles just fine.例如,使用 node v6.15.1 (Boron) 将返回与您相同的错误,但在v8.14.0 (Carbon) 上运行时,它编译得很好。

Use node使用节点

Check that the node version you are using supports the features you need.检查您使用的节点版本是否支持您需要的功能。

The feature you are using object spread properties was added on v8.6.0 (running node without flags).您正在使用的对象扩展属性的功能已添加到v8.6.0 (无标志的运行节点)。

Use babel使用巴别塔

You can transpile your webpack config , this way you can use your current node version and use all the ES6 features you may like.您可以转译您的 webpack 配置,这样您就可以使用当前的节点版本并使用您可能喜欢的所有 ES6 功能。

You should add .babelrc file like this below.你应该像下面这样添加 .babelrc 文件。

Then do install: npm i babel-plugin-transform-object-rest-spread然后安装: npm i babel-plugin-transform-object-rest-spread

{
    "presets": ["env", "react"],
    "plugins": ["transform-object-rest-spread"]

}

This is worked for Me, try it!这对我有用,试试吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM