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.
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
10 changes: 10 additions & 0 deletions packages/host/src/node/babel-plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert/strict";
import { createRequire } from "node:module";
import path from "node:path";

import type { PluginObj, NodePath } from "@babel/core";
Expand Down Expand Up @@ -101,6 +102,7 @@ export function plugin(): PluginObj {
}
} else if (
!path.isAbsolute(id) &&
!resolvesToNonNodeModule(id, this.filename) &&
isNodeApiModule(path.join(from, id))
) {
const relativePath = path.join(from, id);
Expand All @@ -114,3 +116,11 @@ export function plugin(): PluginObj {
},
};
}

function resolvesToNonNodeModule(id: string, filename: string): boolean {
try {
return !createRequire(filename).resolve(id).endsWith(".node");
} catch {
return false;
}
}
1 change: 1 addition & 0 deletions packages/node-addon-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 Down
24 changes: 23 additions & 1 deletion packages/node-addon-examples/scripts/verify-prebuilds.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from "node:fs";
import assert from "node:assert/strict";
import path from "node:path";

import plistModule from "@expo/plist";

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

const EXPECTED_ANDROID_ARCHS = ["armeabi-v7a", "arm64-v8a", "x86_64", "x86"];
Expand Down Expand Up @@ -65,7 +67,27 @@ 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
const libraryName = path.basename(frameworkDir, ".framework");
const infoPlist: unknown = plistModule.default.parse(
await fs.promises.readFile(
path.join(frameworkDir, file.name),
"utf8",
),
);
assert(
typeof infoPlist === "object" && infoPlist !== null,
"Expected Info.plist to contain a dictionary",
);
assert("CFBundleExecutable" in infoPlist);
assert("CFBundleIdentifier" in infoPlist);
assert.equal(infoPlist.CFBundleExecutable, libraryName);
assert.equal(
infoPlist.CFBundleIdentifier,
`com.callstackincubator.node-api.${libraryName}`.replace(
/[^A-Za-z0-9-.]/g,
"-",
),
);
continue;
} else {
assert(
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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