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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176import path from 'node:path'import { fileURLToPath } from 'node:url'import { rolldownBuild } from '@sxzz/test-utils'import { expect, test } from 'vitest'import { dts } from '../src'
const dirname = path.dirname(fileURLToPath(import.meta.url))
test('basic', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/basic.ts'), [dts()], ) expect(snapshot).toMatchSnapshot()})
test('tsx', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/tsx.tsx'), [dts()], ) expect(snapshot).toMatchSnapshot()})
test('jsdoc', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/jsdoc.ts'), [dts({ isolatedDeclarations: false })], ) expect(snapshot).toMatchSnapshot()})
test('typescript compiler', async () => { const root = path.resolve(dirname, 'fixtures/tsc') const { snapshot } = await rolldownBuild( [path.resolve(root, 'entry1.ts'), path.resolve(root, 'entry2.ts')], [ { name: 'virtual-module', resolveId(id) { if (id === '/vfs') return '/vfs.ts' }, load(id) { if (id === '/vfs.ts') return `export const vfs = Math.random()` }, }, dts({ emitDtsOnly: true, compilerOptions: { skipLibCheck: true, isolatedDeclarations: false, }, isolatedDeclarations: false, }), ], ) expect(snapshot.replaceAll(/\/\/#region.*/g, '')).toMatchSnapshot()})
test('resolve dependencies', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/resolve-dep.ts'), [ dts({ resolve: ['oxc-transform'], isolatedDeclarations: true, emitDtsOnly: true, }), ], ) expect(snapshot).contain('interface TransformOptions')})
test('resolve dts', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/resolve-dts/index.ts'), [dts()], ) expect(snapshot).matchSnapshot()})
// Test alias mapping based on rolldown input optiontest('input alias', async () => { const root = path.resolve(dirname, 'fixtures/alias') const { snapshot, chunks } = await rolldownBuild( null!, [ dts({ emitDtsOnly: false, // Generate both JS and DTS files compilerOptions: {}, isolatedDeclarations: false, }), ], { cwd: root, // A mapping from output chunk names to input files. This mapping should // be used in both JS and DTS outputs. input: { output1: 'input1.ts', 'output2/index': 'input2.ts', }, }, ) const fileNames = chunks.map((chunk) => chunk.fileName).sort()
// The JS output and DTS output should have the same structure expect(fileNames).toContain('output1.d.ts') expect(fileNames).toContain('output1.js') expect(fileNames).toContain('output2/index.d.ts') expect(fileNames).toContain('output2/index.js')
expect(snapshot).toMatchSnapshot()})
test('isolated declaration error', async () => { const error = await rolldownBuild( path.resolve(dirname, 'fixtures/isolated-decl-error.ts'), [ dts({ emitDtsOnly: true, isolatedDeclarations: true, }), ], ).catch((error: any) => error) expect(String(error)).toContain( `Function must have an explicit return type annotation with --isolatedDeclarations.`, ) expect(String(error)).toContain(`export function fn() {`)})
test('paths', async () => { const root = path.resolve(dirname, 'fixtures/paths') const { snapshot } = await rolldownBuild( path.resolve(root, 'index.ts'), [ dts({ isolatedDeclarations: true, emitDtsOnly: true, }), ], { resolve: { tsconfigFilename: path.resolve(root, 'tsconfig.json'), }, }, ) expect(snapshot).toMatchSnapshot()})
test('tree-shaking', async () => { const { snapshot } = await rolldownBuild( path.resolve(dirname, 'fixtures/tree-shaking/index.ts'), [ dts(), { name: 'external-node', resolveId(id) { if (id.startsWith('node:')) return { id, external: true, moduleSideEffects: false } }, }, ], { treeshake: true }, ) expect(snapshot).matchSnapshot()})
test('dts input', async () => { const { snapshot } = await rolldownBuild(null!, [dts({ dtsInput: true })], { input: { index: path.resolve(dirname, 'fixtures/dts-input.d.ts'), }, }) expect(snapshot).toMatchSnapshot()})