Skip to content

Commit 4cc7b78

Browse files
committed
implement separate CJS and MJS entrypoints (v0.4.2)
1 parent 5268749 commit 4cc7b78

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

packages/macos/index.cjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const isNativeScriptRuntime =
2+
typeof globalThis.interop !== "undefined" ||
3+
typeof globalThis.NSObject !== "undefined";
4+
5+
if (!isNativeScriptRuntime) {
6+
// deno-lint-ignore no-process-globals
7+
if (typeof process !== "undefined" && typeof process.dlopen === "function") {
8+
// ===
9+
// If we're in a Node-like environment (e.g. Node.js, Deno, or Bun)
10+
// ===
11+
12+
const module = { exports: {} };
13+
process.dlopen(
14+
module,
15+
require("node:path").resolve(
16+
__dirname,
17+
"./build/RelWithDebInfo/NativeScript.apple.node/macos-arm64/NativeScript.framework/Versions/A/NativeScript",
18+
),
19+
);
20+
module.exports.init(process.env.METADATA_PATH);
21+
} else if (typeof require !== "undefined") {
22+
// ===
23+
// If we're in a React Native-like environment
24+
// ===
25+
26+
// react-native-node-api/babel-plugin will rewrite this to:
27+
// module.exports = require("react-native-node-api").requireNodeAddon("-nativescript-macos-node-api—-NativeScript");
28+
module.exports = require("./build/RelWithDebInfo/NativeScript.apple.node");
29+
}
30+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ if (!isNativeScriptRuntime) {
2424

2525
module.exports.init(
2626
// deno-lint-ignore no-process-globals
27-
process.env.METADATA_PATH
27+
process.env.METADATA_PATH,
2828
);
2929
} else if (typeof require !== "undefined") {
3030
// ===
3131
// If we're in a React Native-like environment
3232
// ===
3333

34+
// Hermes doesn't support MJS, so fall back to CJS. Really, the bundler
35+
// shouldn't have resolved this file in the first place.
36+
3437
// react-native-node-api/babel-plugin will rewrite this to:
3538
// module.exports = require("react-native-node-api").requireNodeAddon("-nativescript-macos-node-api—-NativeScript");
3639
module.exports = require("./build/RelWithDebInfo/NativeScript.apple.node");

packages/macos/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/macos-node-api",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "An embeddable, engine-agnostic NativeScript runtime for macOS based on Node-API",
55
"repository": {
66
"type": "git",
@@ -49,5 +49,14 @@
4949
},
5050
"devDependencies": {
5151
"rimraf": "^6.0.0"
52+
},
53+
"exports": {
54+
".": {
55+
"types": "./index.d.ts",
56+
"import": "./index.mjs",
57+
"require": "./index.cjs"
58+
},
59+
"./package.json": "./package.json",
60+
"./*": "./*"
5261
}
5362
}

0 commit comments

Comments
 (0)