Skip to content

Commit c962a1f

Browse files
committed
Strip "lib" prefix from prebuild directory names
1 parent c512bf6 commit c962a1f

File tree

12 files changed

+102
-58
lines changed

12 files changed

+102
-58
lines changed

packages/ferric/src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
determineXCFrameworkFilename,
1515
createXCframework,
1616
createUniversalAppleLibrary,
17-
determineLibraryFilename,
17+
determineLibraryBasename,
1818
prettyPath,
1919
} from "react-native-node-api-modules";
2020

@@ -146,7 +146,7 @@ export const buildCommand = new Command("build")
146146
}
147147
);
148148

149-
const libraryName = determineLibraryFilename([
149+
const libraryName = determineLibraryBasename([
150150
...androidLibraries.map(([, outputPath]) => outputPath),
151151
]);
152152

packages/ferric/src/cargo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function getWeakNodeApiFrameworkPath(target: AppleTargetName) {
128128
assert(fs.existsSync(WEAK_NODE_API_PATH), "Expected weak-node-api to exist");
129129
return joinPathAndAssertExistence(
130130
WEAK_NODE_API_PATH,
131-
"libweak-node-api.xcframework",
131+
"weak-node-api.xcframework",
132132
APPLE_XCFRAMEWORK_CHILDS_PER_TARGET[target]
133133
);
134134
}
@@ -137,7 +137,7 @@ export function getWeakNodeApiAndroidLibraryPath(target: AndroidTargetName) {
137137
assert(fs.existsSync(WEAK_NODE_API_PATH), "Expected weak-node-api to exist");
138138
return joinPathAndAssertExistence(
139139
WEAK_NODE_API_PATH,
140-
"libweak-node-api.android.node",
140+
"weak-node-api.android.node",
141141
ANDROID_ARCH_PR_TARGET[target]
142142
);
143143
}
@@ -205,7 +205,7 @@ export function getTargetEnvironmentVariables({
205205
} else if (isAppleTarget(target)) {
206206
const weakNodeApiFrameworkPath = getWeakNodeApiFrameworkPath(target);
207207
return {
208-
RUSTFLAGS: `-L framework=${weakNodeApiFrameworkPath} -l framework=libweak-node-api`,
208+
RUSTFLAGS: `-L framework=${weakNodeApiFrameworkPath} -l framework=weak-node-api`,
209209
};
210210
} else {
211211
throw new Error(`Unexpected target: ${target}`);

packages/react-native-node-api-cmake/src/weak-node-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function getWeakNodeApiPath(triplet: SupportedTriplet): string {
1717
);
1818
assert(fs.existsSync(pathname), "Weak Node API path does not exist");
1919
if (isAppleTriplet(triplet)) {
20-
const xcframeworkPath = path.join(pathname, "libweak-node-api.xcframework");
20+
const xcframeworkPath = path.join(pathname, "weak-node-api.xcframework");
2121
assert(
2222
fs.existsSync(xcframeworkPath),
2323
`Expected an XCFramework at ${xcframeworkPath}`
@@ -26,7 +26,7 @@ export function getWeakNodeApiPath(triplet: SupportedTriplet): string {
2626
} else if (isAndroidTriplet(triplet)) {
2727
const libraryPath = path.join(
2828
pathname,
29-
"libweak-node-api.android.node",
29+
"weak-node-api.android.node",
3030
ANDROID_ARCHITECTURES[triplet],
3131
"libweak-node-api.so"
3232
);

packages/react-native-node-api-modules/android/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ find_package(hermes-engine REQUIRED CONFIG)
88

99
add_library(weak-node-api SHARED IMPORTED)
1010
set_target_properties(weak-node-api PROPERTIES
11-
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/../weak-node-api/libweak-node-api.android.node/${ANDROID_ABI}/libweak-node-api.so"
11+
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/../weak-node-api/weak-node-api.android.node/${ANDROID_ABI}/libweak-node-api.so"
1212
)
1313
target_include_directories(weak-node-api INTERFACE
1414
../weak-node-api

packages/react-native-node-api-modules/react-native-node-api-modules.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Pod::Spec.new do |s|
3232
s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{hpp,cpp,c,h}", "weak-node-api/include/*.h", "weak-node-api/*.hpp"
3333
s.public_header_files = "weak-node-api/include/*.h"
3434

35-
s.vendored_frameworks = "auto-linked/apple/*.xcframework", "weak-node-api/libweak-node-api.xcframework"
35+
s.vendored_frameworks = "auto-linked/apple/*.xcframework", "weak-node-api/weak-node-api.xcframework"
3636
s.script_phase = {
3737
:name => 'Copy Node-API xcframeworks',
3838
:execution_position => :before_compile,

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ export function replaceWithRequireNodeAddon(
3030
modulePath: string,
3131
naming: NamingStrategy
3232
) {
33-
const requireCallArgument = getLibraryName(
34-
replaceWithNodeExtension(modulePath),
35-
naming
36-
);
33+
const requireCallArgument = getLibraryName(modulePath, naming);
3734
p.replaceWith(
3835
t.callExpression(
3936
t.memberExpression(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ export {
2121
determineXCFrameworkFilename,
2222
} from "./prebuilds/apple.js";
2323

24-
export { determineLibraryFilename, prettyPath } from "./path-utils.js";
24+
export { determineLibraryBasename, prettyPath } from "./path-utils.js";

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

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ describe("isNodeApiModule", () => {
4949
// remove read permission on file
5050
fs.chmodSync(candidate, 0);
5151
try {
52-
assert.throws(() => isNodeApiModule(path.join(tempDirectoryPath, "addon")), /Found an unreadable module addon\.android\.node/);
52+
assert.throws(
53+
() => isNodeApiModule(path.join(tempDirectoryPath, "addon")),
54+
/Found an unreadable module addon\.android\.node/
55+
);
5356
} finally {
5457
fs.chmodSync(candidate, 0o600);
5558
}
@@ -81,7 +84,10 @@ describe("isNodeApiModule", () => {
8184
const unreadable = path.join(tempDirectoryPath, "addon.android.node");
8285
// only android module is unreadable
8386
fs.chmodSync(unreadable, 0);
84-
assert.throws(() => isNodeApiModule(path.join(tempDirectoryPath, "addon")), /Found an unreadable module addon\.android\.node/);
87+
assert.throws(
88+
() => isNodeApiModule(path.join(tempDirectoryPath, "addon")),
89+
/Found an unreadable module addon\.android\.node/
90+
);
8591
fs.chmodSync(unreadable, 0o600);
8692
});
8793
});
@@ -108,12 +114,9 @@ describe("replaceExtensionWithNode", () => {
108114
});
109115

110116
describe("determineModuleContext", () => {
111-
it("works", (context) => {
117+
it("strips the file extension", (context) => {
112118
const tempDirectoryPath = setupTempDirectory(context, {
113119
"package.json": `{ "name": "my-package" }`,
114-
// Two sub-packages with the same name
115-
"sub-package-a/package.json": `{ "name": "my-sub-package" }`,
116-
"sub-package-b/package.json": `{ "name": "my-sub-package" }`,
117120
});
118121

119122
{
@@ -123,20 +126,43 @@ describe("determineModuleContext", () => {
123126
assert.equal(packageName, "my-package");
124127
assert.equal(relativePath, "some-dir/some-file");
125128
}
129+
});
130+
131+
it("strips a lib prefix", (context) => {
132+
const tempDirectoryPath = setupTempDirectory(context, {
133+
"package.json": `{ "name": "my-package" }`,
134+
});
135+
136+
{
137+
const { packageName, relativePath } = determineModuleContext(
138+
path.join(tempDirectoryPath, "some-dir/libsome-file.node")
139+
);
140+
assert.equal(packageName, "my-package");
141+
assert.equal(relativePath, "some-dir/some-file");
142+
}
143+
});
144+
145+
it("resolves the correct package name", (context) => {
146+
const tempDirectoryPath = setupTempDirectory(context, {
147+
"package.json": `{ "name": "root-package" }`,
148+
// Two sub-packages with the same name
149+
"sub-package-a/package.json": `{ "name": "my-sub-package-a" }`,
150+
"sub-package-b/package.json": `{ "name": "my-sub-package-b" }`,
151+
});
126152

127153
{
128154
const { packageName, relativePath } = determineModuleContext(
129155
path.join(tempDirectoryPath, "sub-package-a/some-file.node")
130156
);
131-
assert.equal(packageName, "my-sub-package");
157+
assert.equal(packageName, "my-sub-package-a");
132158
assert.equal(relativePath, "some-file");
133159
}
134160

135161
{
136162
const { packageName, relativePath } = determineModuleContext(
137163
path.join(tempDirectoryPath, "sub-package-b/some-file.node")
138164
);
139-
assert.equal(packageName, "my-sub-package");
165+
assert.equal(packageName, "my-sub-package-b");
140166
assert.equal(relativePath, "some-file");
141167
}
142168
});
@@ -295,22 +321,30 @@ describe("determineModuleContext", () => {
295321
it("should read package.json only once across multiple module paths for the same package", (context) => {
296322
const tempDir = setupTempDirectory(context, {
297323
"package.json": `{ "name": "cached-pkg" }`,
298-
"subdir1/file1.node": "",
324+
"subdir1/file1.node": "",
299325
"subdir2/file2.node": "",
300-
"subdir1/file1.xcframework": ""
326+
"subdir1/file1.xcframework": "",
301327
});
302328
let readCount = 0;
303329
const orig = fs.readFileSync;
304-
context.mock.method(fs, "readFileSync", (...args: Parameters<typeof fs.readFileSync>) => {
305-
const [pathArg] = args;
306-
if (typeof pathArg === "string" && pathArg.endsWith("package.json")) {
307-
readCount++;
330+
context.mock.method(
331+
fs,
332+
"readFileSync",
333+
(...args: Parameters<typeof fs.readFileSync>) => {
334+
const [pathArg] = args;
335+
if (typeof pathArg === "string" && pathArg.endsWith("package.json")) {
336+
readCount++;
337+
}
338+
return orig(...args);
308339
}
309-
return orig(...args);
310-
});
340+
);
311341

312-
const ctx1 = determineModuleContext(path.join(tempDir, "subdir1/file1.node"));
313-
const ctx2 = determineModuleContext(path.join(tempDir, "subdir2/file2.node"));
342+
const ctx1 = determineModuleContext(
343+
path.join(tempDir, "subdir1/file1.node")
344+
);
345+
const ctx2 = determineModuleContext(
346+
path.join(tempDir, "subdir2/file2.node")
347+
);
314348
assert.equal(ctx1.packageName, "cached-pkg");
315349
assert.equal(ctx2.packageName, "cached-pkg");
316350
assert.equal(readCount, 1);

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function isNodeApiModule(modulePath: string): boolean {
4141
// Cannot read directory: treat as no module
4242
return false;
4343
}
44-
return Object.values(PLATFORM_EXTENSIONS).some(extension => {
44+
return Object.values(PLATFORM_EXTENSIONS).some((extension) => {
4545
const fileName = baseName + extension;
4646
if (!entries.includes(fileName)) {
4747
return false;
@@ -103,25 +103,32 @@ export function determineModuleContext(
103103
let pkgName = packageNameCache.get(pkgDir);
104104
if (!pkgName) {
105105
const pkg = readPackageSync({ cwd: pkgDir });
106-
assert(typeof pkg.name === "string", "Expected package.json to have a name");
106+
assert(
107+
typeof pkg.name === "string",
108+
"Expected package.json to have a name"
109+
);
107110
pkgName = pkg.name;
108111
packageNameCache.set(pkgDir, pkgName);
109112
}
110113
// Compute module-relative path
111-
const relPath = normalizeModulePath(
112-
path.relative(pkgDir, originalPath)
113-
);
114+
const relPath = normalizeModulePath(path.relative(pkgDir, originalPath));
114115
return { packageName: pkgName, relativePath: relPath };
115116
}
116117

117118
export function normalizeModulePath(modulePath: string) {
118-
return path.normalize(stripExtension(modulePath));
119+
const dirname = path.normalize(path.dirname(modulePath));
120+
const basename = path.basename(modulePath);
121+
const strippedBasename = stripExtension(basename).replace(/^lib/, "");
122+
return path.join(dirname, strippedBasename);
119123
}
120124

121125
export function escapePath(modulePath: string) {
122126
return modulePath.replace(/[^a-zA-Z0-9]/g, "-");
123127
}
124128

129+
/**
130+
* Get the name of the library which will be used when the module is linked in.
131+
*/
125132
export function getLibraryName(modulePath: string, naming: NamingStrategy) {
126133
const { packageName, relativePath } = determineModuleContext(modulePath);
127134
return naming.stripPathSuffix
@@ -252,19 +259,21 @@ export function findNodeApiModulePaths(
252259
return [];
253260
}
254261
const candidatePath = path.join(fromPath, suffix);
255-
return fs.readdirSync(candidatePath, { withFileTypes: true }).flatMap((file) => {
256-
if (
257-
file.isFile() &&
258-
file.name === MAGIC_FILENAME &&
259-
hasPlatformExtension(platform, candidatePath)
260-
) {
261-
return [candidatePath];
262-
} else if (file.isDirectory()) {
263-
// Traverse into the child directory
264-
return findNodeApiModulePaths(options, path.join(suffix, file.name));
265-
}
266-
return [];
267-
});
262+
return fs
263+
.readdirSync(candidatePath, { withFileTypes: true })
264+
.flatMap((file) => {
265+
if (
266+
file.isFile() &&
267+
file.name === MAGIC_FILENAME &&
268+
hasPlatformExtension(platform, candidatePath)
269+
) {
270+
return [candidatePath];
271+
} else if (file.isDirectory()) {
272+
// Traverse into the child directory
273+
return findNodeApiModulePaths(options, path.join(suffix, file.name));
274+
}
275+
return [];
276+
});
268277
}
269278

270279
/**
@@ -308,10 +317,10 @@ export function findNodeApiModulePathsByDependency({
308317
}
309318

310319
/**
311-
* Determine the library filename based on the library paths.
312-
* Ensuring that all framework paths have the same base name.
320+
* Determine the library basename (no file extension nor "lib" prefix) based on the library paths.
321+
* Errors if all framework paths doesn't produce the same basename.
313322
*/
314-
export function determineLibraryFilename(libraryPaths: string[]) {
323+
export function determineLibraryBasename(libraryPaths: string[]) {
315324
const libraryNames = libraryPaths.map((p) =>
316325
// Strip the "lib" prefix and any file extension
317326
path.basename(p, path.extname(p)).replace(/^lib/, "")

packages/react-native-node-api-modules/src/node/prebuilds/android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs";
33
import path from "node:path";
44

55
import { AndroidTriplet } from "./triplets.js";
6-
import { determineLibraryFilename } from "../path-utils.js";
6+
import { determineLibraryBasename } from "../path-utils.js";
77

88
export const DEFAULT_ANDROID_TRIPLETS = [
99
"aarch64-linux-android",
@@ -26,7 +26,7 @@ export const ANDROID_ARCHITECTURES = {
2626
* Ensuring that all framework paths have the same base name.
2727
*/
2828
export function determineAndroidLibsFilename(libraryPaths: string[]) {
29-
const libraryName = determineLibraryFilename(libraryPaths);
29+
const libraryName = determineLibraryBasename(libraryPaths);
3030
return `${libraryName}.android.node`;
3131
}
3232

0 commit comments

Comments
 (0)