-
Notifications
You must be signed in to change notification settings - Fork 3
Update docs on usage #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+59
−21
Merged
Update docs on usage #216
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,16 +17,15 @@ The app developer has to install both `calculator-lib` as well as `react-native- | |||||||||||||
The reason for the latter is a current limitation of the React Native Community CLI which doesn't consider transitive dependencies when enumerating packages for auto-linking. | ||||||||||||||
|
||||||||||||||
> [!WARNING] | ||||||||||||||
> It's important to match the exact version of the `react-native-node-api` declared as peer dependency by `calculator-lib`. | ||||||||||||||
> It's important to match the version range of the `react-native-node-api` declared as a peer dependency by `calculator-lib`. | ||||||||||||||
|
||||||||||||||
For the app to resolve the Node-API dynamic library files, the app developer must update their Metro config to use a `resolveRequest` function exported from `react-native-node-api`: | ||||||||||||||
For the app to resolve the Node-API dynamic library files, the app developer must update their Babel config to use a `requireNodeAddon` function exported from `react-native-node-api`: | ||||||||||||||
|
||||||||||||||
```javascript | ||||||||||||||
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config"); | ||||||||||||||
const nodeApi = require("react-native-node-api/metro-config"); | ||||||||||||||
module.exports = mergeConfig(getDefaultConfig(__dirname), { | ||||||||||||||
resolver: { resolveRequest: nodeApi.resolveRequest }, | ||||||||||||||
}); | ||||||||||||||
module.exports = { | ||||||||||||||
presets: ["module:@react-native/babel-preset"], | ||||||||||||||
plugins: ["module:react-native-node-api/babel-plugin"], // 👈 This needs to be added to the babel.config.js of the app | ||||||||||||||
}; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
At some point the app code will import (or require) the entrypoint of `calculator-lib`: | ||||||||||||||
|
@@ -121,18 +120,36 @@ NAPI_MODULE_INIT(/* napi_env env, napi_value exports */) { | |||||||||||||
} | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### Build the prebuilt binaries | ||||||||||||||
```cmake | ||||||||||||||
# CMakeLists.txt | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
npx react-native-node-api build ./addon.c | ||||||||||||||
cmake_minimum_required(VERSION 3.15...3.31) | ||||||||||||||
project(addon) | ||||||||||||||
|
||||||||||||||
add_compile_definitions(-DNAPI_VERSION=4) | ||||||||||||||
|
||||||||||||||
file(GLOB SOURCE_FILES "addon.c") | ||||||||||||||
|
||||||||||||||
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC}) | ||||||||||||||
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") | ||||||||||||||
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC}) | ||||||||||||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB}) | ||||||||||||||
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) | ||||||||||||||
|
||||||||||||||
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) | ||||||||||||||
# Generate node.lib | ||||||||||||||
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) | ||||||||||||||
endif() | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
This is a shorthand command which generates a CMake project from the single source-file and prebuilds for both the Apple and Android platforms. See the [CLI documentation](./CLI.md) for more information on the options available and [documentation on prebuilds](./PREBUILDS.md) for the specifics on their format and structure. | ||||||||||||||
### Build the prebuilt binaries | ||||||||||||||
|
||||||||||||||
<!-- TODO: Add a listing of the files produced when running command: Some temp (cached) CMakeList.txt, the CMake project dir, 2x platform specific prebuild directories --> | ||||||||||||||
``` | ||||||||||||||
npx cmake-rn | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### Load and export the native module | ||||||||||||||
|
||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path './build/Release/addon.node' should be documented to explain that this is the output path from the cmake-rn build process, as it may not be immediately clear to developers where this path comes from.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||
```javascript | ||||||||||||||
module.exports = require("./prebuild.node"); | ||||||||||||||
module.exports = require("./build/Release/addon.node"); | ||||||||||||||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding an example of what the printed path looks like to help developers understand the expected output format.
/path/to/your/project/node_modules/.cache/react-native-node-api/hermes-android-0.73.3
Copilot uses AI. Check for mistakes.