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/quiet-files-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-node-api": patch
---

Fix `require('./addon')` being rewritten to a native addon when `addon.js` or `addon.json` exists next to `addon.node`, matching Node.js resolution order.
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
7 changes: 7 additions & 0 deletions packages/host/src/node/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ const packageNameCache = new Map<string, string>();
* TODO: Consider checking for a specific platform extension.
*/
export function isNodeApiModule(modulePath: string): boolean {
// Node resolves `.js` and `.json` before `.node`
if (
!modulePath.endsWith(".node") &&
[".js", ".json"].some((extension) => fs.existsSync(modulePath + extension))
) {
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,11 +30,13 @@
"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:*",
"read-pkg": "^9.0.1",
"weak-node-api": "workspace:*"
"weak-node-api": "workspace:*",
"zod": "^4.1.11"
},
"dependencies": {
"assert": "^2.1.0",
Expand Down
27 changes: 25 additions & 2 deletions packages/node-addon-examples/scripts/verify-prebuilds.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import fs from "node:fs";
import assert from "node:assert/strict";
import path from "node:path";

import plist from "@expo/plist";
import * as z from "zod";

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

const EXPECTED_ANDROID_ARCHS = ["armeabi-v7a", "arm64-v8a", "x86_64", "x86"];
Expand Down Expand Up @@ -37,6 +40,27 @@ async function verifyAndroidPrebuild(dirent: fs.Dirent) {
}
}

const InfoPlistSchema = z.object({
CFBundleExecutable: z.string(),
CFBundleName: z.string(),
CFBundleIdentifier: z.string().regex(/^[A-Za-z0-9-.]+$/),
});

async function verifyInfoPlist(frameworkDir: string) {
const libraryName = path.basename(frameworkDir, ".framework");
const info = InfoPlistSchema.parse(
plist.default.parse(
await fs.promises.readFile(path.join(frameworkDir, "Info.plist"), "utf8"),
),
);
assert.equal(info.CFBundleExecutable, libraryName);
assert.equal(info.CFBundleName, libraryName);
assert(
fs.existsSync(path.join(frameworkDir, libraryName)),
`Missing executable ${libraryName} in ${frameworkDir}`,
);
}

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 +89,7 @@ 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;
await verifyInfoPlist(frameworkDir);
} else {
assert(
!file.name.endsWith(".node"),
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

Loading