Skip to content

Commit

Permalink
fix: monorepo problems
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Sep 25, 2024
1 parent e4099ca commit e518b30
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface DtsOptions {
/**
* The output directory of the declaration files. Please note,
* it is relative to the current working directory.
* @default 'dist/types'
* @default 'dist'
*/
outdir?: ts.CompilerOptions['outDir'] // sadly, the bundler uses `outdir` instead of `outDir` and to avoid confusion, we'll use `outdir` here

Expand All @@ -68,65 +68,54 @@ export interface DtsOptions {
* @param options The options for generating the declaration files.
*/
export async function generate(entryPoints: string | string[], options?: DtsOptions): Promise<void> {
const path = p.resolve(options?.tsconfigPath ?? 'tsconfig.json')
const cwd = options?.cwd ?? process.cwd()
const configPath = options?.tsconfigPath ?? p.resolve(cwd, 'tsconfig.json')
const root = (options?.root ?? 'src').replace(/^\.\//, '')

console.log('TSConfig path:', configPath)
console.log('Root directory:', root)

try {
const configFile = ts.readConfigFile(path, ts.sys.readFile)
const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
if (configFile.error) {
throw new Error(`Failed to read tsconfig: ${configFile.error.messageText}`)
}

const cwd = options?.cwd ?? process.cwd()
const base = options?.base ?? cwd
const rootDir = p.resolve(cwd, root)

const parsedCommandLine = ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd)

if (parsedCommandLine.errors.length) {
throw new Error(`Failed to parse tsconfig: ${parsedCommandLine.errors.map((e) => e.messageText).join(', ')}`)
}

const outDir = p.resolve(cwd, options?.outdir || parsedCommandLine.options.outDir || 'dist')

const compilerOptions: ts.CompilerOptions = {
...parsedCommandLine.options,
...options?.compiler,
declaration: true,
emitDeclarationOnly: true,
noEmit: false,
declarationMap: true,
outDir: outDir,
rootDir: rootDir,
incremental: false, // Disable incremental compilation
outDir: options?.outdir ?? './dist',
rootDir: p.resolve(cwd, root),
}

const host = ts.createCompilerHost(compilerOptions)
console.log('Compiler Options:', JSON.stringify(compilerOptions, null, 2))

// Filter entry points to only include files within the current package
const filteredEntryPoints = (Array.isArray(entryPoints) ? entryPoints : [entryPoints]).filter((entryPoint) => {
const relativePath = p.relative(cwd, entryPoint)
return !relativePath.startsWith('..') && !p.isAbsolute(relativePath)
})

if (filteredEntryPoints.length === 0) {
console.warn('No valid entry points found within the current package.')
return
}
const host = ts.createCompilerHost(compilerOptions)

const program = ts.createProgram({
rootNames: filteredEntryPoints,
rootNames: parsedCommandLine.fileNames.filter((file) => file.startsWith(compilerOptions.rootDir ?? 'src')),
options: compilerOptions,
host,
})

const emitResult = program.emit(undefined, (fileName, data) => {
if (fileName.endsWith('.d.ts') || fileName.endsWith('.d.ts.map')) {
const outputPath = p.join(outDir, p.relative(rootDir, fileName))
const outputPath = p.join(compilerOptions.outDir ?? './dist', p.relative(p.resolve(cwd, root), fileName))
const dir = p.dirname(outputPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
fs.writeFileSync(outputPath, data)
console.log('Emitted:', outputPath)
}
})

Expand Down Expand Up @@ -166,8 +155,9 @@ export function dts(options?: DtsOptions): BunPlugin {
await generate(entrypoints, {
root,
include: entrypoints,
outdir: options?.outdir || build.config.outdir,
cwd: options?.cwd || process.cwd(),
tsconfigPath: options?.tsconfigPath,
outdir: options?.outdir || build.config.outdir,
...options,
})
},
Expand Down

0 comments on commit e518b30

Please sign in to comment.