Skip to content

Commit

Permalink
feat: add proper monorepo support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Sep 25, 2024
1 parent c6e4274 commit 3ca01df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This Bun plugin generates dts files for your TypeScript project.

- Automatic dts generation based on your entrypoints
- Honors & inherits your `tsconfig.json` settings
- Monorepo support

## Usage

Expand Down
7 changes: 1 addition & 6 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import process from 'node:process'
import dts from './src/index'

// eslint-disable-next-line no-console
console.log('Building...')

const result = await Bun.build({
entrypoints: ['src/index.ts'],
outdir: 'dist',
target: 'bun',

plugins: [
dts({
cwd: import.meta.dir,
}),
],
plugins: [dts()],
})

if (!result.success) {
Expand Down
36 changes: 20 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
const base = options?.base ?? cwd
const rootDir = `${cwd}/${root}`

// Merge the base tsconfig with the package-specific one
const mergedConfig = {
...configJson,
compilerOptions: {
...configJson.compilerOptions,
...options?.compiler,
},
}

const opts: TsOptions = {
base,
baseUrl: base,
Expand All @@ -87,10 +96,14 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
...(options?.include && { include: options.include }),
}

const parsedConfig = ts.parseJsonConfigFileContent(configJson, ts.sys, cwd, opts, path)
const parsedConfig = ts.parseJsonConfigFileContent(mergedConfig, ts.sys, cwd, opts, path)
parsedConfig.options.emitDeclarationOnly = true
// console.log('Root directory:', rootDir)
// console.log('Output directory:', parsedConfig.options.outDir)

// Use the outdir from options if provided, otherwise use the one from tsconfig
const outDir = options?.outdir || parsedConfig.options.outDir || 'dist'

// Ensure outDir is relative to the package root, not the monorepo root
parsedConfig.options.outDir = p.resolve(cwd, outDir)

const host = ts.createCompilerHost(parsedConfig.options)

Expand All @@ -102,10 +115,7 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
if ('isDeclarationFile' in sourceFile) {
const originalFileName = sourceFile.fileName
const entryPointName = p.basename(originalFileName, '.ts')
// console.log('originalFileName', originalFileName)
// console.log('entryPointName', entryPointName)
const newFileName = p.join(parsedConfig.options.outDir || 'dist', `${entryPointName}.d.ts`)
// console.log('newFileName', newFileName)

return ts.factory.updateSourceFile(
sourceFile,
Expand All @@ -123,27 +133,20 @@ export async function generate(entryPoints: string | string[], options?: DtsOpti
],
}

// console.log('Parsed config:', JSON.stringify(parsedConfig.options, null, 2))

const program = ts.createProgram({
rootNames: Array.isArray(entryPoints) ? entryPoints : [entryPoints],
options: parsedConfig.options,
host,
})

// console.log('Program created with root names:', program.getRootFileNames())

// const emitResult = program.emit(
program.emit(
undefined,
(fileName, data) => {
if (fileName.endsWith('.d.ts')) {
// console.log('Emitting declaration file:', fileName)
const outputPath = p.join(parsedConfig.options.outDir || 'dist', p.relative(rootDir, fileName))
// console.log('Attempting to write file:', outputPath)
try {
ts.sys.writeFile(outputPath, data)
// console.log('Successfully wrote file:', outputPath)
console.log('Successfully wrote file:', outputPath)
} catch (error) {
console.error('Error writing file:', outputPath, error)
}
Expand All @@ -169,12 +172,13 @@ export function dts(options?: DtsOptions): BunPlugin {

async setup(build) {
const entrypoints = [...build.config.entrypoints].sort()
const root = build.config.root ?? 'src'
const root = options?.root ?? build.config.root ?? 'src'

await generate(entrypoints, {
root,
include: entrypoints,
outdir: build.config.outdir,
outdir: options?.outdir || build.config.outdir,
cwd: options?.cwd || process.cwd(),
...options,
})
},
Expand Down

0 comments on commit 3ca01df

Please sign in to comment.