diff --git a/.changeset/depreciate-nunjucks.md b/.changeset/depreciate-nunjucks.md new file mode 100644 index 000000000..defb5e224 --- /dev/null +++ b/.changeset/depreciate-nunjucks.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/generator": major +--- + +Migrating from Nunjucks render engine to React render engine \ No newline at end of file diff --git a/README.md b/README.md index 5fe3c6018..11745b47b 100644 --- a/README.md +++ b/README.md @@ -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 deprecated and will be removed in 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) diff --git a/apps/generator/docs/file-templates.md b/apps/generator/docs/file-templates.md index 7a7a8ce77..3bdda56af 100644 --- a/apps/generator/docs/file-templates.md +++ b/apps/generator/docs/file-templates.md @@ -5,7 +5,9 @@ weight: 140 ## Generating files with the Nunjucks render engine -> **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. +> **Note**: This section applies only to the Nunjucks render engine. For information on using the React render engine, please look at the [Generating files with the React render engine](#generating-files-with-the-react-render-engine) section below. + +> **Note**: Nunjucks renderer engine will be deprecated in the future release and we strongly recommend using the React renderer engine instead. 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: diff --git a/apps/generator/docs/nunjucks-depreciate.md b/apps/generator/docs/nunjucks-depreciate.md new file mode 100644 index 000000000..8c932c018 --- /dev/null +++ b/apps/generator/docs/nunjucks-depreciate.md @@ -0,0 +1,139 @@ +-- +title: "Migrating from Nunjucks to React render engine" +weight: 170 +--- + +## Migration Guide from nunjucks render engine to react render engine + +### Introduction + +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. + +### Step-by-step migration guide + +#### 1. Update package.json + +Change your template configuration in `package.json`: + +```json +{ +"generator": { +"renderer": "react" +} +} +``` + +Once the deprecation period has ended, and we remove the default Nunjucks, the React render engine will be used by default and this setting will no longer be needed to configure + +#### 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: + +#### 4. Basic template structure + +Nunjucks: +```js +

{{ asyncapi.info().title() }}

+

{{ asyncapi.info().description() }}

+``` + +React: +```js +import { File } from '@asyncapi/generator-react-sdk'; + +export default function({ asyncapi }) { + return ( + +

{asyncapi.info().title()}

+

{asyncapi.info().description()}

+
+ ); +} +``` + +#### 5. Macros and Partials + +Replace macros with React components: + +Nunjucks: +```js +{% macro renderChannel(channel) %} +
+

{{ channel.address() }}

+

{{ channel.description() }}

+
+{% endmacro %} + +{{ renderChannel(someChannel) }} +``` + +React: +```js +// components/Channel.js +import { Text } from '@asyncapi/generator-react-sdk'; + +export function Channel({ channel }) { + return ( + +
+

{channel.address()}

+

{channel.description()}

+
+
+ ); +} + +// Main template +import { File, Text } from '@asyncapi/generator-react-sdk'; +import { Channel } from './components/Channel'; + +export default function({ asyncapi }) { + return ( + + +

Channels

+
+ {asyncapi.channels().map(channel => ( + + ))} +
+ ); +} +``` + +#### 6. File template + +The detailed guide on file template can be found in [this](file-templates.md) guide. + +### 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 + +### 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) \ No newline at end of file diff --git a/apps/generator/docs/nunjucks-render-engine.md b/apps/generator/docs/nunjucks-render-engine.md index 34626b49b..4c7cdbe21 100644 --- a/apps/generator/docs/nunjucks-render-engine.md +++ b/apps/generator/docs/nunjucks-render-engine.md @@ -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. ### Common assumptions diff --git a/apps/generator/lib/filtersRegistry.js b/apps/generator/lib/filtersRegistry.js index 6659960ed..53406b1b5 100644 --- a/apps/generator/lib/filtersRegistry.js +++ b/apps/generator/lib/filtersRegistry.js @@ -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. @@ -21,6 +22,7 @@ module.exports.registerFilters = async (nunjucks, templateConfig, templateDir, f /** * Registers the local template filters. + * @deprecated since version 3.0 * @private * @param {Object} nunjucks Nunjucks environment. * @param {String} templateDir Directory where template is located. @@ -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. @@ -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. diff --git a/apps/generator/lib/renderer/nunjucks.js b/apps/generator/lib/renderer/nunjucks.js index 410b8d945..c1c6086be 100644 --- a/apps/generator/lib/renderer/nunjucks.js +++ b/apps/generator/lib/renderer/nunjucks.js @@ -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 @@ -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