gpt4 book ai didi

javascript - 对象传播运算符在 Node 8.6 的 webpack 上被检测为错误

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:47 24 4
gpt4 key购买 nike

我正在尝试将我的应用程序的 Node 版本从 node6 升级到 native 支持展开运算符的 node8.6,一切正常,直到我尝试使用 webpack 编译我的 Node 脚本。

我已经创建了一个测试脚本(也测试了对 async/await 的原生支持):

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 配置:

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 构建输出: enter image description here

object spread 会报错,async/await 会默认转译,即使 target: 'node' 是在 webpack 配置中设置的...

更新这是 package.json:enter image description here

最佳答案

npm install babel-plugin-transform-object-rest-spread --save

如果规则数组在 webpack.config 文件中,则在第一个对象中包含以下查询

query: {
plugins:[ 'transform-object-rest-spread' ]
}

关于javascript - 对象传播运算符在 Node 8.6 的 webpack 上被检测为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com