Skip to content

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

@eps1lon

Description

@eps1lon

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): mui/material-ui#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

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Priority - High

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions