Skip to content

Commit

Permalink
feat: add support for *.nitro.tsx extension
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkabwe committed Jan 25, 2025
1 parent 1b97dc7 commit cd99907
Show file tree
Hide file tree
Showing 20 changed files with 495 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/BUG_REPORT.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ body:
label: Reproduceable Code
description: >
Share a small reproduceable code snippet here (or the entire file if necessary).
Most importantly, share your native code (your HybridObjects), and ideally also the .nitro.ts specs, as well as the generated protocols/interfaces.
Most importantly, share your native code (your HybridObjects), and ideally also the .nitro.{ts,tsx} specs, as well as the generated protocols/interfaces.
render: tsx
placeholder: >
class HybridMath: HybridMathSpec {
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/NITROGEN_ERROR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ body:
required: true
- type: textarea
attributes:
label: Nitro Specs (`.nitro.ts`)
description: Share your Nitro specs (`.nitro.ts` files), ideally stripped down to the core problem so I can reproduce this myself locally.
label: Nitro Specs (`.nitro.{ts,tsx}`)
description: Share your Nitro specs (`.nitro.{ts,tsx}` files), ideally stripped down to the core problem so I can reproduce this myself locally.
render: ts
placeholder: >
interface Math extends HybridObject {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/configuration-nitro-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Here, the Hybrid Object "`Math`" is autolinked to create an instance of `HybridM
## `ignorePaths`
Configures the TypeScript parser to ignore specific given paths when looking for `*.nitro.ts` specs.
Configures the TypeScript parser to ignore specific given paths when looking for `*.nitro.{ts,tsx}` specs.
By default, this is empty (`[]`), but it can be set to ignore paths like `["node_modules", "lib"]`.
Expand Down
2 changes: 1 addition & 1 deletion docs/static/nitro.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"items": {
"type": "string"
},
"description": "A list of paths relative to the project directory that should be ignored by nitrogen. Nitrogen will not look for `.nitro.ts` files in these directories."
"description": "A list of paths relative to the project directory that should be ignored by nitrogen. Nitrogen will not look for `.nitro.{ts,tsx}` files in these directories."
},
"createGitAttributes": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/config/NitroUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const NitroUserConfigSchema = z.object({
),
/**
* A list of paths relative to the project directory that should be ignored by nitrogen.
* Nitrogen will not look for `.nitro.ts` files in these directories.
* Nitrogen will not look for `.nitro.{ts,tsx}` files in these directories.
*/
ignorePaths: z.array(z.string()).optional(),
/**
Expand Down
6 changes: 3 additions & 3 deletions packages/nitrogen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ await yargs(hideBin(process.argv))
.command(
'$0 [basePath]',
`Usage: ${chalk.bold(`${commandName} <basePath> [options]`)}\n` +
`Run the nitro code-generator on all ${chalk.underline('**/*.nitro.ts')} files found ` +
`Run the nitro code-generator on all ${chalk.underline('**/*.nitro.{ts,tsx}')} files found ` +
`in the current directory and generate C++, Swift or Kotlin outputs in ${chalk.underline('./nitrogen/generated')}.`,
(y) =>
y
.positional('basePath', {
type: 'string',
description: `The base path of where Nitrogen will start looking for ${chalk.underline('**/*.nitro.ts')} specs.`,
description: `The base path of where Nitrogen will start looking for ${chalk.underline('**/*.nitro.{ts,tsx}')} specs.`,
default: process.cwd(),
})
.option('out', {
Expand Down Expand Up @@ -83,7 +83,7 @@ await yargs(hideBin(process.argv))
.usage(
`Usage: ${chalk.bold('$0 [options]')}\n` +
`$0 is a code-generater for Nitro Modules (${chalk.underline('https://github.com/mrousavy/nitro')})\n` +
`It converts all TypeScript specs found in ${chalk.underline('**/*.nitro.ts')} to C++, Swift or Kotlin specs.\n` +
`It converts all TypeScript specs found in ${chalk.underline('**/*.nitro.{ts,tsx}')} to C++, Swift or Kotlin specs.\n` +
`Each library/module must have a ${chalk.underline('nitro.json')} configuration file in it's root directory.`
)
.help()
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function initNewNitroModule(
`👉 To install dependencies, use your package manager of choice (e.g. ${chalk.bold('npm install')})`
)
Logger.info(
`👉 To create your first Hybrid Object, add a ${chalk.underline(`*.nitro.ts`)} file, declare a TypeScript interface that extends ${chalk.bold('HybridObject<...>')}, and run ${chalk.bold('npx nitro-codegen')}.`
`👉 To create your first Hybrid Object, add a ${chalk.underline(`*.nitro.{ts,tsx}`)} file, declare a TypeScript interface that extends ${chalk.bold('HybridObject<...>')}, and run ${chalk.bold('npx nitro-codegen')}.`
)
Logger.info(
`👉 To test your module in an app, create a new React Native app somewhere (e.g. in ${chalk.underline(`./${moduleName}/example`)}) and add ${chalk.bold(moduleName)} as a local dependency.`
Expand Down
6 changes: 3 additions & 3 deletions packages/nitrogen/src/nitrogen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function runNitrogen({
})

const ignorePaths = NitroConfig.getIgnorePaths()
const globPattern = [path.join(baseDirectory, '**', '*.nitro.ts')]
const globPattern = [path.join(baseDirectory, '**', '*.nitro.{ts,tsx}')]
ignorePaths.forEach((ignorePath) => {
globPattern.push('!' + path.join(baseDirectory, ignorePath))
})
Expand All @@ -77,11 +77,11 @@ export async function runNitrogen({
// If no source files are found, we can exit
if (project.getSourceFiles().length === 0) {
const searchDir = prettifyDirectory(
path.join(path.resolve(baseDirectory), '**', '*.nitro.ts')
path.join(path.resolve(baseDirectory), '**', '*.nitro.{ts,tsx}')
)
Logger.error(
`❌ Nitrogen didn't find any spec files in ${chalk.underline(searchDir)}! ` +
`To create a Nitro Module, create a TypeScript file with the "${chalk.underline('.nitro.ts')}" suffix ` +
`To create a Nitro Module, create a TypeScript file with the "${chalk.underline('.nitro.{ts,tsx}')}" suffix ` +
'and export an interface that extends HybridObject<T>.'
)
process.exit()
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-nitro-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Nitro Image also contains a `HybridTestObject`, which can be used for playing ar

## Contributing

When any of the HybridObjects specs (`*.nitro.ts`) change, you must re-run nitrogen:
When any of the HybridObjects specs (`*.nitro.{ts,tsx}`) change, you must re-run nitrogen:

```ts
npx nitro-codegen
Expand All @@ -42,7 +42,7 @@ npx nitro-codegen
- [`ios/`](ios): All your iOS-specific implementations. (`HybridImage.swift` and `HybridImageFactory.swift`)
- [`nitrogen/`](nitrogen): All files generated by nitrogen. You should commit this folder to git.
- [`src/`](src): The TypeScript codebase. This defines all HybridObjects and loads them at runtime.
- [`specs/`](src/specs): All HybridObject types. Nitrogen will run on all `*.nitro.ts` files.
- [`specs/`](src/specs): All HybridObject types. Nitrogen will run on all `*.nitro.{ts,tsx}` files.
- [`nitro.json`](nitro.json): The configuration file for nitrogen. This will define all native namespaces, as well as the library name.
- [`NitroImage.podspec`](NitroImage.podspec): The iOS podspec build file to build the iOS code. This contains three important pieces:
1. Specifies the Pod's name. This must be identical to the name specified in `nitro.json`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
///
/// JHybridTestObjectTSXSpec.cpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#include "JHybridTestObjectTSXSpec.hpp"





namespace margelo::nitro::image {

jni::local_ref<JHybridTestObjectTSXSpec::jhybriddata> JHybridTestObjectTSXSpec::initHybrid(jni::alias_ref<jhybridobject> jThis) {
return makeCxxInstance(jThis);
}

void JHybridTestObjectTSXSpec::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JHybridTestObjectTSXSpec::initHybrid),
});
}

size_t JHybridTestObjectTSXSpec::getExternalMemorySize() noexcept {
static const auto method = _javaPart->getClass()->getMethod<jlong()>("getMemorySize");
return method(_javaPart);
}

// Properties
double JHybridTestObjectTSXSpec::getTestValue() {
static const auto method = _javaPart->getClass()->getMethod<double()>("getTestValue");
auto __result = method(_javaPart);
return __result;
}

// Methods


} // namespace margelo::nitro::image
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
///
/// HybridTestObjectTSXSpec.hpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#pragma once

#include <NitroModules/JHybridObject.hpp>
#include <fbjni/fbjni.h>
#include "HybridTestObjectTSXSpec.hpp"




namespace margelo::nitro::image {

using namespace facebook;

class JHybridTestObjectTSXSpec: public jni::HybridClass<JHybridTestObjectTSXSpec, JHybridObject>,
public virtual HybridTestObjectTSXSpec {
public:
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/image/HybridTestObjectTSXSpec;";
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis);
static void registerNatives();

protected:
// C++ constructor (called from Java via `initHybrid()`)
explicit JHybridTestObjectTSXSpec(jni::alias_ref<jhybridobject> jThis) :
HybridObject(HybridTestObjectTSXSpec::TAG),
_javaPart(jni::make_global(jThis)) {}

public:
~JHybridTestObjectTSXSpec() override {
// Hermes GC can destroy JS objects on a non-JNI Thread.
jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); });
}

public:
size_t getExternalMemorySize() noexcept override;

public:
inline const jni::global_ref<JHybridTestObjectTSXSpec::javaobject>& getJavaPart() const noexcept {
return _javaPart;
}

public:
// Properties
double getTestValue() override;

public:
// Methods


private:
friend HybridBase;
using HybridBase::HybridBase;
jni::global_ref<JHybridTestObjectTSXSpec::javaobject> _javaPart;
};

} // namespace margelo::nitro::image
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
///
/// HybridTestObjectTSXSpec.kt
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

package com.margelo.nitro.image

import androidx.annotation.Keep
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.*

/**
* A Kotlin class representing the TestObjectTSX HybridObject.
* Implement this abstract class to create Kotlin-based instances of TestObjectTSX.
*/
@DoNotStrip
@Keep
@Suppress(
"KotlinJniMissingFunction", "unused",
"RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet",
"LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName"
)
abstract class HybridTestObjectTSXSpec: HybridObject() {
@DoNotStrip
private var mHybridData: HybridData = initHybrid()

init {
super.updateNative(mHybridData)
}

override fun updateNative(hybridData: HybridData) {
mHybridData = hybridData
super.updateNative(hybridData)
}

// Properties
@get:DoNotStrip
@get:Keep
abstract val testValue: Double

// Methods


private external fun initHybrid(): HybridData

companion object {
private const val TAG = "HybridTestObjectTSXSpec"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
///
/// HybridTestObjectTSXSpecSwift.cpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#include "HybridTestObjectTSXSpecSwift.hpp"

namespace margelo::nitro::image {
} // namespace margelo::nitro::image
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
///
/// HybridTestObjectTSXSpecSwift.hpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#pragma once

#include "HybridTestObjectTSXSpec.hpp"

// Forward declaration of `HybridTestObjectTSXSpec_cxx` to properly resolve imports.
namespace NitroImage { class HybridTestObjectTSXSpec_cxx; }





#include "NitroImage-Swift-Cxx-Umbrella.hpp"

namespace margelo::nitro::image {

/**
* The C++ part of HybridTestObjectTSXSpec_cxx.swift.
*
* HybridTestObjectTSXSpecSwift (C++) accesses HybridTestObjectTSXSpec_cxx (Swift), and might
* contain some additional bridging code for C++ <> Swift interop.
*
* Since this obviously introduces an overhead, I hope at some point in
* the future, HybridTestObjectTSXSpec_cxx can directly inherit from the C++ class HybridTestObjectTSXSpec
* to simplify the whole structure and memory management.
*/
class HybridTestObjectTSXSpecSwift: public virtual HybridTestObjectTSXSpec {
public:
// Constructor from a Swift instance
explicit HybridTestObjectTSXSpecSwift(const NitroImage::HybridTestObjectTSXSpec_cxx& swiftPart):
HybridObject(HybridTestObjectTSXSpec::TAG),
_swiftPart(swiftPart) { }

public:
// Get the Swift part
inline NitroImage::HybridTestObjectTSXSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
inline size_t getExternalMemorySize() noexcept override {
return _swiftPart.getMemorySize();
}

public:
// Properties
inline double getTestValue() noexcept override {
return _swiftPart.getTestValue();
}

public:
// Methods


private:
NitroImage::HybridTestObjectTSXSpec_cxx _swiftPart;
};

} // namespace margelo::nitro::image
Loading

0 comments on commit cd99907

Please sign in to comment.