Apply extensionAlias to imports-field resolutions#549
Merged
alexander-akait merged 3 commits intomainfrom Apr 22, 2026
Merged
Conversation
When a request goes through the package.json "imports" field (e.g. `#foo` -> `./foo.js`), the resolver skipped `raw-resolve` where the `extensionAlias` plugin is tapped, so TypeScript-style extension substitution (`.js` -> `.ts`) never fired for internal subpath imports. This made it impossible to use `extensionAlias` together with the imports field, which is what TypeScript projects need when their imports map to `.js` targets but the actual source is `.ts`. The imports field is internal to the package, so applying the consumer's aliases does not expose files the package author never intended to expose -- unlike the exports field, where the same change would cross a trust boundary (see issue #355 / #413). Changes: - Teach ExtensionAliasPlugin to also operate on `request.path` / `request.relativePath` when the specifier has already been joined (i.e. `request.request === undefined`). - Introduce an `imports-field-relative` hook between ImportsFieldPlugin and the `relative` hook, tap per-alias plugins on it, and forward to `relative` by default. - Leave the exports field untouched. Fixes #413 for the imports-field case.
|
🦋 Changeset detectedLatest commit: 418fd35 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #549 +/- ##
==========================================
- Coverage 96.58% 96.57% -0.02%
==========================================
Files 50 50
Lines 2783 2800 +17
Branches 850 863 +13
==========================================
+ Hits 2688 2704 +16
- Misses 79 80 +1
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Reflects the change that extensionAlias now applies to paths resolved via the imports field: - Note the behavior and the exports-field carve-out on the extensionAlias option docs. - Update ExtensionAliasPlugin and ImportsFieldPlugin wiring lines. - Add imports-field-relative to the pipeline hooks table. - Extend the "internal import" flow diagram to show the relative-target branch through imports-field-relative.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes part of #413 (the
importsfield half).extensionAliasis tapped onraw-resolveand keys offrequest.request. When a request is resolved through the package.jsonimportsfield,ImportsFieldPluginbypassesraw-resolveand hands off to therelativehook withrequest: undefined(onlyrequest.pathis set), so TypeScript-style extension substitution (.js→.ts) never fires for internal subpath imports like#foo. This madeextensionAliasunusable together with the imports field — the exact setupeslint-import-resolver-typescriptneeds.Unlike the
exportsfield, theimportsfield is internal to the package, so applying the consumer's aliases does not expose files the package author never meant to expose (the concern raised in #355 / #413). For that reason this enables the behavior by default forimportsonly; theexportsside can still be discussed as an opt-in later.Before
After
Changes
ExtensionAliasPlugin: handle both the existing "request" mode (specifier-based) and a new "path" mode (whenrequest.request === undefined, operate onrequest.path/request.relativePath).ResolverFactory: introduce animports-field-relativehook betweenImportsFieldPluginandrelative, tap per-alias plugins there, and forward torelativeby default viaNextPlugin.ExportsFieldPluginis left untouched.test/extension-alias.test.jscovering the primary alias, fallback to later alternatives, strict (no-fallback) mapping, and single-string alias.Test plan
npx jest— 996 passing (992 before + 4 new)npx jest test/extension-alias.test.js— 12 passingnpx tsc— cleannpx eslint lib/ExtensionAliasPlugin.js lib/ResolverFactory.js test/extension-alias.test.js— cleanself-ref/foo→foo.js) unchangedhttps://claude.ai/code/session_01JGjZtthJN27rDbRPUAPjQh
Generated by Claude Code