How to make webpack accept optional chaining without babel

It occurs that optional chaining won't be supported by Webpack 4 alone, however, it is already supported in Webpack 5, so if it's possible the best course of action would be to upgrade to Webpack 5.

Otherwise if you can't update to Webpack 5, you should use Babel (@babel/plugin-proposal-optional-chaining) or TypeScript compiler (target at most ES2019) for optional chaining transpilation.


According to this similar issue, webpack relies on the parser Acorn and thus presumably needs Acorn to support optional chaining first. Acorn has an open pull request here for optional chaining, but in the meantime, a "solution" suggested by a user in the first issue is to disable parsing on the files you need optional using module.noParse until such a time that Acorn and webpack support the feature.

Update: Optional chaining is now supported in Acorn as of v7.3.0, and according to this webpack issue comment, it sounds like they don't expect webpack to support it until webpack 5 releases. The progress to webpack 5 can be tracked here.


Relating to @Klaycon's answer, Acorn released today a new version that supports optional chaining. As soon as Webpack reflects the change on their side- using optional chaining with webpack shouldn't be a problem anymore.