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 14 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This is a Monorepo managed using [Turborepo](https://turbo.build/) and contains

![npm](https://img.shields.io/npm/v/@asyncapi/generator?style=for-the-badge) ![npm](https://img.shields.io/npm/dt/@asyncapi/generator?style=for-the-badge)

> warning: This package doesn't support AsyncAPI 1.x anymore. We recommend to upgrade to the latest AsyncAPI version using the [AsyncAPI converter](https://github.com/asyncapi/converter-js) (You can refer to [installation guide](/apps/generator//docs//installation-guide.md)). If you need to convert documents on the fly, you may use the [Node.js](https://github.com/asyncapi/converter-js) or [Go](https://github.com/asyncapi/converter-go) converters.
[!IMPORTANT]
Deprecation Notice: The nunjucks renderer engine is depreciated and will be removed in the future releases.We strongly recommend using the react renderer engine instead, you can find how to migrate from nunjucks to react in the [migration guide](apps/generator/docs/nunjucks-depreciate.md)
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

<!-- toc is generated with GitHub Actions do not remove toc markers -->

Expand Down
2 changes: 2 additions & 0 deletions apps/generator/docs/file-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ weight: 140

> **Note**: This section applies only to the Nunjucks render engine. For information on using the React render engine, refer to the [Generating files with the React render engine](#generating-files-with-the-react-render-engine) section below.
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

> **Note**: Nunjucks renderer engine will be depreciated in the future release and we strongly recommend using the react renderer engine instead.
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

It is possible to generate files for each specific object in your AsyncAPI documentation using the Nunjucks render engine. For example, you can specify a filename like `$$channel$$.js` to generate a file for each channel defined in your AsyncAPI. The following file-template names and extra variables are available:

- `$$channel$$`, within the template-file you have access to two variables [`channel`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channel) and [`channelName`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channels). Where the `channel` contains the current channel being rendered.
Expand Down
139 changes: 139 additions & 0 deletions apps/generator/docs/nunjucks-depreciate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
--
Copy link
Member

Choose a reason for hiding this comment

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

please rename file, the document focus not on deprecation but on migration

nunjucks-react-migration.md

title: "Migration guide from nunjucks render engine"
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved
weight: 170
---

## Migration Guide from nunjucks render engine to react render engine
Copy link
Member

Choose a reason for hiding this comment

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

please remove

Suggested change
## Migration Guide from nunjucks render engine to react render engine

@Florence-Njeri @Gmin2 below you can see a preview from website, how would this document render - we would have strange duplication.

Screenshot 2024-10-21 at 11 32 23


### Introduction
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
### Introduction

when you see other generator docs, we don't write "introduction" we just write it without heading

we can of course change it if you think it is wrong, but then it should be a followup PR that unifies approach in all generator docs


The 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. For a comprehensive understanding of why we introduced React as an alternative in 2021 and why we're now removing Nunjucks entirely, please read our article [React as a Generator Engine](https://www.asyncapi.com/blog/react-as-generator-engine). The principles discussed in this article still apply to our current transition.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The 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. For a comprehensive understanding of why we introduced React as an alternative in 2021 and why we're now removing Nunjucks entirely, please read our article [React as a Generator Engine](https://www.asyncapi.com/blog/react-as-generator-engine). The principles discussed in this article still apply to our current transition.
The generator is moving away from Nunjucks templates in favor of React templates. This guide will help you migrate your existing Nunjucks templates to React. For a comprehensive understanding of why we introduced React as an alternative in 2021 and why we're now removing Nunjucks entirely, please read our article [React as a Generator Engine](https://www.asyncapi.com/blog/react-as-generator-engine). The principles discussed in this article still apply to our current transition.


### Step-by-step migration guide
Copy link
Member

Choose a reason for hiding this comment

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

probably because of my previous comments you will have to do changes to heading sizes, and start this from "##" and update the rest as well


#### 1. Update package.json

Change your template configuration in `package.json`:

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

Once deprecation period is ended, and we remove default nunjucks engine, react will become default and this setting will no longer be needed to configure
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

#### 2. Install dependencies

Install the necessary React dependencies:

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

#### 3. File naming

In Nunjucks, the template's filename directly corresponds to the output file. For example, a template named index.html will generate an index.html file.

In React, the filename of the generated file is not controlled by the file itself, but rather by the File component. The React component itself can be named anything with a `.js` extension, but the output file is controlled by the `name` attribute of the File component:
Copy link
Member

Choose a reason for hiding this comment

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

I see : at the end of paragraph that indicates an example follows, but there is no example - I think it is needed

nunjucks: index.html react: index.jsorindex.html.jsoranything-you-want.js`


#### 4. Basic template structure

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

React:
```js
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>
);
}
```

#### 5. Macros and Partials

Replace macros with React components:

Nunjucks:
```js
{% macro renderChannel(channel) %}
<div class="channel">
<h3>{{ channel.address() }}</h3>
<p>{{ channel.description() }}</p>
</div>
{% endmacro %}

{{ renderChannel(someChannel) }}
```

React:
```js
// 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 channel={channel} />
))}
</File>
);
}
```

#### 6. File template

The detailed guide on file template can be found in [this](file-templates.md) guide.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The detailed guide on file template can be found in [this](file-templates.md) guide.
Check [detailed guide on file templates](file-templates.md) to learn what is the difference between templating multiple file output in Nunjucks and React.

links always should be descriptive for accessibility reasons - the screen reader would only read "this" to the reader, which basically means nothing for the reader with sight disabilities.

https://www.youtube.com/watch?v=AOkkLzNgXhU


Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#### 7. Models generation
If you have a template written with Nunjucks, it is almost certain that you have your own custom models/classes/types templates in place. Instead of migrating them to React render engine we strongly advise you to delegate models generation to AsyncAPI Modelina project. Learn more on [how to add models generation using Modelina](https://www.asyncapi.com/docs/tools/generator/model-generation).

totally new section that I think is important to have, to safe people time

### Additional Resources and Information

#### Template Examples
For a complete example of React features in use, please refer to the [AsyncAPI Template for Generator Templates](https://github.com/asyncapi/template-for-generator-templates). The `master` branch demonstrates all React features, while the `nunjucks` branch shows the old Nunjucks implementation. This comparison can be particularly helpful in understanding the differences and migration process.

#### Filters to Helpers
If you've been using Nunjucks filters placed in the `filters` directory, you can still use this functionality in React. However, they should be treated as normal functions that you import into your components. We recommend renaming the `filters` directory to `helpers` to better reflect their new usage in React.

#### Hooks Remain Unchanged
It's important to note that hooks remain unchanged in this migration process. Hooks are not related to the render engine functionality, so you can continue to use them as you have been.

### Testing your migration

After migrating, test your template thoroughly:

1. Run the generator using your new React template
2. Compare the output with the previous Nunjucks template output
3. Check for any missing or incorrectly rendered content

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
You might want to consider implementing snapshot tests for your template before starting migration. This will ease the review of changes in content rendered after render engine changes. Snapshot tests allow you to have tests that will persist expected output from Nunjucks template, and compare it with output produced after you do the migration. Check the [example of such snapshot integration test for our internal react template we use for development and testing](https://github.com/asyncapi/generator/blob/master/apps/generator/test/integration.test.js#L66).

### Conclusion

Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can learn more about why we introduced ther React render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can learn more about why we introduced ther React render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)
Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can learn more about why we introduced ther React render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)
Suggested change
Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can learn more about why we introduced ther React render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)
Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable code. You can learn more about why we introduced the React render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)

Copy link
Member

Choose a reason for hiding this comment

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

@Gmin2 when you apply this change, please also improve the list - as per my other comment about links

1 change: 1 addition & 0 deletions apps/generator/docs/nunjucks-render-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ weight: 120
---

[Nunjucks](https://mozilla.github.io/nunjucks) is the default render engine, however, we strongly recommend adopting the [React](#react) engine.
> **Note**: Nunjucks renderer engine will be depreciated in the future release and we strongly recommend using the react renderer engine instead.
Copy link
Member

Choose a reason for hiding this comment

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

maybe it should be warning @Florence-Njeri ? probably in few months we should even consider removing any nunjucks mention from the docs, other than migration guide - but that is for later

Copy link
Member

Choose a reason for hiding this comment

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

@Gmin2 make it clear that it is not that "nunjucks will be deprecated in the future releases", basically when people will see this text in docs, it means deprecation is in place already, and in future releases we will remove it


### Common assumptions

Expand Down
4 changes: 4 additions & 0 deletions apps/generator/lib/filtersRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const nunjucksFilters = require('@asyncapi/nunjucks-filters');

/**
* Registers all template filters.
* @deprecated since version 3.0
* @param {Object} nunjucks Nunjucks environment.
* @param {Object} templateConfig Template configuration.
* @param {String} templateDir Directory where template is located.
Expand All @@ -21,6 +22,7 @@ module.exports.registerFilters = async (nunjucks, templateConfig, templateDir, f

/**
* Registers the local template filters.
* @deprecated since version 3.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i have assumed this to be included in the next major release, is this fine @derberg ?

Copy link
Member

Choose a reason for hiding this comment

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

no it won't, so we cannot predict version of the release since it is deprecated

just write @deprecated without any specific note

Copy link
Member

Choose a reason for hiding this comment

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

if jsdocs allows it, please use a link, and text specifying that more info someone can find in https://www.asyncapi.com/docs/tools/generator/nunjucks-react-migration

* @private
* @param {Object} nunjucks Nunjucks environment.
* @param {String} templateDir Directory where template is located.
Expand Down Expand Up @@ -68,6 +70,7 @@ function registerLocalFilters(nunjucks, templateDir, filtersDir) {

/**
* Registers the additionally configured filters.
* @deprecated since version 3.0
* @private
* @param {Object} nunjucks Nunjucks environment.
* @param {String} templateDir Directory where template is located.
Expand Down Expand Up @@ -112,6 +115,7 @@ async function registerConfigFilters(nunjucks, templateDir, templateConfig) {

/**
* Add filter functions to Nunjucks environment. Only owned functions from the module are added.
* @deprecated since version 3.0
* @private
* @param {Object} nunjucks Nunjucks environment.
* @param {Object} filters Module with functions.
Expand Down
3 changes: 2 additions & 1 deletion apps/generator/lib/renderer/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const nunjucksExport = module.exports;

/**
* Configures Nunjucks templating system
*
* @deprecated since version 3.0
* @private
* @param {boolean} debug flag
* @param {string} templateDir path
Expand All @@ -18,6 +18,7 @@ nunjucksExport.configureNunjucks = (debug, templateDir) => {
/**
* Renders the template with nunjucks and returns a string.
*
* @deprecated since version 3.0
* @param {import('@asyncapi/parser').AsyncAPIDocument} asyncapiDocument
* @param {string} templateString template filecontent to be rendered with nunjucks
* @param {string} filePath path to the template file
Expand Down
Loading