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.

webpack should not warn import usage, when import is unreachable (e.g. guarded by `if` statement)

See original GitHub issue

Bug report

What is the current behavior?

Bundling

import * as React from "react";
if (React.useId !== undefined) {
  // do something with React.useId
}

will currently warn with export 'useId' (imported as 'React') was not found in 'react' (or crash if strictExportPresence is enabled).

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

  1. Clone https://github.com/eps1lon/webpack-export-shim
  2. yarn start:webpack

What is the expected behavior?

No warning in this case (just like Node.js and browsers with ES modules support. The repro repository has steps to verify the above code is safe in an ES module browser and Node.js). We want to write code that works for both React 17 and React 18 (or rather provides improved behavior for React 18). However, this warning makes using the library very disruptive and even impossible if you have strictExportPresence enabled (like Create React App has): https://github.com/mui-org/material-ui/issues/29860. The warning should probably apply to unconditional usage but in our case we have usage guarded with Module.namedExport !== undefined. This seems statically analyzeable and safe to disable the warning for every usage in that block).

Other relevant information: webpack version: npm:5.64.3 Node.js version: 16.13.0 Operating System: Ubuntu 20.04.3 LTS Additional tools: Chrome Version 96.0.4664.45

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:45
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
alexander-akaitcommented, Nov 24, 2021

We will fix it, no need to duplicate, just wait

1reaction
sgrobertcommented, Nov 24, 2021

Hi there,

Just to add, these two bugs will crash a Vercel setup. So hope that this bug can be resolved soon.

  1. ./node_modules/@mui/utils/esm/useId.js Attempted import error: ‘useId’ is not exported from ‘react’ (imported as ‘React’).

  2. ./node_modules/@mui/utils/esm/useId.js Attempted import error: ‘useId’ is not exported from ‘react’ (imported as ‘React’).


Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack - warn when import is undefined / not declared
Is there a way to get a warning/error message when imports are undefined, i.e. not declared in a file that I am importing...
Read more >
Why your Angular App is not Working: 11 common Mistakes
Importing unnecessarily module bloats your application size significantly. This advice does not only apply to framework angular modules.
Read more >
WebView - Android Developers
FOCUS_DOWN If the reference refers to a view that does not exist or is part of a hierarchy that is invisible, a RuntimeException...
Read more >
The React Handbook – Learn React for Beginners
This book does not try to cover everything under the sun related to React, ... but rather to import for example useState ,...
Read more >
The Complete Guide to React User Authentication with Auth0
React security is not too different from nightclub security. If users want to enter a protected route from your application, Auth0 will stop ......
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