Skip to content

Commit 5dd3481

Browse files
committed
Move "install name" logic into TurboModule
1 parent 698609c commit 5dd3481

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

packages/react-native-node-api-modules/cpp/CxxNodeApiHostModule.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
2323
return jsi::Value::undefined();
2424
}
2525

26-
jsi::Value CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
27-
const jsi::String path) {
28-
const std::string pathStr = path.utf8(rt);
26+
jsi::Value
27+
CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
28+
const jsi::String libraryName) {
29+
const std::string libraryNameStr = libraryName.utf8(rt);
2930

30-
auto [it, inserted] = nodeAddons_.emplace(pathStr, NodeAddon());
31+
auto [it, inserted] = nodeAddons_.emplace(libraryNameStr, NodeAddon());
3132
NodeAddon &addon = it->second;
3233

3334
// Check if this module has been loaded already, if not then load it...
3435
if (inserted) {
35-
if (!loadNodeAddon(addon, pathStr)) {
36+
if (!loadNodeAddon(addon, libraryNameStr)) {
3637
return jsi::Value::undefined();
3738
}
3839
}
@@ -47,10 +48,19 @@ jsi::Value CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
4748
}
4849

4950
bool CxxNodeApiHostModule::loadNodeAddon(NodeAddon &addon,
50-
const std::string &path) const {
51+
const std::string &libraryName) const {
52+
#if defined(__APPLE__)
53+
std::string libraryPath =
54+
"@rpath/" + libraryName + ".framework/" + libraryName;
55+
#elif defined(__ANDROID__)
56+
std::string libraryPath = libraryName
57+
#else
58+
abort()
59+
#endif
60+
5161
typename LoaderPolicy::Symbol initFn = NULL;
5262
typename LoaderPolicy::Module library =
53-
LoaderPolicy::loadLibrary(path.c_str());
63+
LoaderPolicy::loadLibrary(libraryPath.c_str());
5464
if (NULL != library) {
5565
addon.moduleHandle = library;
5666

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { transformFileSync } from "@babel/core";
66

77
import { plugin } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
9-
import { getLibraryInstallName } from "../path-utils.js";
9+
import { getLibraryName } from "../path-utils.js";
1010

1111
describe("plugin", () => {
1212
it("transforms require calls, regardless", (context) => {
@@ -38,11 +38,11 @@ describe("plugin", () => {
3838
`,
3939
});
4040

41-
const ADDON_1_REQUIRE_ARG = getLibraryInstallName(
41+
const ADDON_1_REQUIRE_ARG = getLibraryName(
4242
path.join(tempDirectoryPath, "addon-1"),
4343
{ stripPathSuffix: false }
4444
);
45-
const ADDON_2_REQUIRE_ARG = getLibraryInstallName(
45+
const ADDON_2_REQUIRE_ARG = getLibraryName(
4646
path.join(tempDirectoryPath, "addon-2"),
4747
{ stripPathSuffix: false }
4848
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { PluginObj, NodePath } from "@babel/core";
55
import * as t from "@babel/types";
66

77
import {
8-
getLibraryInstallName,
8+
getLibraryName,
99
isNodeApiModule,
1010
replaceWithNodeExtension,
1111
NamingStrategy,
@@ -30,7 +30,7 @@ export function replaceWithRequireNodeAddon(
3030
modulePath: string,
3131
naming: NamingStrategy
3232
) {
33-
const requireCallArgument = getLibraryInstallName(
33+
const requireCallArgument = getLibraryName(
3434
replaceWithNodeExtension(modulePath),
3535
naming
3636
);

packages/react-native-node-api-modules/src/node/path-utils.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,3 @@ export function getLibraryName(modulePath: string, naming: NamingStrategy) {
114114
? packageName
115115
: `${packageName}--${escapePath(relativePath)}`;
116116
}
117-
118-
export function getLibraryInstallName(
119-
modulePath: string,
120-
naming: NamingStrategy
121-
) {
122-
const libraryName = getLibraryName(modulePath, naming);
123-
return `@rpath/${libraryName}.framework/${libraryName}`;
124-
}

packages/react-native-node-api-modules/src/react-native/NativeNodeApiHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TurboModule } from "react-native";
22
import { TurboModuleRegistry } from "react-native";
33

44
export interface Spec extends TurboModule {
5-
requireNodeAddon(path: string): void;
5+
requireNodeAddon(libraryName: string): void;
66
}
77

88
export default TurboModuleRegistry.getEnforcing<Spec>("NodeApiHost");

0 commit comments

Comments
 (0)