From 4bce4ca0c31b3034b6305b65548932f4e0dd0fae Mon Sep 17 00:00:00 2001 From: Mikhail Golbakh Date: Fri, 22 Aug 2025 16:25:56 +0200 Subject: [PATCH] feat: add remote-versions plugin --- README-ru.md | 37 ++++++++++++++++++++++++++++ README.md | 37 ++++++++++++++++++++++++++++ src/index.ts | 2 ++ src/plugins/index.ts | 2 ++ src/plugins/remote-versions/index.ts | 25 +++++++++++++++++++ src/plugins/remote-versions/types.ts | 1 + 6 files changed, 104 insertions(+) create mode 100644 src/plugins/remote-versions/index.ts create mode 100644 src/plugins/remote-versions/types.ts diff --git a/README-ru.md b/README-ru.md index eb9d119..b4d059e 100644 --- a/README-ru.md +++ b/README-ru.md @@ -459,6 +459,43 @@ interface UikitPluginOptions { } ``` +### Remote Versions + +Добавляет информацию о версиях микрофронтендов на страницу. + +Этот плагин создает глобальный объект `window.__REMOTE_VERSIONS__`, содержащий указанные версии микрофронтендов, который может использоваться module federation или аналогичными архитектурами микрофронтендов для определения того, какие версии удаленных модулей загружать. + +Он может использоваться в связке с [App Builder](https://github.com/gravity-ui/app-builder?tab=readme-ov-file#module-federation) и опцией `moduleFederation.remotesRuntimeVersioning` для автоматической загрузки удаленных модулей с соответствующими версиями. + +Использование: + +```js +import {createRenderFunction, createRemoteVersionsPlugin} from '@gravity-ui/app-layout'; + +const renderLayout = createRenderFunction([createRemoteVersionsPlugin()]); + +app.get((req, res) => { + res.send( + renderLayout({ + title: 'Home page', + pluginsOptions: { + remoteVersions: { + header: '1.2.3', + footer: '2.1.0', + sidebar: '0.5.1', + }, + }, + }), + ); +}); +``` + +Параметры плагина: + +```typescript +type RemoteVersionsPluginOptions = Record; +``` + ### Утилиты Для создания плагинов используется предустановленная утилита: diff --git a/README.md b/README.md index 599ac02..2f38f1c 100644 --- a/README.md +++ b/README.md @@ -459,6 +459,43 @@ interface UikitPluginOptions { } ``` +### Remote Versions + +Adds microfrontend versions information to the page. + +This plugin creates a global `window.__REMOTE_VERSIONS__` object containing the provided microfrontend versions, which can be used by module federation or similar microfrontend architectures to determine which versions of remote modules to load. + +It can be used in combination with [App Builder](https://github.com/gravity-ui/app-builder?tab=readme-ov-file#module-federation) and the `moduleFederation.remotesRuntimeVersioning` option to automatically load remote modules with the corresponding versions. + +Usage: + +```js +import {createRenderFunction, createRemoteVersionsPlugin} from '@gravity-ui/app-layout'; + +const renderLayout = createRenderFunction([createRemoteVersionsPlugin()]); + +app.get((req, res) => { + res.send( + renderLayout({ + title: 'Home page', + pluginsOptions: { + remoteVersions: { + header: '1.2.3', + footer: '2.1.0', + sidebar: '0.5.1', + }, + }, + }), + ); +}); +``` + +Plugin options: + +```typescript +type RemoteVersionsPluginOptions = Record; +``` + ### Helpers There is helper to create all plugins: diff --git a/src/index.ts b/src/index.ts index ee4516b..b391db4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ export { createYandexMetrikaPlugin, createLayoutPlugin, createUikitPlugin, + createRemoteVersionsPlugin, createDefaultPlugins, } from './plugins/index.js'; @@ -33,3 +34,4 @@ export type { } from './plugins/google-analytics/index.js'; export type {LayoutPluginOptions, Manifest, LayoutInitOptions} from './plugins/layout/index.js'; export type {UikitPluginOptions} from './plugins/uikit/index.js'; +export type {RemoteVersionsPluginOptions} from './plugins/remote-versions/index.js'; diff --git a/src/plugins/index.ts b/src/plugins/index.ts index 0fd0d68..7c5bc22 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -1,6 +1,7 @@ import {createGoogleAnalyticsPlugin} from './google-analytics/index.js'; import {createLayoutPlugin} from './layout/index.js'; import type {LayoutInitOptions} from './layout/index.js'; +import {createRemoteVersionsPlugin} from './remote-versions/index.js'; import {createUikitPlugin} from './uikit/index.js'; import {createYandexMetrikaPlugin} from './yandex-metrika/index.js'; @@ -18,4 +19,5 @@ export { createYandexMetrikaPlugin, createLayoutPlugin, createUikitPlugin, + createRemoteVersionsPlugin, }; diff --git a/src/plugins/remote-versions/index.ts b/src/plugins/remote-versions/index.ts new file mode 100644 index 0000000..6eb742f --- /dev/null +++ b/src/plugins/remote-versions/index.ts @@ -0,0 +1,25 @@ +import htmlescape from 'htmlescape'; + +import type {Plugin} from '../../types.js'; + +import type {RemoteVersionsPluginOptions} from './types.js'; + +export type {RemoteVersionsPluginOptions} from './types.js'; + +export function createRemoteVersionsPlugin(): Plugin< + RemoteVersionsPluginOptions, + 'remoteVersions' +> { + return { + name: 'remoteVersions', + apply({options: versions, renderContent}) { + if (!versions) { + return; + } + + renderContent.inlineScripts.push( + `window.__REMOTE_VERSIONS__ = ${htmlescape(versions)};`, + ); + }, + }; +} diff --git a/src/plugins/remote-versions/types.ts b/src/plugins/remote-versions/types.ts new file mode 100644 index 0000000..66df2c0 --- /dev/null +++ b/src/plugins/remote-versions/types.ts @@ -0,0 +1 @@ +export type RemoteVersionsPluginOptions = Record;