Skip to content

Commit 2d4f74f

Browse files
committed
fix: verify native prebuild fixtures
1 parent 48fa7fc commit 2d4f74f

6 files changed

Lines changed: 43 additions & 2 deletions

File tree

.changeset/calm-bears-resolve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-native-node-api": patch
3+
---
4+
5+
Preserve Node.js module resolution precedence when a JavaScript file and native addon share a basename.

packages/host/src/node/babel-plugin/plugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe("plugin", () => {
129129
itTransforms("and does not touch required JS files", {
130130
files: {
131131
"package.json": `{ "name": "my-package" }`,
132-
// TODO: Add a ./my-addon.node to make this test complete
132+
"my-addon.node": "// This is supposed to be a binary file",
133133
"my-addon.js": "// Some JS file",
134134
"index.js": `
135135
const addon = require('./my-addon');

packages/host/src/node/babel-plugin/plugin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from "node:assert/strict";
2+
import { createRequire } from "node:module";
23
import path from "node:path";
34

45
import type { PluginObj, NodePath } from "@babel/core";
@@ -101,6 +102,7 @@ export function plugin(): PluginObj {
101102
}
102103
} else if (
103104
!path.isAbsolute(id) &&
105+
!resolvesToNonNodeModule(id, this.filename) &&
104106
isNodeApiModule(path.join(from, id))
105107
) {
106108
const relativePath = path.join(from, id);
@@ -114,3 +116,11 @@ export function plugin(): PluginObj {
114116
},
115117
};
116118
}
119+
120+
function resolvesToNonNodeModule(id: string, filename: string): boolean {
121+
try {
122+
return !createRequire(filename).resolve(id).endsWith(".node");
123+
} catch {
124+
return false;
125+
}
126+
}

packages/node-addon-examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"bootstrap": "node --run copy-and-build"
3030
},
3131
"devDependencies": {
32+
"@expo/plist": "0.4.7",
3233
"cmake-rn": "workspace:*",
3334
"node-addon-examples": "github:nodejs/node-addon-examples#4b7dd86a85644610e6de80154df9acac9329b509",
3435
"gyp-to-cmake": "workspace:*",

packages/node-addon-examples/scripts/verify-prebuilds.mts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import fs from "node:fs";
22
import assert from "node:assert/strict";
33
import path from "node:path";
44

5+
import plistModule from "@expo/plist";
6+
57
import { EXAMPLES_DIR } from "./cmake-projects.mjs";
68

79
const EXPECTED_ANDROID_ARCHS = ["armeabi-v7a", "arm64-v8a", "x86_64", "x86"];
@@ -65,7 +67,27 @@ async function verifyApplePrebuild(dirent: fs.Dirent) {
6567
"Expected only directory and files in framework",
6668
);
6769
if (file.name === "Info.plist") {
68-
// TODO: Verify the contents of the Info.plist file
70+
const libraryName = path.basename(frameworkDir, ".framework");
71+
const infoPlist: unknown = plistModule.default.parse(
72+
await fs.promises.readFile(
73+
path.join(frameworkDir, file.name),
74+
"utf8",
75+
),
76+
);
77+
assert(
78+
typeof infoPlist === "object" && infoPlist !== null,
79+
"Expected Info.plist to contain a dictionary",
80+
);
81+
assert("CFBundleExecutable" in infoPlist);
82+
assert("CFBundleIdentifier" in infoPlist);
83+
assert.equal(infoPlist.CFBundleExecutable, libraryName);
84+
assert.equal(
85+
infoPlist.CFBundleIdentifier,
86+
`com.callstackincubator.node-api.${libraryName}`.replace(
87+
/[^A-Za-z0-9-.]/g,
88+
"-",
89+
),
90+
);
6991
continue;
7092
} else {
7193
assert(

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)