question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional chain is incorrectly removed when used on import

See original GitHub issue

Bug report

What is the current behavior?

Webpack incorrectly compiles x?.value to (something).value when x is something that’s been imported.

If the current behavior is a bug, please provide the steps to reproduce.

webpack.config.js:

module.exports = {
    entry: "./index.js",
    target: "web",
};

index.js:

import x from './x.js';
console.log(`optional value is: ${x?.value}`);

x.js:

export default undefined;

Produces dist/main.js:

(()=>{"use strict";console.log(`optional value is: ${(void 0).value}`)})();

which leads to TypeError: Cannot read property 'value' of undefined at runtime.

What is the expected behavior?

The ? should be retained or converted to a ternary operator.

When import x from './x.js'; is removed and replaced with const x = (() => void console)(); (something complicated enough that webpack doesn’t completely constant-fold it), the output is:

(()=>{const o=void console;console.log(`optional value is: ${o?.value}`)})();

which is as expected (the ? is retained).

Other relevant information: webpack version: latest, 5.27.2 Node.js version: v15.9.0 Operating System: macOS 11.2.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:29
  • Comments:26 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ekilahcommented, Apr 1, 2022

seems to be included in 5.71.0 now 🥳

image
2reactions
Cherrycommented, Jul 23, 2021

A simple workaround we’ve found is referencing the imported module as another variable. For example:

import x from './x.js';
const xRef = x;
console.log(`optional value is: ${xRef?.value}`);

Not ideal, but it works until this can be fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is wrong with optional chaining and how to fix it - DEV ...
Optional chaining simplifies above and removes a lot of errors by saying that values considered as no value are only two - null...
Read more >
How to make webpack accept optional chaining without babel
We're using webpack 4 to create a bundle from our Javascript sources. We're not using Babel because we're authoring only for a single...
Read more >
Minified code in LWC is wrong because of optional chaining ...
Incorrectly minified optional chaining is a known bug. Here is a quote from the response given by Salesforce support on this issue.
Read more >
Optional chaining '?.' - The Modern JavaScript Tutorial
The optional chaining ?. is a safe way to access nested object ... There's a little better way to write it, using the...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
By using the ?. operator instead of just . , JavaScript knows to implicitly check to be sure obj.first is not null or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found