vite-plugin-vendure-dashboard.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { lingui } from '@lingui/vite-plugin';
  2. import tailwindcss from '@tailwindcss/vite';
  3. import { TanStackRouterVite } from '@tanstack/router-plugin/vite';
  4. import react from '@vitejs/plugin-react';
  5. import path from 'path';
  6. import { PluginOption } from 'vite';
  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 { themeVariablesPlugin } from './vite-plugin-theme.js';
  13. import { ThemeVariablesPluginOptions } from './vite-plugin-theme.js';
  14. import { UiConfigPluginOptions, uiConfigPlugin } from './vite-plugin-ui-config.js';
  15. /**
  16. * @description
  17. * Options for the {@link vendureDashboardPlugin} Vite plugin.
  18. */
  19. export type VitePluginVendureDashboardOptions = {
  20. /**
  21. * @description
  22. * The path to the Vendure server configuration file.
  23. */
  24. vendureConfigPath: string | URL;
  25. /**
  26. * @description
  27. * The name of the exported variable from the Vendure server configuration file.
  28. * This is only required if the plugin is unable to auto-detect the name of the exported variable.
  29. */
  30. vendureConfigExport?: string;
  31. /**
  32. * @description
  33. * The path to the directory where the generated GraphQL Tada files will be output.
  34. */
  35. gqlTadaOutputPath?: string;
  36. tempCompilationDir?: string;
  37. disableTansStackRouterPlugin?: boolean;
  38. } & UiConfigPluginOptions &
  39. ThemeVariablesPluginOptions;
  40. /**
  41. * @description
  42. * This is a Vite plugin which configures a set of plugins required to build the Vendure Dashboard.
  43. */
  44. export function vendureDashboardPlugin(options: VitePluginVendureDashboardOptions): PluginOption[] {
  45. const tempDir = options.tempCompilationDir ?? path.join(import.meta.dirname, './.vendure-dashboard-temp');
  46. const normalizedVendureConfigPath = getNormalizedVendureConfigPath(options.vendureConfigPath);
  47. const packageRoot = getDashboardPackageRoot();
  48. const linguiConfigPath = path.join(packageRoot, 'lingui.config.js');
  49. if (process.env.IS_LOCAL_DEV !== 'true') {
  50. process.env.LINGUI_CONFIG = linguiConfigPath;
  51. }
  52. return [
  53. // TODO: solve https://github.com/kentcdodds/babel-plugin-macros/issues/87
  54. // lingui(),
  55. ...(options.disableTansStackRouterPlugin
  56. ? []
  57. : [
  58. TanStackRouterVite({
  59. autoCodeSplitting: true,
  60. routeFileIgnorePattern: '.graphql.ts|components',
  61. routesDirectory: path.join(packageRoot, 'src/app/routes'),
  62. generatedRouteTree: path.join(packageRoot, 'src/app/routeTree.gen.ts'),
  63. }),
  64. ]),
  65. react({
  66. // babel: {
  67. // plugins: ['@lingui/babel-plugin-lingui-macro'],
  68. // },
  69. }),
  70. themeVariablesPlugin({ theme: options.theme }),
  71. tailwindcss(),
  72. configLoaderPlugin({ vendureConfigPath: normalizedVendureConfigPath, tempDir }),
  73. viteConfigPlugin({ packageRoot }),
  74. adminApiSchemaPlugin(),
  75. dashboardMetadataPlugin({ rootDir: tempDir }),
  76. uiConfigPlugin({ adminUiConfig: options.adminUiConfig }),
  77. ...(options.gqlTadaOutputPath
  78. ? [gqlTadaPlugin({ gqlTadaOutputPath: options.gqlTadaOutputPath, tempDir, packageRoot })]
  79. : []),
  80. ];
  81. }
  82. /**
  83. * @description
  84. * Returns the path to the root of the `@vendure/dashboard` package.
  85. */
  86. function getDashboardPackageRoot(): string {
  87. const fileUrl = import.meta.resolve('@vendure/dashboard');
  88. const packagePath = fileUrl.startsWith('file:') ? new URL(fileUrl).pathname : fileUrl;
  89. return fixWindowsPath(path.join(packagePath, '../../../'));
  90. }
  91. /**
  92. * Get the normalized path to the Vendure config file given either a string or URL.
  93. */
  94. export function getNormalizedVendureConfigPath(vendureConfigPath: string | URL): string {
  95. const stringPath = typeof vendureConfigPath === 'string' ? vendureConfigPath : vendureConfigPath.href;
  96. if (stringPath.startsWith('file:')) {
  97. return fixWindowsPath(new URL(stringPath).pathname);
  98. }
  99. return fixWindowsPath(stringPath);
  100. }
  101. function fixWindowsPath(filePath: string): string {
  102. // Fix Windows paths that might start with a leading slash
  103. if (process.platform === 'win32') {
  104. // Remove leading slash before drive letter on Windows
  105. if (/^[/\\][A-Za-z]:/.test(filePath)) {
  106. return filePath.substring(1);
  107. }
  108. }
  109. return filePath;
  110. }