vite-plugin-vendure-dashboard.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import tailwindcss from '@tailwindcss/vite';
  2. import { TanStackRouterVite } from '@tanstack/router-plugin/vite';
  3. import react from '@vitejs/plugin-react';
  4. import path from 'path';
  5. import { PluginOption } from 'vite';
  6. import { PathAdapter } from './utils/config-loader.js';
  7. import { adminApiSchemaPlugin } from './vite-plugin-admin-api-schema.js';
  8. import { configLoaderPlugin } from './vite-plugin-config-loader.js';
  9. import { viteConfigPlugin } from './vite-plugin-config.js';
  10. import { dashboardMetadataPlugin } from './vite-plugin-dashboard-metadata.js';
  11. import { gqlTadaPlugin } from './vite-plugin-gql-tada.js';
  12. import { dashboardTailwindSourcePlugin } from './vite-plugin-tailwind-source.js';
  13. import { themeVariablesPlugin, ThemeVariablesPluginOptions } from './vite-plugin-theme.js';
  14. import { transformIndexHtmlPlugin } from './vite-plugin-transform-index.js';
  15. import { uiConfigPlugin, UiConfigPluginOptions } from './vite-plugin-ui-config.js';
  16. /**
  17. * @description
  18. * Options for the {@link vendureDashboardPlugin} Vite plugin.
  19. */
  20. export type VitePluginVendureDashboardOptions = {
  21. /**
  22. * @description
  23. * The path to the Vendure server configuration file.
  24. */
  25. vendureConfigPath: string | URL;
  26. /**
  27. * @description
  28. * The {@link PathAdapter} allows you to customize the resolution of paths
  29. * in the compiled Vendure source code which is used as part of the
  30. * introspection step of building the dashboard.
  31. *
  32. * It enables support for more complex repository structures, such as
  33. * monorepos, where the Vendure server configuration file may not
  34. * be located in the root directory of the project.
  35. *
  36. * If you get compilation errors like "Error loading Vendure config: Cannot find module",
  37. * you probably need to provide a custom `pathAdapter` to resolve the paths correctly.
  38. *
  39. * @example
  40. * ```ts
  41. * vendureDashboardPlugin({
  42. * tempCompilationDir: join(__dirname, './__vendure-dashboard-temp'),
  43. * pathAdapter: {
  44. * getCompiledConfigPath: ({ inputRootDir, outputPath, configFileName }) => {
  45. * const projectName = inputRootDir.split('/libs/')[1].split('/')[0];
  46. * const pathAfterProject = inputRootDir.split(`/libs/${projectName}`)[1];
  47. * const compiledConfigFilePath = `${outputPath}/${projectName}${pathAfterProject}`;
  48. * return path.join(compiledConfigFilePath, configFileName);
  49. * },
  50. * transformTsConfigPathMappings: ({ phase, patterns }) => {
  51. * // "loading" phase is when the compiled Vendure code is being loaded by
  52. * // the plugin, in order to introspect the configuration of your app.
  53. * if (phase === 'loading') {
  54. * return patterns.map((p) =>
  55. * p.replace('libs/', '').replace(/.ts$/, '.js'),
  56. * );
  57. * }
  58. * return patterns;
  59. * },
  60. * },
  61. * // ...
  62. * }),
  63. * ```
  64. */
  65. pathAdapter?: PathAdapter;
  66. /**
  67. * @description
  68. * The name of the exported variable from the Vendure server configuration file.
  69. * This is only required if the plugin is unable to auto-detect the name of the exported variable.
  70. */
  71. vendureConfigExport?: string;
  72. /**
  73. * @description
  74. * The path to the directory where the generated GraphQL Tada files will be output.
  75. */
  76. gqlTadaOutputPath?: string;
  77. tempCompilationDir?: string;
  78. disableTansStackRouterPlugin?: boolean;
  79. /**
  80. * @description
  81. * If set to `true`, compilation errors during the build process will be reported and
  82. * the build will fail.
  83. *
  84. * @default false
  85. */
  86. reportCompilationErrors?: boolean;
  87. } & UiConfigPluginOptions &
  88. ThemeVariablesPluginOptions;
  89. /**
  90. * @description
  91. * This is a Vite plugin which configures a set of plugins required to build the Vendure Dashboard.
  92. */
  93. export function vendureDashboardPlugin(options: VitePluginVendureDashboardOptions): PluginOption[] {
  94. const tempDir = options.tempCompilationDir ?? path.join(import.meta.dirname, './.vendure-dashboard-temp');
  95. const normalizedVendureConfigPath = getNormalizedVendureConfigPath(options.vendureConfigPath);
  96. const packageRoot = getDashboardPackageRoot();
  97. const linguiConfigPath = path.join(packageRoot, 'lingui.config.js');
  98. if (process.env.IS_LOCAL_DEV !== 'true') {
  99. process.env.LINGUI_CONFIG = linguiConfigPath;
  100. }
  101. return [
  102. // TODO: solve https://github.com/kentcdodds/babel-plugin-macros/issues/87
  103. // lingui(),
  104. ...(options.disableTansStackRouterPlugin
  105. ? []
  106. : [
  107. TanStackRouterVite({
  108. autoCodeSplitting: true,
  109. routeFileIgnorePattern: '.graphql.ts|components',
  110. routesDirectory: path.join(packageRoot, 'src/app/routes'),
  111. generatedRouteTree: path.join(packageRoot, 'src/app/routeTree.gen.ts'),
  112. }),
  113. ]),
  114. react({
  115. // babel: {
  116. // plugins: ['@lingui/babel-plugin-lingui-macro'],
  117. // },
  118. }),
  119. themeVariablesPlugin({ theme: options.theme }),
  120. dashboardTailwindSourcePlugin(),
  121. tailwindcss(),
  122. configLoaderPlugin({
  123. vendureConfigPath: normalizedVendureConfigPath,
  124. tempDir,
  125. reportCompilationErrors: options.reportCompilationErrors,
  126. pathAdapter: options.pathAdapter,
  127. }),
  128. viteConfigPlugin({ packageRoot }),
  129. adminApiSchemaPlugin(),
  130. dashboardMetadataPlugin(),
  131. uiConfigPlugin({ adminUiConfig: options.adminUiConfig }),
  132. ...(options.gqlTadaOutputPath
  133. ? [gqlTadaPlugin({ gqlTadaOutputPath: options.gqlTadaOutputPath, tempDir, packageRoot })]
  134. : []),
  135. transformIndexHtmlPlugin(),
  136. ];
  137. }
  138. /**
  139. * @description
  140. * Returns the path to the root of the `@vendure/dashboard` package.
  141. */
  142. function getDashboardPackageRoot(): string {
  143. const fileUrl = import.meta.resolve('@vendure/dashboard');
  144. const packagePath = fileUrl.startsWith('file:') ? new URL(fileUrl).pathname : fileUrl;
  145. return fixWindowsPath(path.join(packagePath, '../../../'));
  146. }
  147. /**
  148. * Get the normalized path to the Vendure config file given either a string or URL.
  149. */
  150. export function getNormalizedVendureConfigPath(vendureConfigPath: string | URL): string {
  151. const stringPath = typeof vendureConfigPath === 'string' ? vendureConfigPath : vendureConfigPath.href;
  152. if (stringPath.startsWith('file:')) {
  153. return fixWindowsPath(new URL(stringPath).pathname);
  154. }
  155. return fixWindowsPath(stringPath);
  156. }
  157. function fixWindowsPath(filePath: string): string {
  158. // Fix Windows paths that might start with a leading slash
  159. if (process.platform === 'win32') {
  160. // Remove leading slash before drive letter on Windows
  161. if (/^[/\\][A-Za-z]:/.test(filePath)) {
  162. return filePath.substring(1);
  163. }
  164. }
  165. return filePath;
  166. }