Skip to content

Refactor requireNodeAddon to take the package name and extension-less path of an addon #222

@kraenhansen

Description

@kraenhansen

This is a suggestion which is subtly different from the one proposed in #91.

Goals

  • Removing the requirement for the Babel plugin, for use-cases where a library author "controls" the code calling into the host package.
  • Give more control to the host package when deciding what library files to loads (at runtime).
  • Supporting multiple platforms (including Node.js) with source-code calling into the host package with the same call arguments

Example library

Imagine a library ("my-lib") with a single index.js entrypoint calling into our host package:

// node_modules/my-lib/index.js
import { requireNodeAddon } from "react-native-node-api";
const addon = requireNodeAddon({
  packageName: "my-lib", // The name from the package.json
  path: "build/Release/hello" // Path to the addon, relative to the package root, stripped from its ".*.node" file extensions.
});
export const sum = addon.sum;

and the following directory structure:

index.js
index.d.ts
build/Release
├── hello.android.node
│   ├── arm64-v8a
│   │   └── libhello.so
│   ├── armeabi-v7a
│   │   └── libhello.so
│   ├── react-native-node-api-module
│   ├── x86
│   │   └── libhello.so
│   └── x86_64
│       └── libhello.so
├── hello.apple.node
│   ├── Info.plist
│   ├── ios-arm64
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   ├── ios-arm64-simulator
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   ├── macos-arm64_x86_64
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   ├── react-native-node-api-module
│   ├── tvos-arm64
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   ├── tvos-arm64-simulator
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   ├── xros-arm64
│   │   └── hello.framework
│   │       ├── Headers
│   │       ├── hello
│   │       └── Info.plist
│   └── xros-arm64-simulator
│       └── hello.framework
│           ├── Headers
│           ├── hello
│           └── Info.plist
└── hello.nodejs.node
    ├── darwin-arm64
    │   └── hello.node
    └── react-native-node-api-module

Metadata

Metadata

Assignees

No one assigned

    Labels

    Host 🏡Our `react-native-node-api-modules` package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions