Description
Reproduction link or steps
https://github.com/SuperchupuDev/tsdown-repro/tree/node14-optional-chaining
What is expected?
Optional chaining was added to node in node 14.0.0, according to both mdn and node's 14.0.0 changelog. As such targeting node 14.0.0 should keep the optional chaining syntax as-is, like this:
function myOptionalChain() {
return process?.env?.MY_VAR?.endsWith?.('yup');
}
What is actually happening?
The syntax is replaced, unless you are targeting node16.9.0
or above
function myOptionalChain() {
var _process, _process$endsWith;
return (_process = process) === null || _process === void 0 || (_process = _process.env) === null || _process === void 0 || (_process = _process.MY_VAR) === null || _process === void 0 || (_process$endsWith = _process.endsWith) === null || _process$endsWith === void 0 ? void 0 : _process$endsWith.call(_process, "yup");
}
Any additional comments?
Fixing this should decrease bundle size for all projects that target node 14-16, since replacing optional chaining adds a lot of boilerplate for what it is.
Note that the ??
operator was added in node 14 as well and it works completely fine when setting the target to node14
, which means this issue doesn't extend to other node >=14 features
Also, there are no mentions of optional chaining in the node 16.9.0 changelog, which makes me think setting optional chaining to be >=16.9.0 instead of >=14.0.0 was probably just an accident