Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-bears-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-node-api": patch
---

Preserve Node.js module resolution precedence when a JavaScript file and native addon share a basename: `require('./foo')` no longer gets rewritten to load a Node-API addon when a same-named `foo.js`/`.cjs`/`.mjs`/`.json` file exists alongside it, since that source file is what `require()` actually resolves to. An explicit `require('./foo.node')` is unaffected.
2 changes: 1 addition & 1 deletion packages/host/src/node/babel-plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe("plugin", () => {
itTransforms("and does not touch required JS files", {
files: {
"package.json": `{ "name": "my-package" }`,
// TODO: Add a ./my-addon.node to make this test complete
"my-addon.node": "// This is supposed to be a binary file",
"my-addon.js": "// Some JS file",
"index.js": `
const addon = require('./my-addon');
Expand Down
1 change: 1 addition & 0 deletions packages/host/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
createXCframework,
createUniversalAppleLibrary,
determineXCFrameworkFilename,
escapeBundleIdentifier,
} from "./prebuilds/apple.js";

export {
Expand Down
12 changes: 12 additions & 0 deletions packages/host/src/node/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ export type NamingStrategy = {
// Cache mapping package directory to package name across calls
const packageNameCache = new Map<string, string>();

// Extensions Node's own require() resolves before ever trying `.node`.
const COLLIDING_SOURCE_EXTENSIONS = [".js", ".cjs", ".mjs", ".json"];

/**
* @param modulePath Batch-scans the path to the module to check (must be extensionless or end in .node)
* @returns True if a platform specific prebuild exists for the module path, warns on unreadable modules.
* @throws If the parent directory cannot be read, or if a detected module is unreadable.
* TODO: Consider checking for a specific platform extension.
*/
export function isNodeApiModule(modulePath: string): boolean {
if (
!modulePath.endsWith(".node") &&
COLLIDING_SOURCE_EXTENSIONS.some((extension) =>
fs.existsSync(modulePath + extension),
)
) {
// An explicit require('./foo.node') has no such ambiguity to defer to.
return false;
}
{
// HACK: Take a shortcut (if applicable): existing `.node` files are addons
try {
Expand Down
4 changes: 3 additions & 1 deletion packages/node-addon-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"bootstrap": "node --run copy-and-build"
},
"devDependencies": {
"@expo/plist": "0.4.7",
"cmake-rn": "workspace:*",
"node-addon-examples": "github:nodejs/node-addon-examples#4b7dd86a85644610e6de80154df9acac9329b509",
"gyp-to-cmake": "workspace:*",
Expand All @@ -38,6 +39,7 @@
},
"dependencies": {
"assert": "^2.1.0",
"react-native-node-api": "workspace:*"
"react-native-node-api": "workspace:*",
"zod": "^4.1.11"
}
}
42 changes: 40 additions & 2 deletions packages/node-addon-examples/scripts/verify-prebuilds.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ import fs from "node:fs";
import assert from "node:assert/strict";
import path from "node:path";

import plistModule from "@expo/plist";
import { escapeBundleIdentifier } from "react-native-node-api";
import { z } from "zod";

import { DIRS } from "./cmake-projects.mjs";

// @expo/plist is CJS with an `export default`; under this genuine ESM
// (.mts) module's interop, the default import binds to the whole
// `module.exports`, nesting the real API one `.default` deeper.
const plist = plistModule.default;

const FrameworkInfoPlistSchema = z.object({
CFBundleExecutable: z.string(),
CFBundleIdentifier: z.string(),
});

const EXPECTED_ANDROID_ARCHS = ["armeabi-v7a", "arm64-v8a", "x86_64", "x86"];

const EXPECTED_XCFRAMEWORK_PLATFORMS = [
Expand Down Expand Up @@ -37,6 +51,27 @@ async function verifyAndroidPrebuild(dirent: fs.Dirent) {
}
}

async function verifyFrameworkInfoPlist(
infoPlistPath: string,
libraryName: string,
) {
const contents = await fs.promises.readFile(infoPlistPath, "utf8");
const parsed = FrameworkInfoPlistSchema.parse(plist.parse(contents));
assert.equal(
parsed.CFBundleExecutable,
libraryName,
`Unexpected CFBundleExecutable in ${infoPlistPath}`,
);
assert.equal(
parsed.CFBundleIdentifier,
// Mirrors the default writeFrameworkInfoPlist derives in
// packages/host/src/node/prebuilds/apple.ts, since none of the
// examples pass --apple-bundle-identifier.
escapeBundleIdentifier(`com.callstackincubator.node-api.${libraryName}`),
`Unexpected CFBundleIdentifier in ${infoPlistPath}`,
);
}

async function verifyApplePrebuild(dirent: fs.Dirent) {
console.log("Verifying Apple prebuild", dirent.name, "in", dirent.parentPath);
for (const arch of EXPECTED_XCFRAMEWORK_PLATFORMS) {
Expand Down Expand Up @@ -65,8 +100,11 @@ async function verifyApplePrebuild(dirent: fs.Dirent) {
"Expected only directory and files in framework",
);
if (file.name === "Info.plist") {
// TODO: Verify the contents of the Info.plist file
continue;
const libraryName = path.basename(frameworkDir, ".framework");
await verifyFrameworkInfoPlist(
path.join(frameworkDir, file.name),
libraryName,
);
} else {
assert(
!file.name.endsWith(".node"),
Expand Down
26 changes: 8 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading