Skip to content
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

fix: add the migration guide and nunjucks depreciation notes #1253

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions .changeset/depreciate-nunjucks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/generator": minor
---

Migration guide for nunjucks render engine to react render engine
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved
120 changes: 120 additions & 0 deletions apps/generator/docs/nunjucka-depreciate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
--
title: "Depreciation of nunjucks render engine"
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
weight: 170
---

# Migration Guide from nunjucks render engine to react render engine
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

## Introduction

AsyncAPI Generator is moving away from Nunjucks templates in favor of React templates. This guide will help you migrate your existing Nunjucks templates to React.
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

## Step-by-Step Migration Guide
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

### 1. Update package.json

Change your template configuration in `package.json`:
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"generator": {
"renderer": "react"
}
}
```

### 2. Install Dependencies

Install the necessary React dependencies:

```bash
npm install @asyncapi/generator-react-sdk
```

### 3. Basic Template Structure
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

Nunjucks:
```jsx
<h1>{{ asyncapi.info().title() }}</h1>
<p>{{ asyncapi.info().description() }}</p>
```

React:
```jsx
import { File } from '@asyncapi/generator-react-sdk';

export default function({ asyncapi }) {
return (
<File name="index.html">
<h1>{asyncapi.info().title()}</h1>
<p>{asyncapi.info().description()}</p>
</File>
);
}
```
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

### 4. Macros
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Replace macros with React components:

Nunjucks:
```jsx
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
{% macro renderChannel(channel) %}
<div class="channel">
<h3>{{ channel.address() }}</h3>
<p>{{ channel.description() }}</p>
</div>
{% endmacro %}

{{ renderChannel(someChannel) }}
```

React:
```jsx
// components/Channel.js
import { Text } from '@asyncapi/generator-react-sdk';

export function Channel({ channel }) {
return (
<Text>
<div className="channel">
<h3>{channel.address()}</h3>
<p>{channel.description()}</p>
</div>
</Text>
);
}

// Main template
import { File, Text } from '@asyncapi/generator-react-sdk';
import { Channel } from './components/Channel';

export default function({ asyncapi }) {
return (
<File name="channels.html">
<Text>
<h2>Channels</h2>
</Text>
{asyncapi.channels().map(channel => (
<Channel key={channel.address()} channel={channel} />
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
))}
</File>
);
}
```

### 5. File template

//TODO: we can add a link to Florence docs once it is merged
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

## Testing Your Migration
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

After migrating, test your template thoroughly:

1. Run the generator with your new React template
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
2. Compare the output with the previous Nunjucks template output
3. Check for any missing or incorrectly rendered content

## Conclusion

Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can read why we introduced react render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
Loading