fix(babel): normalize resolved paths to POSIX so Windows rewrites work#390
Open
YevheniiKotyrlo wants to merge 1 commit into
Open
fix(babel): normalize resolved paths to POSIX so Windows rewrites work#390YevheniiKotyrlo wants to merge 1 commit into
YevheniiKotyrlo wants to merge 1 commit into
Conversation
The import plugin's relative-import handlers resolve a source against the file being transformed and then match the result against forward-slash literals (`react-native/Libraries/Components/`, `react-native-web/dist`). path.resolve returns backslash-separated paths on Windows, so those splits never match and the import is left un-rewritten. Only relative imports break, and only on Windows -- bare specifiers are plain string matches; the failing cases were green on the Linux/macOS CI. Add a `resolvePosix` helper (resolve + normalize \ -> /) and use it in parseReactNativeSource / parseReactNativeWebSource. Resolve semantics are unchanged; only the separator is normalized. import-plugin's isFromThisModule stays on path.resolve -- it compares two OS-native paths, so it already works. Adds a helpers unit test asserting the POSIX invariant.
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
The babel import plugin's relative-import rewriting silently no-ops on Windows.
parseReactNativeSource/parseReactNativeWebSourceresolve a relative source against the file being transformed and then match the result against forward-slash literals:path.resolvereturns backslash-separated paths on Windows, so.split("react-native/Libraries/Components/")(and thereact-native-web/distsplit) never matches —internalPathisundefined, the function bails, and the import is left un-rewritten. The plugin's other cases (barereact-native/react-native-webspecifiers) are plain string matches and are unaffected, so only relative imports break, and only on Windows.This surfaces as three tests failing on Windows while green on the Linux/macOS CI:
src/__tests__/babel/react-native.test.ts›7. import View from '../View/View';src/__tests__/babel/react-native-web.test.ts›6. import View from '../View';src/__tests__/babel/react-native-web.test.ts›17. const View = _interopRequireDefault(require('../View'));Fix
Normalize the resolved path to POSIX separators before the forward-slash matching, via a small shared helper:
parseReactNativeSourceandparseReactNativeWebSourcenow callresolvePosixinstead ofresolve. The resolve semantics are unchanged — each keeps its own base (dirname(filename)vsfilename); only the separator is normalized.import-plugin.ts'sisFromThisModuleis intentionally left onpath.resolve: it compares two OS-native absolute paths (state.filename.startsWith(thisModuleDist)), so both sides share the platform separator and it already works — normalizing only one side would break it.Test plan
src/__tests__/babel/helpers.test.tsassertsresolvePosixnever returns a backslash and collapses..likepath.resolve— a platform-independent guard of the invariant (the e2e cases only exercise the Windows path on a Windows runner).yarn test(full suite green),yarn typecheck,yarn eslint, andprettier --checkall pass.