Skip to content

feat: support import specifier guard#20320

Merged
alexander-akait merged 6 commits intomainfrom
import-guard
Feb 2, 2026
Merged

feat: support import specifier guard#20320
alexander-akait merged 6 commits intomainfrom
import-guard

Conversation

@hai-x
Copy link
Member

@hai-x hai-x commented Jan 17, 2026

Summary

Fixes #14814 and closes #15497.

What kind of change does this PR introduce?

Support import specifier guard detection.

For example:

import * as ns from "react"

if(ns.foo){ // create guard for `ns` => ns: ["", "foo"].
	ns.foo; // ns->"foo" is in guard.
	ns; //  ns->"" is in guard.
}

We will firstly walk expression in IfStatement.test (the same as for ConditionalExpression) to collect import specifier guards which will be tagged to the harmonySpecifierTag variable. Then, while walk both IfStatement.test and the IfStatement.consequent(the same as for ConditionalExpression), we visit these guards to determine whether the export is present at runtime.

Did you add tests for your changes?

Yes

Does this PR introduce a breaking change?

No

If relevant, what needs to be documented once your changes are merged or what have you already documented?

No

@changeset-bot
Copy link

changeset-bot bot commented Jan 17, 2026

🦋 Changeset detected

Latest commit: 01cdc18

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Jan 17, 2026

This PR is packaged and the instant preview is available (cd4793d).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@cd4793d
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@cd4793d
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@cd4793d

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 17, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing import-guard (01cdc18) with main (fe48655)

Summary

✅ 72 untouched benchmarks

@hai-x hai-x marked this pull request as ready for review January 20, 2026 18:51
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good, let's

  • rebase main, because I see some problems style of with jsdocs types (we recently apply eslint rule for this)
  • let's merge HarmonyImportSpecifierGuardParserPlugin and HarmonyImportDependencyParserPlugin into one HarmonyImportDependencyParserPlugin plugin, just create private method and put this logic here, we don't use it somewhere else so no need an extra require call and putting the code into own file, since their logic is very closely related and dependent on each other

@alexander-akait
Copy link
Member

Also let's add changesets log

@hai-x hai-x force-pushed the import-guard branch 2 times, most recently from 8ed0c1e to a22079f Compare February 1, 2026 18:34
@hai-x
Copy link
Member Author

hai-x commented Feb 1, 2026

Thanks for the review. Please take another look.

This was referenced Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants