1
0

vite-plugin-ui-config.ts 4.8 KB

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