|
| 1 | +import { afterEach, describe, expect, test } from 'bun:test' |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { dirname, join } from 'node:path' |
| 4 | +import { build } from 'vite' |
| 5 | + |
| 6 | +const pluginEntry = join(import.meta.dir, '..', 'src', 'index.ts') |
| 7 | +const projects: string[] = [] |
| 8 | + |
| 9 | +/** A throwaway Vite project. It sits in this package so `@stacksjs/dtsx` resolves. */ |
| 10 | +function project(files: Record<string, string>): string { |
| 11 | + const dir = mkdtempSync(join(import.meta.dir, '.vite-project-')) |
| 12 | + projects.push(dir) |
| 13 | + for (const [file, text] of Object.entries(files)) { |
| 14 | + mkdirSync(dirname(join(dir, file)), { recursive: true }) |
| 15 | + writeFileSync(join(dir, file), text) |
| 16 | + } |
| 17 | + return dir |
| 18 | +} |
| 19 | + |
| 20 | +/** Runs `vite build` in the project, loading its `vite.config.ts`. */ |
| 21 | +async function viteBuild(dir: string): Promise<void> { |
| 22 | + await build({ root: dir, configFile: join(dir, 'vite.config.ts'), logLevel: 'silent' }) |
| 23 | +} |
| 24 | + |
| 25 | +function config(body: string): string { |
| 26 | + return `import { dts } from ${JSON.stringify(pluginEntry)}\nexport default ${body}\n` |
| 27 | +} |
| 28 | + |
| 29 | +function declarations(dir: string): string[] { |
| 30 | + return (readdirSync(dir, { recursive: true }) as string[]).filter(file => file.endsWith('.d.ts')).sort() |
| 31 | +} |
| 32 | + |
| 33 | +const library = { |
| 34 | + 'src/index.ts': `export { double } from './utils'\nexport const version = '1.0.0'\n`, |
| 35 | + 'src/utils.ts': `export function double(value: number): number { return value * 2 }\n`, |
| 36 | +} |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + for (const dir of projects.splice(0)) |
| 40 | + rmSync(dir, { recursive: true, force: true }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('vite build with @dtsx/vite-plugin', () => { |
| 44 | + test('a library build keeps declarations next to the bundle', async () => { |
| 45 | + const dir = project({ |
| 46 | + ...library, |
| 47 | + 'vite.config.ts': config(`{ plugins: [dts()], build: { lib: { entry: 'src/index.ts', formats: ['es'], fileName: 'index' } } }`), |
| 48 | + }) |
| 49 | + await viteBuild(dir) |
| 50 | + |
| 51 | + const dist = join(dir, 'dist') |
| 52 | + expect(existsSync(join(dist, 'index.js'))).toBe(true) |
| 53 | + expect(declarations(dist)).toEqual(['index.d.ts', 'utils.d.ts']) |
| 54 | + expect(readFileSync(join(dist, 'index.d.ts'), 'utf8')).toContain(`export declare const version: '1.0.0';`) |
| 55 | + expect(readFileSync(join(dist, 'utils.d.ts'), 'utf8')).toContain('export declare function double(value: number): number;') |
| 56 | + }) |
| 57 | + |
| 58 | + test('declarations from an earlier build are replaced, not kept by luck', async () => { |
| 59 | + const dir = project({ |
| 60 | + ...library, |
| 61 | + 'dist/stale.d.ts': 'export declare const stale: 1;\n', |
| 62 | + 'vite.config.ts': config(`{ plugins: [dts()], build: { lib: { entry: 'src/index.ts', formats: ['es'], fileName: 'index' } } }`), |
| 63 | + }) |
| 64 | + await viteBuild(dir) |
| 65 | + expect(declarations(join(dir, 'dist'))).toEqual(['index.d.ts', 'utils.d.ts']) |
| 66 | + }) |
| 67 | + |
| 68 | + test('a library entry at the project root does not declare the Vite config', async () => { |
| 69 | + const dir = project({ |
| 70 | + 'index.ts': `export const answer = 42\n`, |
| 71 | + 'vite.config.ts': config(`{ plugins: [dts()], build: { lib: { entry: 'index.ts', formats: ['es'], fileName: 'index' } } }`), |
| 72 | + }) |
| 73 | + await viteBuild(dir) |
| 74 | + expect(declarations(join(dir, 'dist'))).toEqual(['index.d.ts']) |
| 75 | + }) |
| 76 | + |
| 77 | + test('without a library entry the source root is src', async () => { |
| 78 | + const dir = project({ |
| 79 | + ...library, |
| 80 | + 'scripts/release.ts': `export const tool = true\n`, |
| 81 | + 'vite.config.ts': config(`{ plugins: [dts()], build: { rolldownOptions: { input: 'src/index.ts' } } }`), |
| 82 | + }) |
| 83 | + await viteBuild(dir) |
| 84 | + expect(declarations(join(dir, 'dist'))).toEqual(['index.d.ts', 'utils.d.ts']) |
| 85 | + }) |
| 86 | + |
| 87 | + test('several output formats generate once', async () => { |
| 88 | + const dir = project({ |
| 89 | + ...library, |
| 90 | + 'vite.config.ts': [ |
| 91 | + `import { dts } from ${JSON.stringify(pluginEntry)}`, |
| 92 | + `export default { plugins: [dts({ onSuccess: () => { (globalThis as any).__dtsxRuns += 1 } })], build: { lib: { entry: 'src/index.ts', formats: ['es', 'cjs'], fileName: 'index' } } }`, |
| 93 | + ].join('\n'), |
| 94 | + }) |
| 95 | + ;(globalThis as any).__dtsxRuns = 0 |
| 96 | + await viteBuild(dir) |
| 97 | + expect((globalThis as any).__dtsxRuns).toBe(1) |
| 98 | + expect(declarations(join(dir, 'dist'))).toEqual(['index.d.ts', 'utils.d.ts']) |
| 99 | + }) |
| 100 | + |
| 101 | + test('timing after generates once the bundle is written', async () => { |
| 102 | + const dir = project({ |
| 103 | + ...library, |
| 104 | + 'vite.config.ts': config(`{ plugins: [dts({ timing: 'after' })], build: { lib: { entry: 'src/index.ts', formats: ['es'], fileName: 'index' } } }`), |
| 105 | + }) |
| 106 | + await viteBuild(dir) |
| 107 | + expect(declarations(join(dir, 'dist'))).toEqual(['index.d.ts', 'utils.d.ts']) |
| 108 | + }) |
| 109 | + |
| 110 | + test('an explicit root and outdir are used as given', async () => { |
| 111 | + const dir = project({ |
| 112 | + ...library, |
| 113 | + 'vite.config.ts': config(`{ plugins: [dts({ root: '.', entrypoints: ['src/**/*.ts'], outdir: 'types' })], build: { lib: { entry: 'src/index.ts', formats: ['es'], fileName: 'index' } } }`), |
| 114 | + }) |
| 115 | + await viteBuild(dir) |
| 116 | + expect(declarations(join(dir, 'types'))).toEqual(['src/index.d.ts', 'src/utils.d.ts']) |
| 117 | + }) |
| 118 | +}) |
0 commit comments