diff --git a/docs/docs/getting-started/configuration-nitro-json.md b/docs/docs/getting-started/configuration-nitro-json.md index 4ed9639656..9129682a3c 100644 --- a/docs/docs/getting-started/configuration-nitro-json.md +++ b/docs/docs/getting-started/configuration-nitro-json.md @@ -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$$" @@ -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: diff --git a/docs/static/nitro.schema.json b/docs/static/nitro.schema.json index 6fa69e40df..7aab05cd14 100644 --- a/docs/static/nitro.schema.json +++ b/docs/static/nitro.schema.json @@ -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": { diff --git a/packages/nitrogen/src/config/NitroConfig.ts b/packages/nitrogen/src/config/NitroConfig.ts index 8019a6d028..cd8e5a7d50 100644 --- a/packages/nitrogen/src/config/NitroConfig.ts +++ b/packages/nitrogen/src/config/NitroConfig.ts @@ -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(...)`. diff --git a/packages/nitrogen/src/config/NitroUserConfig.ts b/packages/nitrogen/src/config/NitroUserConfig.ts index 902b7b20e0..1b69d5578a 100644 --- a/packages/nitrogen/src/config/NitroUserConfig.ts +++ b/packages/nitrogen/src/config/NitroUserConfig.ts @@ -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. * @@ -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(), }) /** diff --git a/packages/nitrogen/src/config/getConfig.ts b/packages/nitrogen/src/config/getConfig.ts index efe4f5679c..3734d2e0b0 100644 --- a/packages/nitrogen/src/config/getConfig.ts +++ b/packages/nitrogen/src/config/getConfig.ts @@ -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.` diff --git a/packages/nitrogen/src/createHybridClassRuntime.ts b/packages/nitrogen/src/createHybridClassRuntime.ts index ab920fc9e1..0d0968cd06 100644 --- a/packages/nitrogen/src/createHybridClassRuntime.ts +++ b/packages/nitrogen/src/createHybridClassRuntime.ts @@ -175,7 +175,7 @@ export {} const jsCode = ` ${createFileMetadataString(jsFileName)} -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('${NitroConfig.current.getRuntimePackageName()}') ${cacheComment} ${cacheDeclarationsCode.join('\n')} diff --git a/packages/template/nitro.json b/packages/template/nitro.json index c9df39b3a3..28074c9592 100644 --- a/packages/template/nitro.json +++ b/packages/template/nitro.json @@ -1,5 +1,6 @@ { "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "react-native", "cxxNamespace": ["$$cxxNamespace$$"], "ios": { "iosModuleName": "$$iosModuleName$$" diff --git a/scripts/releases/check-consumer.mjs b/scripts/releases/check-consumer.mjs index 4d7e1b46b0..119e940ad6 100644 --- a/scripts/releases/check-consumer.mjs +++ b/scripts/releases/check-consumer.mjs @@ -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: { @@ -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); } @@ -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}`); diff --git a/tests/react-native/packages/react-native-nitro-test-external/nitro.json b/tests/react-native/packages/react-native-nitro-test-external/nitro.json index 9a9f21bde7..6d68280560 100644 --- a/tests/react-native/packages/react-native-nitro-test-external/nitro.json +++ b/tests/react-native/packages/react-native-nitro-test-external/nitro.json @@ -1,5 +1,6 @@ { "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "react-native", "cxxNamespace": [ "test", "external" diff --git a/tests/react-native/packages/react-native-nitro-test-external/nitrogen/generated/shared/javascript/TestView.cjs b/tests/react-native/packages/react-native-nitro-test-external/nitrogen/generated/shared/javascript/TestView.cjs index 46d4e72d3b..d16a928393 100644 --- a/tests/react-native/packages/react-native-nitro-test-external/nitrogen/generated/shared/javascript/TestView.cjs +++ b/tests/react-native/packages/react-native-nitro-test-external/nitrogen/generated/shared/javascript/TestView.cjs @@ -5,7 +5,7 @@ /// Copyright © Marc Rousavy @ Margelo /// -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('react-native-nitro-modules') // Copies of these getters, e.g. from Babel's interop helpers, share the caches. let hybridClass diff --git a/tests/react-native/packages/react-native-nitro-test/nitro.json b/tests/react-native/packages/react-native-nitro-test/nitro.json index 49de44095d..40548e0bcc 100644 --- a/tests/react-native/packages/react-native-nitro-test/nitro.json +++ b/tests/react-native/packages/react-native-nitro-test/nitro.json @@ -1,5 +1,6 @@ { "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "react-native", "cxxNamespace": [ "test" ], diff --git a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectCpp.cjs b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectCpp.cjs index 054ced2c4e..765fd600bb 100644 --- a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectCpp.cjs +++ b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectCpp.cjs @@ -5,7 +5,7 @@ /// Copyright © Marc Rousavy @ Margelo /// -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('react-native-nitro-modules') // Copies of this getter, e.g. from Babel's interop helpers, share the cached class. let hybridClass diff --git a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectSwiftKotlin.cjs b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectSwiftKotlin.cjs index 0fcf0bda5b..988b3a5a5e 100644 --- a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectSwiftKotlin.cjs +++ b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/BenchmarkObjectSwiftKotlin.cjs @@ -5,7 +5,7 @@ /// Copyright © Marc Rousavy @ Margelo /// -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('react-native-nitro-modules') // Copies of this getter, e.g. from Babel's interop helpers, share the cached class. let hybridClass diff --git a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/RecyclableTestView.cjs b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/RecyclableTestView.cjs index 7baefab8c1..e96af40df5 100644 --- a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/RecyclableTestView.cjs +++ b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/RecyclableTestView.cjs @@ -5,7 +5,7 @@ /// Copyright © Marc Rousavy @ Margelo /// -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('react-native-nitro-modules') // Copies of these getters, e.g. from Babel's interop helpers, share the caches. let hybridClass diff --git a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/TestView.cjs b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/TestView.cjs index 6e5de33c4f..dd4c358862 100644 --- a/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/TestView.cjs +++ b/tests/react-native/packages/react-native-nitro-test/nitrogen/generated/shared/javascript/TestView.cjs @@ -5,7 +5,7 @@ /// Copyright © Marc Rousavy @ Margelo /// -const { NitroModules } = require('nitro-core') +const { NitroModules } = require('react-native-nitro-modules') // Copies of these getters, e.g. from Babel's interop helpers, share the caches. let hybridClass diff --git a/tests/standalone/nitro-test-external/nitro.json b/tests/standalone/nitro-test-external/nitro.json index a6c30b64a3..14d07905f4 100644 --- a/tests/standalone/nitro-test-external/nitro.json +++ b/tests/standalone/nitro-test-external/nitro.json @@ -1,5 +1,6 @@ { "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "bare", "cxxNamespace": [ "test", "external" diff --git a/tests/standalone/nitro-test/nitro.json b/tests/standalone/nitro-test/nitro.json index c447bfce2a..3d04ecc903 100644 --- a/tests/standalone/nitro-test/nitro.json +++ b/tests/standalone/nitro-test/nitro.json @@ -1,5 +1,6 @@ { "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "bare", "cxxNamespace": [ "test" ],