Skip to content

Commit 0220d80

Browse files
committed
refactor entrypoints
1 parent 29523d2 commit 0220d80

File tree

7 files changed

+72
-61
lines changed

7 files changed

+72
-61
lines changed

packages/ios/index.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
// react-native-node-api/babel-plugin will rewrite this path to:
2-
// module.exports = require("react-native-node-api").requireNodeAddon("-nativescript-ios-node-api—-NativeScript");
3-
// ... And some other build-time tooling in react-native-node-api searches this
4-
// package for `*.node` files to point `requireNodeAddon()` to the true path.
5-
//
6-
// This is why we've added react-native-node-api as a peer dependency. We've
7-
// marked it as optional, however, because other hosts may choose not to use
8-
// that Babel transform.
9-
module.exports = require("./build/Release/NativeScript.apple.node");
1+
if (typeof interop === "undefined") {
2+
// deno-lint-ignore no-process-globals
3+
if (process) {
4+
// ===
5+
// If we're in a Node-like environment (e.g. Node.js for Mobile)
6+
// ===
107

11-
// Out of interest, we could alternatively write the path as follows:
12-
// module.exports = require("bindings")("NativeScript");
13-
//
14-
// react-native-node-api/babel-plugin would recognise and rewrite it all the
15-
// same. That approach might make sense if we were also supporting Node.js with
16-
// this package (as in the case of Node.js, you'd omit the Babel transform and
17-
// you would actually depend on the "bindings" package), but unless we one day
18-
// merge the iOS and macOS packages into one, it feels better to reduce the
19-
// amount of magic involved.
8+
const path =
9+
"./build/RelWithDebInfo/NativeScript.apple.node/ios-arm64/NativeScript.framework/NativeScript";
10+
11+
let metaURL = import.meta.url;
12+
if (!metaURL.includes("://")) {
13+
metaURL = "file://" + metaURL;
14+
}
15+
16+
const module = { exports: {} };
17+
18+
// deno-lint-ignore no-process-globals
19+
process.dlopen(module, new URL(path, metaURL).pathname);
20+
21+
module.exports.init(
22+
// deno-lint-ignore no-process-globals
23+
process.env.METADATA_PATH
24+
);
25+
} else {
26+
// ===
27+
// If we're in a React Native-like environment
28+
// ===
29+
30+
// react-native-node-api/babel-plugin will rewrite this to:
31+
// module.exports = require("react-native-node-api").requireNodeAddon("-nativescript-macos-node-api—-NativeScript");
32+
module.exports = require("./build/RelWithDebInfo/NativeScript.apple.node");
33+
}
34+
}

packages/ios/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"typings": "index.d.ts",
1111
"type": "module",
1212
"files": [
13-
"build/Release/NativeScript.apple.node",
13+
"build/RelWithDebInfo/NativeScript.apple.node",
1414
"index.d.ts",
1515
"types"
1616
],

packages/macos/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!build

packages/macos/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/// <reference path="./types/index.d.ts" />
22

33
import "@nativescript/objc-node-api";
4-
5-
export * from "./lib/native.js";

packages/macos/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
export * from "./lib/native.js";
1+
if (typeof interop === "undefined") {
2+
// deno-lint-ignore no-process-globals
3+
if (process) {
4+
// ===
5+
// If we're in a Node-like environment (e.g. Node.js, Deno, or Bun)
6+
// ===
7+
8+
const path =
9+
"./build/RelWithDebInfo/NativeScript.apple.node/macos-arm64_x86_64/NativeScript.framework/Versions/0.1.0/NativeScript";
10+
11+
let metaURL = import.meta.url;
12+
if (!metaURL.includes("://")) {
13+
metaURL = "file://" + metaURL;
14+
}
15+
16+
const module = { exports: {} };
17+
18+
// deno-lint-ignore no-process-globals
19+
process.dlopen(module, new URL(path, metaURL).pathname);
20+
21+
module.exports.init(
22+
// deno-lint-ignore no-process-globals
23+
process.env.METADATA_PATH
24+
);
25+
} else {
26+
// ===
27+
// If we're in a React Native-like environment
28+
// ===
29+
30+
// react-native-node-api/babel-plugin will rewrite this to:
31+
// module.exports = require("react-native-node-api").requireNodeAddon("-nativescript-macos-node-api—-NativeScript");
32+
module.exports = require("./build/RelWithDebInfo/NativeScript.apple.node");
33+
}
34+
}

packages/macos/lib/native.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

packages/macos/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
"description": "An embeddable, engine-agnostic NativeScript runtime for macOS based on Node-API",
55
"repository": {
66
"type": "git",
7-
"url": "https://github.com/NativeScript/runtime-node-api.git",
7+
"url": "https://github.com/NativeScript/napi-ios.git",
88
"directory": "packages/macos"
99
},
1010
"main": "index.js",
1111
"typings": "index.d.ts",
1212
"type": "module",
1313
"files": [
14-
"dist/macos/NativeScript.node",
14+
"build/RelWithDebInfo/NativeScript.apple.node",
1515
"index.d.ts",
16-
"lib",
1716
"types"
1817
],
1918
"scripts": {
@@ -51,4 +50,4 @@
5150
"devDependencies": {
5251
"rimraf": "^6.0.0"
5352
}
54-
}
53+
}

0 commit comments

Comments
 (0)