Skip to content

Commit 2421ea7

Browse files
fix(babel): normalize resolved paths to POSIX so Windows rewrites work (#390)
path.resolve separates with backslashes on Windows, so a relative import resolved against the file never matched the forward-slash package paths the handlers split on, and was left unrewritten.
1 parent 8e4c822 commit 2421ea7

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎src/babel/helpers.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { dirname, resolve, sep } from "path";
2+
13
import tBabelTypes, { type CallExpression } from "@babel/types";
24

35
export type BabelTypes = typeof tBabelTypes;
@@ -38,3 +40,8 @@ export function getInteropRequireDefaultSource(
3840

3941
return requireArg.value;
4042
}
43+
44+
// The handlers match "/"-separated package paths, and path.resolve separates with "\" on Windows
45+
export function resolveImportSource(filename: string, source: string): string {
46+
return resolve(dirname(filename), source).split(sep).join("/");
47+
}

‎src/babel/react-native-web.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { dirname, resolve } from "path";
2-
31
import { type NodePath } from "@babel/traverse";
42
import tBabelTypes, {
53
type CallExpression,
@@ -10,12 +8,13 @@ import tBabelTypes, {
108
} from "@babel/types";
119

1210
import { allowedModules } from "./allowedModules";
11+
import { resolveImportSource } from "./helpers";
1312

1413
type BabelTypes = typeof tBabelTypes;
1514

1615
function parseReactNativeWebSource(source: string, filename: string) {
1716
if (source.startsWith(".")) {
18-
source = resolve(dirname(filename), source);
17+
source = resolveImportSource(filename, source);
1918

2019
const internalPath = source.split("react-native-web/dist")[1];
2120
if (!internalPath) {

‎src/babel/react-native.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { dirname, resolve } from "path";
2-
31
import { type NodePath } from "@babel/traverse";
42
import tBabelTypes, {
53
type ImportDeclaration,
@@ -9,12 +7,13 @@ import tBabelTypes, {
97
} from "@babel/types";
108

119
import { allowedModules } from "./allowedModules";
10+
import { resolveImportSource } from "./helpers";
1211

1312
type BabelTypes = typeof tBabelTypes;
1413

1514
function parseReactNativeSource(source: string, filename: string) {
1615
if (source.startsWith(".")) {
17-
source = resolve(dirname(filename), source);
16+
source = resolveImportSource(filename, source);
1817

1918
const internalPath = source.split("react-native/Libraries/Components/")[1];
2019
if (!internalPath) {

0 commit comments

Comments
 (0)