vite-plugin-dashboard-metadata.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import path from 'path';
  2. import { Plugin } from 'vite';
  3. import { CompileResult } from './utils/compiler.js';
  4. import { ConfigLoaderApi, getConfigLoaderApi } from './vite-plugin-config-loader.js';
  5. const virtualModuleId = 'virtual:dashboard-extensions';
  6. const resolvedVirtualModuleId = `\0${virtualModuleId}`;
  7. /**
  8. * This Vite plugin scans the configured plugins for any dashboard extensions and dynamically
  9. * generates an import statement for each one, wrapped up in a `runDashboardExtensions()`
  10. * function which can then be imported and executed in the Dashboard app.
  11. */
  12. export function dashboardMetadataPlugin(): Plugin {
  13. let configLoaderApi: ConfigLoaderApi;
  14. let loadVendureConfigResult: CompileResult;
  15. return {
  16. name: 'vendure:dashboard-extensions-metadata',
  17. configResolved({ plugins }) {
  18. configLoaderApi = getConfigLoaderApi(plugins);
  19. },
  20. resolveId(id) {
  21. if (id === virtualModuleId) {
  22. return resolvedVirtualModuleId;
  23. }
  24. },
  25. async load(id) {
  26. if (id === resolvedVirtualModuleId) {
  27. const startTime = Date.now();
  28. this.debug('Loading dashboard extensions...');
  29. if (!loadVendureConfigResult) {
  30. const configStart = Date.now();
  31. loadVendureConfigResult = await configLoaderApi.getVendureConfig();
  32. this.debug(`Loaded Vendure config in ${Date.now() - configStart}ms`);
  33. }
  34. const { pluginInfo } = loadVendureConfigResult;
  35. const resolveStart = Date.now();
  36. const pluginsWithExtensions =
  37. pluginInfo
  38. ?.map(({ dashboardEntryPath, pluginPath, sourcePluginPath }) => {
  39. if (!dashboardEntryPath) {
  40. return null;
  41. }
  42. // For local plugins, use the sourcePluginPath to resolve the dashboard extension
  43. const basePath = sourcePluginPath
  44. ? path.dirname(sourcePluginPath)
  45. : path.dirname(pluginPath);
  46. const resolved = path.resolve(basePath, dashboardEntryPath);
  47. this.debug(`Resolved extension path: ${resolved}`);
  48. return resolved;
  49. })
  50. .filter(x => x != null) ?? [];
  51. this.info(
  52. `Found ${pluginsWithExtensions.length} Dashboard extensions in ${Date.now() - resolveStart}ms`,
  53. );
  54. this.debug(`Total dashboard extension loading completed in ${Date.now() - startTime}ms`);
  55. return `
  56. export async function runDashboardExtensions() {
  57. ${pluginsWithExtensions
  58. .map(extension => {
  59. return `await import(\`${extension}\`);`;
  60. })
  61. .join('\n')}
  62. }`;
  63. }
  64. },
  65. };
  66. }