对象扩展运算符在节点8.6的webpack上检测为错误 [英] object spread operator detected as an error on webpack for node 8.6
问题描述
我正在尝试将我的应用程序的节点版本从node6升级到node8.6,它本身支持spread运算符,一切正常,直到我尝试使用webpack编译我的节点脚本。
I'm trying to upgrade my application's node version from node6 to node8.6 which supports spread operator natively, all works fine until I try to compile my node script using webpack.
我已经创建了一个测试脚本(测试async / await的原生支持,适用于场合):
I've created a test script (testing native support for async/await too, for the occasion):
const fs = require('fs')
const {promisify} = require('util')
const app = async () => {
try {
const todosString = await promisify(fs.readFile)('todos.txt', {
encoding: 'utf8',
})
return todosString
.split('\n')
.filter(Boolean)
.reduce((acc, val) => ({...acc, [val]: true}), {})
} catch (e) {
console.error('wooot', e)
}
}
app().then(console.log)
这是webpack配置:
Here is the webpack configuration:
const path = require('path')
const nodeExternals = require('webpack-node-externals')
module.exports = {
entry: './index.js',
output: {filename: '[name].js'},
target: 'node',
externals: [nodeExternals()],
node: {
__dirname: true,
__filename: true,
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: [
[
'env',
{
targets: {
node: 'current',
modules: false,
},
},
],
],
},
},
],
},
}
这是我的webpack构建输出:
And here is my webpack build output:
对象传播给出错误,默认情况下会转换async / await,甚至 target:'node'
在webpack配置中设置...
The object spread gives an error and async/await is transpiled by default, even target: 'node'
is set on the webpack config...
更新这是package.json:
UPDATE this is the package.json:
推荐答案
npm install babel-plugin-transform-object-rest-spread --save
并包含如果webpack.config文件中的rules数组
and include the below query in first object if the rules array in webpack.config file
query: {
plugins:[ 'transform-object-rest-spread' ]
}
这篇关于对象扩展运算符在节点8.6的webpack上检测为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!