esm-path-alias.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { rm } from 'node:fs/promises';
  2. import { join } from 'node:path';
  3. import { describe, expect, it } from 'vitest';
  4. import { compile } from '../utils/compiler.js';
  5. import { debugLogger, noopLogger } from '../utils/logger.js';
  6. describe('handling ESM projects with tsconfig path aliases', () => {
  7. it('should compile ESM project with path aliases', { timeout: 60_000 }, async () => {
  8. const tempDir = join(__dirname, './__temp/esm-path-alias');
  9. await rm(tempDir, { recursive: true, force: true });
  10. const result = await compile({
  11. outputPath: tempDir,
  12. vendureConfigPath: join(__dirname, 'fixtures-esm-path-alias', 'vendure-config.ts'),
  13. logger: process.env.LOG ? debugLogger : noopLogger,
  14. module: 'esm',
  15. pathAdapter: {
  16. transformTsConfigPathMappings: ({ phase, patterns }) => {
  17. // For both compiling and loading phases, we need to strip the fixtures directory prefix
  18. // because the compiled output flattens the directory structure
  19. return patterns.map(pattern => {
  20. let transformed = pattern.replace(/\.\/fixtures-esm-path-alias\//, './');
  21. if (phase === 'loading') {
  22. transformed = transformed.replace(/.ts$/, '.js');
  23. }
  24. return transformed;
  25. });
  26. },
  27. },
  28. });
  29. expect(result.pluginInfo).toHaveLength(1);
  30. expect(result.pluginInfo[0].name).toBe('MyPlugin');
  31. expect(result.pluginInfo[0].dashboardEntryPath).toBe('./dashboard/index.tsx');
  32. // sourcePluginPath is undefined because the plugin is discovered from compiled output,
  33. // not from source analysis (path aliases in source can't be followed without runtime resolution)
  34. expect(result.pluginInfo[0].sourcePluginPath).toBeUndefined();
  35. expect(result.pluginInfo[0].pluginPath).toBe(join(tempDir, 'my-plugin', 'src', 'my.plugin.js'));
  36. });
  37. });