|
| 1 | +import path from 'path'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { |
| 4 | + isSafeRelativePath, |
| 5 | + normalizeGenerateTypesOptions, |
| 6 | + resolveEmitAssetName, |
| 7 | +} from './GenerateTypesPlugin'; |
| 8 | + |
| 9 | +describe('GenerateTypesPlugin', () => { |
| 10 | + const basePluginOptions = { |
| 11 | + name: 'testRemote', |
| 12 | + filename: 'remoteEntry.js', |
| 13 | + exposes: { |
| 14 | + './button': './src/components/button', |
| 15 | + }, |
| 16 | + shared: {}, |
| 17 | + }; |
| 18 | + |
| 19 | + describe('normalizeGenerateTypesOptions', () => { |
| 20 | + it('should use compiler outputDir when user does not set outputDir', () => { |
| 21 | + const result = normalizeGenerateTypesOptions({ |
| 22 | + context: '/project', |
| 23 | + outputDir: 'dist', |
| 24 | + dtsOptions: { |
| 25 | + generateTypes: { |
| 26 | + generateAPITypes: true, |
| 27 | + }, |
| 28 | + consumeTypes: false, |
| 29 | + }, |
| 30 | + pluginOptions: basePluginOptions, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result).toBeDefined(); |
| 34 | + expect(result!.remote.outputDir).toBe('dist'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should allow user outputDir to override compiler outputDir', () => { |
| 38 | + const result = normalizeGenerateTypesOptions({ |
| 39 | + context: '/project', |
| 40 | + outputDir: 'dist', |
| 41 | + dtsOptions: { |
| 42 | + generateTypes: { |
| 43 | + generateAPITypes: true, |
| 44 | + outputDir: 'dist/production', |
| 45 | + }, |
| 46 | + consumeTypes: false, |
| 47 | + }, |
| 48 | + pluginOptions: basePluginOptions, |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result).toBeDefined(); |
| 52 | + expect(result!.remote.outputDir).toBe('dist/production'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return undefined when generateTypes is false', () => { |
| 56 | + const result = normalizeGenerateTypesOptions({ |
| 57 | + context: '/project', |
| 58 | + outputDir: 'dist', |
| 59 | + dtsOptions: { |
| 60 | + generateTypes: false, |
| 61 | + consumeTypes: false, |
| 62 | + }, |
| 63 | + pluginOptions: basePluginOptions, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result).toBeUndefined(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('asset emission path calculation', () => { |
| 71 | + // These tests verify the path.relative logic used in emitTypesFiles |
| 72 | + // to ensure correct asset names under various outputDir configurations |
| 73 | + |
| 74 | + it('should compute relative zip path same as basename when outputDir matches compiler output', () => { |
| 75 | + const compilerOutputPath = path.resolve('/project', 'dist'); |
| 76 | + const zipTypesPath = path.resolve('/project', 'dist', '@mf-types.zip'); |
| 77 | + |
| 78 | + const relZip = path.relative(compilerOutputPath, zipTypesPath); |
| 79 | + expect(relZip).toBe('@mf-types.zip'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should compute relative zip path with subdirectory when custom outputDir is deeper', () => { |
| 83 | + const compilerOutputPath = path.resolve('/project', 'dist'); |
| 84 | + const zipTypesPath = path.resolve( |
| 85 | + '/project', |
| 86 | + 'dist', |
| 87 | + 'production', |
| 88 | + '@mf-types.zip', |
| 89 | + ); |
| 90 | + |
| 91 | + const relZip = path.relative(compilerOutputPath, zipTypesPath); |
| 92 | + expect(relZip).toBe(path.join('production', '@mf-types.zip')); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should compute relative api types path with subdirectory', () => { |
| 96 | + const compilerOutputPath = path.resolve('/project', 'dist/react'); |
| 97 | + const apiTypesPath = path.resolve( |
| 98 | + '/project', |
| 99 | + 'dist/react/staging', |
| 100 | + '@mf-types.d.ts', |
| 101 | + ); |
| 102 | + |
| 103 | + const relApi = path.relative(compilerOutputPath, apiTypesPath); |
| 104 | + expect(relApi).toBe(path.join('staging', '@mf-types.d.ts')); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should fall back to basename when zip is outside compiler output (starts with ..)', () => { |
| 108 | + const compilerOutputPath = path.resolve('/project', 'dist'); |
| 109 | + const zipTypesPath = path.resolve( |
| 110 | + '/other-project', |
| 111 | + 'dist', |
| 112 | + '@mf-types.zip', |
| 113 | + ); |
| 114 | + |
| 115 | + const relZip = path.relative(compilerOutputPath, zipTypesPath); |
| 116 | + // When the relative path starts with '..', the plugin should fall back to basename |
| 117 | + expect(isSafeRelativePath(relZip)).toBe(false); |
| 118 | + |
| 119 | + // Verify fallback behavior |
| 120 | + const emitZipName = resolveEmitAssetName({ |
| 121 | + compilerOutputPath, |
| 122 | + assetPath: zipTypesPath, |
| 123 | + fallbackName: path.basename(zipTypesPath), |
| 124 | + }); |
| 125 | + expect(emitZipName).toBe('@mf-types.zip'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle nested deploy environment subdirectories', () => { |
| 129 | + // Simulates: webpack output = dist/react, entry at dist/react/staging/ |
| 130 | + const compilerOutputPath = path.resolve('/project', 'dist/react'); |
| 131 | + const customOutputDir = 'dist/react/staging'; |
| 132 | + const zipTypesPath = path.resolve( |
| 133 | + '/project', |
| 134 | + customOutputDir, |
| 135 | + '@mf-types.zip', |
| 136 | + ); |
| 137 | + |
| 138 | + const relZip = path.relative(compilerOutputPath, zipTypesPath); |
| 139 | + expect(relZip).toBe(path.join('staging', '@mf-types.zip')); |
| 140 | + expect(relZip.startsWith('..')).toBe(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle custom typesFolder with custom outputDir', () => { |
| 144 | + const compilerOutputPath = path.resolve('/project', 'dist'); |
| 145 | + const zipTypesPath = path.resolve( |
| 146 | + '/project', |
| 147 | + 'dist', |
| 148 | + 'production', |
| 149 | + 'my-types.zip', |
| 150 | + ); |
| 151 | + |
| 152 | + const relZip = path.relative(compilerOutputPath, zipTypesPath); |
| 153 | + expect(relZip).toBe(path.join('production', 'my-types.zip')); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should treat windows cross-drive path as unsafe relative path', () => { |
| 157 | + const relZip = path.win32.relative( |
| 158 | + 'C:\\dist', |
| 159 | + 'D:\\types\\@mf-types.zip', |
| 160 | + ); |
| 161 | + expect(relZip).toBe('D:\\types\\@mf-types.zip'); |
| 162 | + expect(isSafeRelativePath(relZip)).toBe(false); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should resolve relative asset name for nested output directory', () => { |
| 166 | + const emitZipName = resolveEmitAssetName({ |
| 167 | + compilerOutputPath: path.resolve('/project', 'dist'), |
| 168 | + assetPath: path.resolve( |
| 169 | + '/project', |
| 170 | + 'dist', |
| 171 | + 'production', |
| 172 | + '@mf-types.zip', |
| 173 | + ), |
| 174 | + fallbackName: '@mf-types.zip', |
| 175 | + }); |
| 176 | + expect(emitZipName).toBe(path.join('production', '@mf-types.zip')); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments