Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/docs/getting-started/configuration-nitro-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description: Configure nitro.json for a React Native Nitro Module, including C++
```json
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "react-native",
"cxxNamespace": ["$$cxxNamespace$$"],
"ios": {
"iosModuleName": "$$iosModuleName$$"
Expand All @@ -25,6 +26,15 @@ description: Configure nitro.json for a React Native Nitro Module, including C++

Nitrogen parses this file with Zod, see [`NitroUserConfig.ts`](https://github.com/margelo/nitro/blob/main/packages/nitrogen/src/config/NitroUserConfig.ts) for more information.

## `environment`

Selects the package that generated JavaScript code imports Nitro APIs from:

- `"react-native"` (default): imports from `react-native-nitro-modules`, which installs Nitro in the React Native runtime.
- `"bare"`: imports from `nitro-core` for standalone runtimes.

This setting only changes the JavaScript imports. Native code generation is the same for both environments.

## `cxxNamespace`

The `cxxNamespace` is the C++ namespace that all C++ specs will be generated in. It is always relative to `margelo::nitro`, and can also have multiple sub-namespaces:
Expand Down
9 changes: 9 additions & 0 deletions docs/static/nitro.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"autolinking"
],
"properties": {
"environment": {
"type": "string",
"enum": [
"react-native",
"bare"
],
"default": "react-native",
"description": "Selects the package imported by generated JavaScript runtime modules: react-native-nitro-modules for react-native, or nitro-core for bare."
},
"cxxNamespace": {
"type": "array",
"items": {
Expand Down
7 changes: 7 additions & 0 deletions packages/nitrogen/src/config/NitroConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export class NitroConfig {
return this.singleton
}

/** Returns the package to import Nitro's JavaScript runtime from. */
getRuntimePackageName(): string {
return this.config.environment === 'react-native'
? 'react-native-nitro-modules'
: 'nitro-core'
}

/**
* Returns the name of the Android C++ library (aka name in CMakeLists.txt `add_library(..)`).
* This will be loaded via `System.loadLibrary(...)`.
Expand Down
14 changes: 11 additions & 3 deletions packages/nitrogen/src/config/NitroUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const isReservedKeywordError = {
}

export const NitroUserConfigSchema = z.object({
/**
* Selects the package imported by generated JavaScript runtime modules.
* React Native uses `react-native-nitro-modules`; bare uses `nitro-core`.
*/
environment: z.enum(['react-native', 'bare']).default('react-native'),
/**
* Represents the C++ namespace of the module that will be generated.
*
Expand Down Expand Up @@ -76,9 +81,12 @@ export const NitroUserConfigSchema = z.object({
*/
ignorePaths: z.array(z.string()).optional(),
/** Removed in favor of implementation and constructor declarations in .nitro.ts. */
autolinking: z.never({
error: 'autolinking has moved to .nitro.ts: declare implementation in each platform and a public constructor to enable new.',
}).optional(),
autolinking: z
.never({
error:
'autolinking has moved to .nitro.ts: declare implementation in each platform and a public constructor to enable new.',
})
.optional(),
})

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/config/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function parseConfig(json: string): NitroUserConfig {
const prop = propPathToString(i.path)
switch (i.code) {
case 'invalid_type':
return `\`${prop}\`: ${i.message}`
case 'invalid_value':
return `\`${prop}\`: ${i.message}`
case 'invalid_union':
case 'invalid_element':
return `\`${prop}\` is not a valid & safe string. It must only contain alphanumeric characters and must not start with a number.`
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/createHybridClassRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export {}
const jsCode = `
${createFileMetadataString(jsFileName)}

const { NitroModules } = require('nitro-core')
const { NitroModules } = require('${NitroConfig.current.getRuntimePackageName()}')

${cacheComment}
${cacheDeclarationsCode.join('\n')}
Expand Down
1 change: 1 addition & 0 deletions packages/template/nitro.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "react-native",
"cxxNamespace": ["$$cxxNamespace$$"],
"ios": {
"iosModuleName": "$$iosModuleName$$"
Expand Down
17 changes: 12 additions & 5 deletions scripts/releases/check-consumer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function checkConsumer(tarballs) {
include: ["*.ts", "*.mts"],
});
writeJSON(path.join(consumer, "nitro.json"), {
environment: "bare",
cxxNamespace: ["releasecheck"],
ios: { iosModuleName: "ReleaseCheck" },
android: {
Expand All @@ -156,6 +157,14 @@ export function checkConsumer(tarballs) {
);
if (!existsSync(header) || !readFileSync(header, "utf8").includes("add("))
throw new Error("Packed Nitrogen did not generate the consumer spec");
run(
"node",
[
"-e",
`${mockRuntime} if (typeof require('#nitro/Math').Math !== 'function') throw Error('Generated HybridObject class is missing')`,
],
{ cwd: consumer },
);
console.log(`Validated packed core packages in ${consumer}`);
checkReactNativeConsumer(packed, version);
}
Expand Down Expand Up @@ -232,21 +241,19 @@ function checkReactNativeConsumer(packed, version) {
),
);
writeJSON(path.join(consumer, "nitro.json"), config);
run("node", ["node_modules/typescript/bin/tsc", "--noEmit"], {
cwd: consumer,
});
run(
"node",
["node_modules/nitrogen/lib/index.js", "--log-level", "warning"],
{ cwd: consumer },
);
run("node", ["node_modules/typescript/bin/tsc", "--noEmit"], {
cwd: consumer,
});
for (const file of [
"shared/c++/views/HybridTestViewComponent.cpp",
"shared/c++/include/NitroTest/HybridBenchmarkObjectCppSpec.hpp",
"ios/swift/HybridBenchmarkObjectSwiftKotlinSpec.swift",
"android/kotlin/com/margelo/nitro/test/HybridBenchmarkObjectSwiftKotlinSpec.kt",
"ios/swift/HybridTestViewSpec.swift",
"android/kotlin/com/margelo/nitro/test/HybridTestViewSpec.kt",
]) {
if (!existsSync(path.join(consumer, "nitrogen/generated", file)))
throw new Error(`Packed Nitrogen did not generate ${file}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "react-native",
"cxxNamespace": [
"test",
"external"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "react-native",
"cxxNamespace": [
"test"
],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/standalone/nitro-test-external/nitro.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "bare",
"cxxNamespace": [
"test",
"external"
Expand Down
1 change: 1 addition & 0 deletions tests/standalone/nitro-test/nitro.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://nitro.margelo.com/nitro.schema.json",
"environment": "bare",
"cxxNamespace": [
"test"
],
Expand Down
Loading