Skip to content

[WIP] ⟡ fix(babel-plugin-import-path-remapper): honor source conditions in exports - #4369

Open
Sverre Johansen (sverrejoh) wants to merge 1 commit into
microsoft:mainfrom
sverrejoh:source-condition-remap
Open

Sverre Johansen (sverrejoh) wants to merge 1 commit into
microsoft:mainfrom
sverrejoh:source-condition-remap

Conversation

@sverrejoh

Copy link
Copy Markdown
Member

Description

babel-plugin-import-path-remapper skips any package that declares exports,
returning before a remap is attempted. That makes lib -> src remapping
impossible for those packages even when the package explicitly declares where
its source lives, which blocks monorepos that would like to move off
hand-maintained alias maps and onto exports maps.

This change honors an explicitly declared source condition on the root "."
export — the shape create-react-native-library generates:

"exports": {
  ".": {
    "source": "./src/index.ts",
    "types": "./lib/index.d.ts",
    "default": "./lib/index.js"
  }
}
  • New sourceExportCondition option (string | string[]), defaulting to [].
    With no configuration nothing changes, so the new behavior is opt-in.
  • Only a plain string on the root "." export is considered. Nested conditions
    and subpath exports are deliberately not resolved.
  • A package that declares exports but no matching source condition is still
    left alone. exports is the package's public contract, so the plugin does not
    guess its way around it.

One behavior change worth calling out for existing users: a custom remap
callback is now reachable for packages that declare exports. Previously such
packages returned early, so remap was never invoked for them.

Follow-up to #4023, which was closed for inactivity with an offer to reopen.
This is a re-authored and narrower take on a fresh branch rather than a revival
of that diff.

Test plan

yarn test in packages/babel-plugin-import-path-remapper — 20/20 pass.

Six tests are added, backed by fixtures under
test/__fixtures__/node_modules/@rnx-kit/example:

  • sourceExportCondition is validated and throws if it is not a string or an
    array of strings
  • a package declaring a source condition on the root export is remapped to
    that entry
  • the same works for a caller-chosen condition name
  • a package with exports but no matching source condition is left untouched
  • source conditions are left alone entirely unless sourceExportCondition is set
  • a custom remap callback runs for packages with exports

Run against the pre-change source as a control, four of the six fail (16/20
pass), so they pin the new behavior rather than only describing it. The
remaining two pass in both directions by design: they assert the opt-in default
is a no-op, which the previous code also satisfied by skipping exports
packages outright.

…ports

Packages that declare entry points via exports were skipped entirely,
making lib -> src remapping impossible even when the package explicitly
declares its source entry. Honor an explicitly declared source condition
on the root export (the shape create-react-native-library generates),
make the remap callback reachable for exports packages, and leave
everything else untouched.
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.

1 participant