Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export const fromTypes =
: ''

let distDir = join(tmpRoot, 'dist')
let rootDir = projectRoot

// Convert Windows path to Unix for TypeScript CLI
if (
Expand All @@ -269,27 +270,28 @@ export const fromTypes =
extendsRef = extendsRef.replace(/\\/g, '/')
src = src.replace(/\\/g, '/')
distDir = distDir.replace(/\\/g, '/')
rootDir = rootDir.replace(/\\/g, '/')
}

const resolvedCompilerOptions = {
lib: ['ESNext'],
module: 'ESNext',
noEmit: false,
declaration: true,
emitDeclarationOnly: true,
moduleResolution: 'bundler',
skipLibCheck: true,
skipDefaultLibCheck: true,
rootDir,
outDir: distDir,
...compilerOptions
}

fs.writeFileSync(
join(tmpRoot, 'tsconfig.json'),
`{
${extendsRef}
"compilerOptions": ${
compilerOptions
? JSON.stringify(compilerOptions)
: `{
"lib": ["ESNext"],
"module": "ESNext",
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
"moduleResolution": "bundler",
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"outDir": "${distDir}"
}`
},
"compilerOptions": ${JSON.stringify(resolvedCompilerOptions)},
"include": ["${src}"]
}`
)
Expand Down
40 changes: 40 additions & 0 deletions test/gen/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { describe, it, expect } from 'bun:test'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'

import { declarationToJSONSchema, fromTypes } from '../../src/gen'

Expand Down Expand Up @@ -648,4 +651,41 @@ describe('Gen > Type Gen', () => {
}
})
})

it('merge compilerOptions override with declaration defaults', () => {
const tmpRoot = mkdtempSync(join(tmpdir(), 'elysia-openapi-'))

try {
const reference = fromTypes('test/gen/sample.ts', {
tmpRoot,
debug: true,
silent: true,
compilerOptions: {
strict: true
}
})()

expect(serializable(reference)!).toBeDefined()

const tsconfig = JSON.parse(
readFileSync(join(tmpRoot, 'tsconfig.json'), 'utf8')
)

expect(tsconfig.compilerOptions).toMatchObject({
lib: ['ESNext'],
module: 'ESNext',
noEmit: false,
declaration: true,
emitDeclarationOnly: true,
moduleResolution: 'bundler',
skipLibCheck: true,
skipDefaultLibCheck: true,
rootDir: process.cwd(),
outDir: join(tmpRoot, 'dist'),
strict: true
})
} finally {
rmSync(tmpRoot, { recursive: true, force: true })
}
})
})