|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { dirname, resolve } from 'node:path' |
| 4 | +import { afterEach, describe, expect, it } from 'vitest' |
| 5 | + |
| 6 | +import { Runtime } from '../src/core/runtime' |
| 7 | +import { normalizePath } from '../src/core/utils' |
| 8 | + |
| 9 | +describe('runtime tests', () => { |
| 10 | + let tempDir: string |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + if (tempDir) { |
| 14 | + rmSync(tempDir, { recursive: true, force: true }) |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + it('should resolve paths relative to tsconfig dir when baseUrl is absent', async () => { |
| 19 | + tempDir = mkdtempSync(resolve(tmpdir(), 'unplugin-dts-')) |
| 20 | + |
| 21 | + writeFileSync( |
| 22 | + resolve(tempDir, 'tsconfig.json'), |
| 23 | + JSON.stringify({ |
| 24 | + compilerOptions: { |
| 25 | + paths: { |
| 26 | + '@/*': ['./src/*'], |
| 27 | + }, |
| 28 | + }, |
| 29 | + include: ['src/**/*'], |
| 30 | + }), |
| 31 | + ) |
| 32 | + |
| 33 | + mkdirSync(resolve(tempDir, 'src')) |
| 34 | + writeFileSync(resolve(tempDir, 'src', 'index.ts'), 'export const foo = 1\n') |
| 35 | + writeFileSync(resolve(tempDir, 'src', 'helper.ts'), 'export const bar = 2\n') |
| 36 | + |
| 37 | + const runtime = await Runtime.toInstance({ |
| 38 | + root: tempDir, |
| 39 | + tsconfigPath: 'tsconfig.json', |
| 40 | + pathsToAliases: true, |
| 41 | + }) |
| 42 | + |
| 43 | + const alias = runtime.aliases.find((a: any) => |
| 44 | + typeof a.find === 'string' ? a.find === '@/' : a.find.test('@/helper'), |
| 45 | + ) |
| 46 | + |
| 47 | + expect(alias).toBeDefined() |
| 48 | + expect(normalizePath(alias!.replacement)).toBe(normalizePath(resolve(tempDir, 'src/$1'))) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should use baseUrl when explicitly set', async () => { |
| 52 | + tempDir = mkdtempSync(resolve(tmpdir(), 'unplugin-dts-')) |
| 53 | + |
| 54 | + writeFileSync( |
| 55 | + resolve(tempDir, 'tsconfig.json'), |
| 56 | + JSON.stringify({ |
| 57 | + compilerOptions: { |
| 58 | + baseUrl: './lib', |
| 59 | + paths: { |
| 60 | + '@/*': ['./src/*'], |
| 61 | + }, |
| 62 | + }, |
| 63 | + include: ['lib/src/**/*'], |
| 64 | + }), |
| 65 | + ) |
| 66 | + |
| 67 | + mkdirSync(resolve(tempDir, 'lib', 'src'), { recursive: true }) |
| 68 | + writeFileSync(resolve(tempDir, 'lib', 'src', 'index.ts'), 'export const foo = 1\n') |
| 69 | + |
| 70 | + const runtime = await Runtime.toInstance({ |
| 71 | + root: tempDir, |
| 72 | + tsconfigPath: 'tsconfig.json', |
| 73 | + pathsToAliases: true, |
| 74 | + }) |
| 75 | + |
| 76 | + const alias = runtime.aliases.find((a: any) => |
| 77 | + typeof a.find === 'string' ? a.find === '@/' : a.find.test('@/helper'), |
| 78 | + ) |
| 79 | + |
| 80 | + expect(alias).toBeDefined() |
| 81 | + expect(normalizePath(alias!.replacement)).toBe(normalizePath(resolve(tempDir, 'lib/src/$1'))) |
| 82 | + }) |
| 83 | +}) |
0 commit comments