|
| 1 | +import { |
| 2 | + cp, |
| 3 | + mkdir, |
| 4 | + mkdtemp, |
| 5 | + realpath, |
| 6 | + rm, |
| 7 | + symlink, |
| 8 | + writeFile, |
| 9 | +} from 'node:fs/promises' |
| 10 | +import os from 'node:os' |
| 11 | +import path from 'node:path' |
| 12 | +import { rolldownBuild } from '@sxzz/test-utils' |
| 13 | +import { afterEach, beforeEach, expect, test } from 'vitest' |
| 14 | +import { dts } from '../src/index.ts' |
| 15 | +import { findSourceMapChunk } from './utils.ts' |
| 16 | + |
| 17 | +const fixture = path.resolve(import.meta.dirname, 'fixtures/content-mapper') |
| 18 | +const root = await realpath( |
| 19 | + await mkdtemp(path.join(os.tmpdir(), 'dts-content-mapper-')), |
| 20 | +) |
| 21 | +beforeEach(async () => { |
| 22 | + await cp(fixture, root, { recursive: true }) |
| 23 | + await mkdir(path.join(root, 'node_modules')) |
| 24 | + await symlink( |
| 25 | + path.join(root, 'mapper'), |
| 26 | + path.join(root, 'node_modules/test-content-mapper'), |
| 27 | + 'dir', |
| 28 | + ) |
| 29 | +}) |
| 30 | +afterEach(() => rm(root, { recursive: true, force: true })) |
| 31 | +const moduleUrl = import.meta.resolve('typescript-next') |
| 32 | +const options = { |
| 33 | + generator: 'tsgo' as const, |
| 34 | + tsconfig: path.join(root, 'tsconfig.json'), |
| 35 | + emitDtsOnly: true, |
| 36 | + tsgo: { moduleUrl, runExternalCode: true }, |
| 37 | +} |
| 38 | + |
| 39 | +test.each(['index.ts', 'Example.component'])( |
| 40 | + 'content mapper: %s', |
| 41 | + async (entry) => { |
| 42 | + const { chunks } = await rolldownBuild(path.join(root, entry), [ |
| 43 | + dts(options), |
| 44 | + ]) |
| 45 | + const declarations = chunks.filter((chunk) => chunk.type === 'chunk') |
| 46 | + expect(declarations.length).toBeGreaterThan(0) |
| 47 | + expect(declarations.map((chunk) => chunk.code).join('\n')).toContain( |
| 48 | + 'name: string', |
| 49 | + ) |
| 50 | + }, |
| 51 | +) |
| 52 | + |
| 53 | +test('content mappers require external code permission', () => { |
| 54 | + expect(() => dts({ ...options, tsgo: { moduleUrl } })).toThrow( |
| 55 | + 'tsgo.runExternalCode', |
| 56 | + ) |
| 57 | +}) |
| 58 | + |
| 59 | +test('content mappers reject VFS', () => { |
| 60 | + expect(() => |
| 61 | + dts({ ...options, tsgo: { ...options.tsgo, vfs: true } }), |
| 62 | + ).toThrow('tsgo.vfs') |
| 63 | +}) |
| 64 | + |
| 65 | +test('reports a missing mapper package', async () => { |
| 66 | + await writeFile( |
| 67 | + path.join(root, 'base.json'), |
| 68 | + JSON.stringify({ |
| 69 | + contentMappers: [ |
| 70 | + { package: 'nonexistent-content-mapper', extensions: ['.component'] }, |
| 71 | + ], |
| 72 | + }), |
| 73 | + ) |
| 74 | + await expect( |
| 75 | + rolldownBuild(path.join(root, 'index.ts'), [dts(options)]), |
| 76 | + ).rejects.toThrow('could not be resolved') |
| 77 | +}) |
| 78 | + |
| 79 | +test('reports mapper failures', async () => { |
| 80 | + await writeFile(path.join(root, 'Example.component'), 'MAPPER_ERROR') |
| 81 | + await expect( |
| 82 | + rolldownBuild(path.join(root, 'Example.component'), [dts(options)]), |
| 83 | + ).rejects.toThrow('failed to transform this file') |
| 84 | +}) |
| 85 | + |
| 86 | +test('declaration maps point to the original mapper source', async () => { |
| 87 | + const { chunks } = await rolldownBuild( |
| 88 | + path.join(root, 'Example.component'), |
| 89 | + [dts(options)], |
| 90 | + {}, |
| 91 | + { dir: path.join(root, 'dist') }, |
| 92 | + ) |
| 93 | + const map = findSourceMapChunk(chunks, 'Example.component.d.ts.map') |
| 94 | + expect(map.sources).toEqual(['../Example.component']) |
| 95 | + expect(map.mappings).not.toBe('') |
| 96 | +}) |
| 97 | + |
| 98 | +test('rebuilds mapped sources from disk', async () => { |
| 99 | + const plugins = [dts(options)] |
| 100 | + const entry = path.join(root, 'Example.component') |
| 101 | + const first = await rolldownBuild(entry, plugins) |
| 102 | + expect(first.snapshot).toContain('name: string') |
| 103 | + await writeFile( |
| 104 | + entry, |
| 105 | + 'export default function greet(count: number): number { return count }', |
| 106 | + ) |
| 107 | + const rebuilt = await rolldownBuild(entry, plugins) |
| 108 | + expect(rebuilt.snapshot).toContain('count: number') |
| 109 | + expect(rebuilt.snapshot).not.toContain('name: string') |
| 110 | +}) |
0 commit comments