Skip to content

Commit 6725125

Browse files
author
Mariusz Pasinski
committed
refactor: extract findNodeAddonForBindings()
1 parent 0a106e5 commit 6725125

File tree

1 file changed

+13
-10
lines changed
  • packages/react-native-node-api-modules/src/node/babel-plugin

1 file changed

+13
-10
lines changed

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ const nodeBindingsSubdirs = [
6363
"./Debug",
6464
];
6565
function findNodeAddonForBindings(id: string, fromDir: string) {
66-
66+
const idWithExt = id.endsWith(".node") ? id : `${id}.node`;
67+
// Support traversing the filesystem to find the Node-API module.
68+
// Currently, we check the most common directories like `bindings` does.
69+
for (const subdir of nodeBindingsSubdirs) {
70+
const resolvedPath = path.join(fromDir, subdir, idWithExt);
71+
if (isNodeApiModule(resolvedPath)) {
72+
return resolvedPath;
73+
}
74+
}
75+
return undefined;
6776
}
6877

6978
export function replaceWithRequireNodeAddon3(
@@ -120,15 +129,9 @@ export function plugin(): PluginObj {
120129
const [argument] = p.parent.arguments;
121130
if (argument.type === "StringLiteral") {
122131
const id = argument.value;
123-
const idWithExt = id.endsWith(".node") ? id : `${id}.node`;
124-
// Support traversing the filesystem to find the Node-API module.
125-
// Currently, we check the most common directories from `bindings`.
126-
for (const subdir of nodeBindingsSubdirs) {
127-
const resolvedPath = path.join(from, subdir, idWithExt);
128-
if (isNodeApiModule(resolvedPath)) {
129-
replaceWithRequireNodeAddon3(p.parentPath, resolvedPath, this.filename);
130-
break;
131-
}
132+
const resolvedPath = findNodeAddonForBindings(id, from);
133+
if (resolvedPath !== undefined) {
134+
replaceWithRequireNodeAddon3(p.parentPath, resolvedPath, this.filename);
132135
}
133136
}
134137
} else {

0 commit comments

Comments
 (0)