webpack should not warn import usage, when import is unreachable (e.g. guarded by `if` statement)
See original GitHub issueBug 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.
- Clone https://github.com/eps1lon/webpack-export-shim
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:
- Created 2 years ago
- Reactions:45
- Comments:11 (3 by maintainers)
Top GitHub Comments
We will fix it, no need to duplicate, just wait
Hi there,
Just to add, these two bugs will crash a Vercel setup. So hope that this bug can be resolved soon.
./node_modules/@mui/utils/esm/useId.js Attempted import error: ‘useId’ is not exported from ‘react’ (imported as ‘React’).
./node_modules/@mui/utils/esm/useId.js Attempted import error: ‘useId’ is not exported from ‘react’ (imported as ‘React’).
Thank you.