Skip to content

Commit e9eeb19

Browse files
refactor(metro): share the babel plugin's test for this package's own modules
The resolver computed the package root and the own-module test the babel import plugin already computes, and its copy dropped the trailing path separator, so a directory beside the package whose name merely begins with dist or src counted as the package's own and its imports were left unrewritten. Both now import one isFromThisModule.
1 parent 1127305 commit e9eeb19

4 files changed

Lines changed: 41 additions & 30 deletions

File tree

‎src/__tests__/metro/resolver.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ describe("nativeResolver", () => {
144144
});
145145
});
146146

147+
test.each([
148+
["dist", join(packageRoot, "distribution", "index.js")],
149+
["src", join(packageRoot, "src-generated", "index.js")],
150+
])(
151+
"rewrites an import from a directory whose name only begins with %s",
152+
(_prefix, originModulePath) => {
153+
const { resolver, calls } = recordingResolver();
154+
155+
nativeResolver(
156+
resolver,
157+
contextFor(originModulePath),
158+
"react-native-gesture-handler",
159+
"ios",
160+
);
161+
162+
expect(calls).toEqual([
163+
["react-native-gesture-handler", "ios"],
164+
["react-native-css/components/react-native-gesture-handler", "ios"],
165+
]);
166+
},
167+
);
168+
147169
test("leaves a resolution that is not a source file alone", () => {
148170
const calls: [string, string | null][] = [];
149171
const resolver: CustomResolver = (_context, moduleName, platform) => {

‎src/babel/import-plugin.ts‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { basename, dirname, resolve, sep } from "path";
2-
31
import { type PluginObj } from "@babel/core";
42
import type { Statement } from "@babel/types";
53

@@ -19,6 +17,7 @@ import {
1917
handleReactNativeWebInteropRequireDefault,
2018
handleReactNativeWebObjectPatternRequire,
2119
} from "./react-native-web";
20+
import { isFromThisModule } from "./this-module";
2221

2322
export default function ({
2423
types: t,
@@ -27,19 +26,6 @@ export default function ({
2726
}): PluginObj<PluginState> {
2827
const processed = new WeakSet();
2928

30-
const packageRoot = resolve(
31-
__dirname,
32-
basename(dirname(__dirname)) === "src" ? "../.." : "../../..",
33-
);
34-
const thisModuleDist = resolve(packageRoot, "dist") + sep;
35-
const thisModuleSrc = resolve(packageRoot, "src") + sep;
36-
37-
function isFromThisModule(filename: string): boolean {
38-
return (
39-
filename.startsWith(thisModuleDist) || filename.startsWith(thisModuleSrc)
40-
);
41-
}
42-
4329
return {
4430
name: "Rewrite react-native to react-native-css",
4531
visitor: {

‎src/babel/this-module.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { basename, dirname, resolve, sep } from "node:path";
2+
3+
// Anchored on the layout segment, since this runs from src/babel under the source condition and from dist/<format>/babel once built
4+
const packageRoot = resolve(
5+
__dirname,
6+
basename(dirname(__dirname)) === "src" ? "../.." : "../../..",
7+
);
8+
const thisModuleDist = resolve(packageRoot, "dist") + sep;
9+
const thisModuleSrc = resolve(packageRoot, "src") + sep;
10+
11+
/** Whether `filename` is one of this package's own modules, which import the libraries it rewrites */
12+
export function isFromThisModule(filename: string): boolean {
13+
return (
14+
filename.startsWith(thisModuleDist) || filename.startsWith(thisModuleSrc)
15+
);
16+
}

‎src/metro/resolver.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { basename, dirname, join, resolve, sep } from "node:path";
1+
import { basename, sep } from "node:path";
22

33
import type {
44
CustomResolutionContext,
@@ -7,20 +7,7 @@ import type {
77
} from "metro-resolver";
88

99
import { allowedModules } from "../babel/allowedModules";
10-
11-
// Anchored on the layout segment, since this runs from src/metro under the source condition and from dist/<format>/metro once built
12-
const packageRoot = resolve(
13-
__dirname,
14-
basename(dirname(__dirname)) === "src" ? "../.." : "../../..",
15-
);
16-
const thisModuleDist = join(packageRoot, "dist");
17-
const thisModuleSrc = join(packageRoot, "src");
18-
19-
function isFromThisModule(filename: string): boolean {
20-
return (
21-
filename.startsWith(thisModuleDist) || filename.startsWith(thisModuleSrc)
22-
);
23-
}
10+
import { isFromThisModule } from "../babel/this-module";
2411

2512
export function nativeResolver(
2613
resolver: CustomResolver,

0 commit comments

Comments
 (0)