Babel 7和webpack 4无法用于Array中的条件扩展操作[短路语法] [英] Babel 7 and webpack 4 not working for Conditional spread operation in Array[Short circuit syntax]

查看:95
本文介绍了Babel 7和webpack 4无法用于Array中的条件扩展操作[短路语法]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个特殊的问题,即从我使用Stage-0的先前版本升级到Babel 7后,短路语法无法正常工作

I am facing peculiar problem where short circuit syntax is not working after upgrading to Babel 7 from previous version where I was using stage-0 and it was working without any issues

不工作:

...isCSCProfile && [{ isComplete: progressbarStates[3] === PROGRESS_BAR_SUCCESS, messageKey: commonOrderMessages.service_request_header, link: "serviceRequest-header" }],

作品:

...(isCSCProfile ? [{ isComplete: progressbarStates[3] === PROGRESS_BAR_SUCCESS, messageKey: commonOrderMessages.service_request_header, link: "serviceRequest-header" }] : []),

我在Webpack中包含了Babel-Polyfill插件,并且正在测试最新的chrome版本

I have included Babel-Polyfill plugin in webpack and I am testing in latest chrome version

所以问题是我应该包括哪个插件以使其在不更改代码的情况下工作

So question is which plugin should I include to make it work without changing the code

推荐答案

我假设isCSCProfile是布尔值.如果isCSCProfile为false,则您将尝试在第一种情况下传播布尔值,该布尔值将不起作用.

I'm assuming that isCSCProfile is boolean. If isCSCProfile is false, then you're trying to spread a boolean in the first case, which will not work.

深入研究之后,似乎我认为扩展语法不适用于布尔值是错误的. @babel/plugin-transform-spread 具有名为loose的选项,应该跳过空"值,这就是您想要的.还有一个关于宽松模式一致性的公开问题,这很可能是因为不能正常工作.

After digging a bit more into this it seems I was wrong about spread syntax not working for booleans. @babel/plugin-transform-spread has option named loose, that should just skip "nullish" values, which is what you want here. There's also an open issue about loose mode consistency, which is very likely the reason this doesn't work right.

这篇关于Babel 7和webpack 4无法用于Array中的条件扩展操作[短路语法]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆