1
0

vite-plugin-ui-config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { LanguageCode, VendureConfig } from '@vendure/core';
  2. import { Plugin } from 'vite';
  3. import { getUiConfig } from './utils/ui-config.js';
  4. import { ConfigLoaderApi, getConfigLoaderApi } from './vite-plugin-config-loader.js';
  5. const virtualModuleId = 'virtual:vendure-ui-config';
  6. const resolvedVirtualModuleId = `\0${virtualModuleId}`;
  7. /**
  8. * @description
  9. * Options used by the {@link vendureDashboardPlugin} to configure how the Dashboard
  10. * connects to the Vendure Admin API
  11. *
  12. * @docsCategory vite-plugin
  13. * @docsPage vendureDashboardPlugin
  14. * @since 3.4.0
  15. */
  16. export interface ApiConfig {
  17. /**
  18. * @description
  19. * The hostname of the Vendure server which the admin UI will be making API calls
  20. * to. If set to "auto", the Admin UI app will determine the hostname from the
  21. * current location (i.e. `window.location.hostname`).
  22. *
  23. * @default 'auto'
  24. */
  25. host?: string | 'auto';
  26. /**
  27. * @description
  28. * The port of the Vendure server which the admin UI will be making API calls
  29. * to. If set to "auto", the Admin UI app will determine the port from the
  30. * current location (i.e. `window.location.port`).
  31. *
  32. * @default 'auto'
  33. */
  34. port?: number | 'auto';
  35. /**
  36. * @description
  37. * The path to the GraphQL Admin API.
  38. *
  39. * @default 'admin-api'
  40. */
  41. adminApiPath?: string;
  42. /**
  43. * @description
  44. * Whether to use cookies or bearer tokens to track sessions.
  45. * Should match the setting of in the server's `tokenMethod` config
  46. * option.
  47. *
  48. * @default 'cookie'
  49. */
  50. tokenMethod?: 'cookie' | 'bearer';
  51. /**
  52. * @description
  53. * The header used when using the 'bearer' auth method. Should match the
  54. * setting of the server's `authOptions.authTokenHeaderKey` config option.
  55. *
  56. * @default 'vendure-auth-token'
  57. */
  58. authTokenHeaderKey?: string;
  59. /**
  60. * @description
  61. * The name of the header which contains the channel token. Should match the
  62. * setting of the server's `apiOptions.channelTokenKey` config option.
  63. *
  64. * @default 'vendure-token'
  65. */
  66. channelTokenKey?: string;
  67. }
  68. /**
  69. * @description
  70. * Options used by the {@link vendureDashboardPlugin} to configure aspects of the
  71. * Dashboard UI behaviour.
  72. *
  73. * @docsCategory vite-plugin
  74. * @docsPage vendureDashboardPlugin
  75. * @since 3.4.0
  76. */
  77. export interface I18nConfig {
  78. /**
  79. * @description
  80. * The default language for the Admin UI. Must be one of the
  81. * items specified in the `availableLanguages` property.
  82. *
  83. * @default LanguageCode.en
  84. */
  85. defaultLanguage?: LanguageCode;
  86. /**
  87. * @description
  88. * The default locale for the Admin UI. The locale affects the formatting of
  89. * currencies & dates. Must be one of the items specified
  90. * in the `availableLocales` property.
  91. *
  92. * If not set, the browser default locale will be used.
  93. *
  94. * @since 2.2.0
  95. */
  96. defaultLocale?: string;
  97. /**
  98. * @description
  99. * An array of languages for which translations exist for the Admin UI.
  100. */
  101. availableLanguages?: LanguageCode[];
  102. /**
  103. * @description
  104. * An array of locales to be used on Admin UI.
  105. *
  106. * @since 2.2.0
  107. */
  108. availableLocales?: string[];
  109. }
  110. /**
  111. * @description
  112. * Options used by the {@link vendureDashboardPlugin} to configure aspects of the
  113. * Dashboard UI behaviour.
  114. *
  115. * @docsCategory vite-plugin
  116. * @docsPage vendureDashboardPlugin
  117. * @since 3.4.0
  118. */
  119. export interface UiConfigPluginOptions {
  120. /**
  121. * @description
  122. * Configuration for API connection settings
  123. */
  124. api?: ApiConfig;
  125. /**
  126. * @description
  127. * Configuration for internationalization settings
  128. */
  129. i18n?: I18nConfig;
  130. }
  131. /**
  132. * @description
  133. * The resolved UI configuration with all defaults applied.
  134. * This is the type of the configuration object available at runtime.
  135. */
  136. export interface ResolvedUiConfig {
  137. /**
  138. * @description
  139. * API connection settings with all defaults applied
  140. */
  141. api: Required<ApiConfig>;
  142. /**
  143. * @description
  144. * Internationalization settings with all defaults applied.
  145. * Note: defaultLocale remains optional as it can be undefined.
  146. */
  147. i18n: Required<Omit<I18nConfig, 'defaultLocale'>> & Pick<I18nConfig, 'defaultLocale'>;
  148. }
  149. /**
  150. * This Vite plugin scans the configured plugins for any dashboard extensions and dynamically
  151. * generates an import statement for each one, wrapped up in a `runDashboardExtensions()`
  152. * function which can then be imported and executed in the Dashboard app.
  153. */
  154. export function uiConfigPlugin(options: UiConfigPluginOptions = {}): Plugin {
  155. let configLoaderApi: ConfigLoaderApi;
  156. let vendureConfig: VendureConfig;
  157. return {
  158. name: 'vendure:dashboard-ui-config',
  159. configResolved({ plugins }) {
  160. configLoaderApi = getConfigLoaderApi(plugins);
  161. },
  162. resolveId(id) {
  163. if (id === virtualModuleId) {
  164. return resolvedVirtualModuleId;
  165. }
  166. },
  167. async load(id) {
  168. if (id === resolvedVirtualModuleId) {
  169. if (!vendureConfig) {
  170. const result = await configLoaderApi.getVendureConfig();
  171. vendureConfig = result.vendureConfig;
  172. }
  173. const config = getUiConfig(vendureConfig, options);
  174. return `
  175. export const uiConfig = ${JSON.stringify(config)}
  176. `;
  177. }
  178. },
  179. };
  180. }