Skip to content

Commit

Permalink
chore: add two examples
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 7, 2024
1 parent de495ee commit 0e996ab
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dts.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DtsGenerationOption } from './src/types'

const config: DtsGenerationOption = {
cwd: './',
cwd: __dirname,
root: './src',
entrypoints: ['**/*.ts'],
outdir: './dist',
Expand Down
24 changes: 24 additions & 0 deletions fixtures/input/example/0001.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { resolve } from 'node:path'
import process from 'node:process'
import { deepMerge } from './utils'

export interface ConfigOptions<T> {
name: string
cwd?: string
defaultConfig: T
}

export async function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T> {
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
}
}
37 changes: 37 additions & 0 deletions fixtures/input/example/0002.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions fixtures/output/0001.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare interface ConfigOptions<T> {
name: string
cwd?: string
defaultConfig: T
}
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T>;
9 changes: 9 additions & 0 deletions fixtures/output/0002.d.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare interface ConfigOptions<T> {
name: string
cwd?: string
defaultConfig: T
}
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T>;
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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',
Expand Down

0 comments on commit 0e996ab

Please sign in to comment.