diff --git a/dts.config.ts b/dts.config.ts index 71bad2d..2ba9a34 100644 --- a/dts.config.ts +++ b/dts.config.ts @@ -1,7 +1,7 @@ import type { DtsGenerationOption } from './src/types' const config: DtsGenerationOption = { - cwd: './', + cwd: __dirname, root: './src', entrypoints: ['**/*.ts'], outdir: './dist', diff --git a/fixtures/input/example/0001.ts b/fixtures/input/example/0001.ts new file mode 100644 index 0000000..7386a69 --- /dev/null +++ b/fixtures/input/example/0001.ts @@ -0,0 +1,24 @@ +import { resolve } from 'node:path' +import process from 'node:process' +import { deepMerge } from './utils' + +export interface ConfigOptions { + name: string + cwd?: string + defaultConfig: T +} + +export async function loadConfig>({ name, cwd, defaultConfig }: ConfigOptions): Promise { + const c = cwd ?? process.cwd() + const configPath = resolve(c, `${name}.config`) + + try { + const importedConfig = await import(configPath) + const loadedConfig = importedConfig.default || importedConfig + return deepMerge(defaultConfig, loadedConfig) + } + catch (error) { + console.error(`Error loading config from ${configPath}:`, error) + return defaultConfig + } +} diff --git a/fixtures/input/example/0002.ts b/fixtures/input/example/0002.ts new file mode 100644 index 0000000..2b29540 --- /dev/null +++ b/fixtures/input/example/0002.ts @@ -0,0 +1,37 @@ +import type { DtsGenerationOption } from '@stacksjs/dtsx' +import type { BunPlugin } from 'bun' +import process from 'node:process' +import { generate } from '@stacksjs/dtsx' + +export function dts(options?: DtsGenerationOption): BunPlugin { + return { + name: 'bun-plugin-dtsx', + + async setup(build) { + const cwd = options?.cwd ?? process.cwd() + const root = options?.root ?? build.config.root + const entrypoints = options?.entrypoints ?? build.config.entrypoints + const outdir = options?.outdir ?? build.config.outdir + // const keepComments = options?.keepComments ?? true + const clean = options?.clean ?? false + const tsconfigPath = options?.tsconfigPath ?? './tsconfig.json' + + await generate({ + ...options, + cwd, + root, + entrypoints, + outdir, + // keepComments, + clean, + tsconfigPath, + }) + }, + } +} + +export { generate } + +export type { DtsGenerationOption } + +export default dts diff --git a/fixtures/output/0001.d.ts b/fixtures/output/0001.d.ts new file mode 100644 index 0000000..bdbced1 --- /dev/null +++ b/fixtures/output/0001.d.ts @@ -0,0 +1,6 @@ +export declare interface ConfigOptions { + name: string + cwd?: string + defaultConfig: T +} +export declare function loadConfig>({ name, cwd, defaultConfig }: ConfigOptions): Promise; \ No newline at end of file diff --git a/fixtures/output/0002.d.ts b/fixtures/output/0002.d.ts new file mode 100644 index 0000000..da76b29 --- /dev/null +++ b/fixtures/output/0002.d.ts @@ -0,0 +1,9 @@ +import type { BunPlugin } from 'bun'; +import type { DtsGenerationOption } from '@stacksjs/dtsx'; +import { generate } from '@stacksjs/dtsx'; + +export { generate } +export type { DtsGenerationOption }; +export declare function dts(options?: DtsGenerationOption): BunPlugin; + +export default dts; \ No newline at end of file diff --git a/fixtures/output/example-0001.d.ts b/fixtures/output/example-0001.d.ts new file mode 100644 index 0000000..bdbced1 --- /dev/null +++ b/fixtures/output/example-0001.d.ts @@ -0,0 +1,6 @@ +export declare interface ConfigOptions { + name: string + cwd?: string + defaultConfig: T +} +export declare function loadConfig>({ name, cwd, defaultConfig }: ConfigOptions): Promise; \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index f4f8439..a4eaed3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,5 @@ import type { DtsGenerationConfig } from './types' +import { resolve } from 'node:path' import process from 'node:process' // @ts-expect-error - types are missing for now import { loadConfig } from 'bun-config' @@ -7,7 +8,7 @@ import { loadConfig } from 'bun-config' // eslint-disable-next-line antfu/no-top-level-await export const config: DtsGenerationConfig = await loadConfig({ name: 'dts', - cwd: process.cwd(), + cwd: resolve('./', __dirname.includes('node_modules') ? '../../..' : '..'), defaultConfig: { cwd: process.cwd(), root: './src',