Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts. A Rolldown plugin to generate and bundle dts files.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145import { cp, mkdir, mkdtemp, realpath, rm, symlink, writeFile,} from 'node:fs/promises'import os from 'node:os'import path from 'node:path'import { rolldownBuild } from '@sxzz/test-utils'import { afterEach, beforeEach, describe, expect, test } from 'vitest'import { dts } from '../src/index.ts'import { findSourceMapChunk } from './utils.ts'
const fixture = path.resolve(import.meta.dirname, 'fixtures/content-mapper')const root = await realpath( await mkdtemp(path.join(os.tmpdir(), 'dts-content-mapper-')),)beforeEach(async () => { await cp(fixture, root, { recursive: true }) await mkdir(path.join(root, 'node_modules')) await symlink( path.join(root, 'mapper'), path.join(root, 'node_modules/test-content-mapper'), 'dir', )})afterEach(() => rm(root, { recursive: true, force: true }))describe.each([ ['tsgo', import.meta.resolve('typescript-next')], ['tsrs', import.meta.resolve('tsrs')],] as const)('%s', (compiler, moduleUrl) => { const native = { moduleUrl, runExternalCode: true } const options = { generator: compiler, tsconfig: path.join(root, 'tsconfig.json'), dtsOnly: true, [compiler]: native, }
test.each(['index.ts', 'Example.component'])( 'content mapper: %s', async (entry) => { const { chunks } = await rolldownBuild(path.join(root, entry), [ dts(options), ]) const declarations = chunks.filter((chunk) => chunk.type === 'chunk') expect(declarations.length).toBeGreaterThan(0) expect(declarations.map((chunk) => chunk.code).join('\n')).toContain( 'name: string', ) }, )
test('content mappers require external code permission', () => { expect(() => dts({ ...options, [compiler]: { moduleUrl } })).toThrow( `${compiler}.runExternalCode`, ) })
test('content mappers reject VFS', () => { expect(() => dts({ ...options, [compiler]: { ...native, vfs: true } }), ).toThrow(`${compiler}.vfs`) })
test('reports a missing mapper package', async () => { await writeFile( path.join(root, 'base.json'), JSON.stringify({ contentMappers: [ { package: 'nonexistent-content-mapper', extensions: ['.component'] }, ], }), ) await expect( rolldownBuild(path.join(root, 'index.ts'), [dts(options)]), ).rejects.toThrow('could not be resolved') })
test('reports mapper failures', async () => { await writeFile(path.join(root, 'Example.component'), 'MAPPER_ERROR') await expect( rolldownBuild(path.join(root, 'Example.component'), [dts(options)]), ).rejects.toThrow('failed to transform this file') })
test('reports mapper diagnostics even when their codes match TypeScript syntax errors', async () => { await writeFile( path.join(root, 'Example.component'), 'export const value = 1 // MAPPER_DIAGNOSTIC', ) await expect( rolldownBuild(path.join(root, 'Example.component'), [dts(options)]), ).rejects.toThrow('Test mapper diagnostic') })
test.each(['broken.ts', 'Broken.component'])( 'ignores unrelated syntax errors in %s', async (fileName) => { await writeFile(path.join(root, fileName), 'export const broken = ;') await writeFile( path.join(root, 'tsconfig.json'), JSON.stringify({ extends: './base.json', include: ['*.ts', '*.component'], }), ) const { snapshot } = await rolldownBuild( path.join(root, 'Example.component'), [dts(options)], ) expect(snapshot).toContain('name: string') }, )
test('declaration maps point to the original mapper source', async () => { const { chunks } = await rolldownBuild( path.join(root, 'Example.component'), [dts(options)], {}, { dir: path.join(root, 'dist') }, ) const map = findSourceMapChunk(chunks, 'Example.component.d.ts.map') expect(map.sources).toEqual(['../Example.component']) expect(map.mappings).not.toBe('') })
test('rebuilds mapped sources from disk', async () => { const plugins = [dts(options)] const entry = path.join(root, 'Example.component') const first = await rolldownBuild(entry, plugins) expect(first.snapshot).toContain('name: string') await writeFile( entry, 'export default function greet(count: number): number { return count }', ) const rebuilt = await rolldownBuild(entry, plugins) expect(rebuilt.snapshot).toContain('count: number') expect(rebuilt.snapshot).not.toContain('name: string') })})