Skip to content

Commit 0e996ab

Browse files
committed
chore: add two examples
1 parent de495ee commit 0e996ab

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

dts.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DtsGenerationOption } from './src/types'
22

33
const config: DtsGenerationOption = {
4-
cwd: './',
4+
cwd: __dirname,
55
root: './src',
66
entrypoints: ['**/*.ts'],
77
outdir: './dist',

fixtures/input/example/0001.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { resolve } from 'node:path'
2+
import process from 'node:process'
3+
import { deepMerge } from './utils'
4+
5+
export interface ConfigOptions<T> {
6+
name: string
7+
cwd?: string
8+
defaultConfig: T
9+
}
10+
11+
export async function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T> {
12+
const c = cwd ?? process.cwd()
13+
const configPath = resolve(c, `${name}.config`)
14+
15+
try {
16+
const importedConfig = await import(configPath)
17+
const loadedConfig = importedConfig.default || importedConfig
18+
return deepMerge(defaultConfig, loadedConfig)
19+
}
20+
catch (error) {
21+
console.error(`Error loading config from ${configPath}:`, error)
22+
return defaultConfig
23+
}
24+
}

fixtures/input/example/0002.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { DtsGenerationOption } from '@stacksjs/dtsx'
2+
import type { BunPlugin } from 'bun'
3+
import process from 'node:process'
4+
import { generate } from '@stacksjs/dtsx'
5+
6+
export function dts(options?: DtsGenerationOption): BunPlugin {
7+
return {
8+
name: 'bun-plugin-dtsx',
9+
10+
async setup(build) {
11+
const cwd = options?.cwd ?? process.cwd()
12+
const root = options?.root ?? build.config.root
13+
const entrypoints = options?.entrypoints ?? build.config.entrypoints
14+
const outdir = options?.outdir ?? build.config.outdir
15+
// const keepComments = options?.keepComments ?? true
16+
const clean = options?.clean ?? false
17+
const tsconfigPath = options?.tsconfigPath ?? './tsconfig.json'
18+
19+
await generate({
20+
...options,
21+
cwd,
22+
root,
23+
entrypoints,
24+
outdir,
25+
// keepComments,
26+
clean,
27+
tsconfigPath,
28+
})
29+
},
30+
}
31+
}
32+
33+
export { generate }
34+
35+
export type { DtsGenerationOption }
36+
37+
export default dts

fixtures/output/0001.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export declare interface ConfigOptions<T> {
2+
name: string
3+
cwd?: string
4+
defaultConfig: T
5+
}
6+
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T>;

fixtures/output/0002.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { BunPlugin } from 'bun';
2+
import type { DtsGenerationOption } from '@stacksjs/dtsx';
3+
import { generate } from '@stacksjs/dtsx';
4+
5+
export { generate }
6+
export type { DtsGenerationOption };
7+
export declare function dts(options?: DtsGenerationOption): BunPlugin;
8+
9+
export default dts;

fixtures/output/example-0001.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export declare interface ConfigOptions<T> {
2+
name: string
3+
cwd?: string
4+
defaultConfig: T
5+
}
6+
export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: ConfigOptions<T>): Promise<T>;

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DtsGenerationConfig } from './types'
2+
import { resolve } from 'node:path'
23
import process from 'node:process'
34
// @ts-expect-error - types are missing for now
45
import { loadConfig } from 'bun-config'
@@ -7,7 +8,7 @@ import { loadConfig } from 'bun-config'
78
// eslint-disable-next-line antfu/no-top-level-await
89
export const config: DtsGenerationConfig = await loadConfig({
910
name: 'dts',
10-
cwd: process.cwd(),
11+
cwd: resolve('./', __dirname.includes('node_modules') ? '../../..' : '..'),
1112
defaultConfig: {
1213
cwd: process.cwd(),
1314
root: './src',

0 commit comments

Comments
 (0)