-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdbb721
commit 0691934
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { PluginBuilder } from 'bun' | ||
import type { UnimportOptions } from 'unimport' | ||
|
||
function getLoader(path: string): string { | ||
return path.endsWith('ts') | ||
? 'ts' | ||
: path.endsWith('js') | ||
? 'js' | ||
: path.endsWith('tsx') | ||
? 'tsx' | ||
: 'jsx' | ||
} | ||
|
||
interface AutoImportsPlugin { | ||
name: string | ||
setup: (builder: PluginBuilder) => Promise<void> | ||
} | ||
|
||
export function autoImports(options: Partial<UnimportOptions & { dts: string }>): AutoImportsPlugin { | ||
return { | ||
name: 'bun-plugin-auto-imports', | ||
|
||
async setup(builder: PluginBuilder): Promise<void> { | ||
const { createUnimport } = await import('unimport') | ||
const { injectImports, generateTypeDeclarations } = createUnimport({ | ||
...options, | ||
dts: undefined, | ||
} as UnimportOptions) | ||
|
||
const dtsContent = await generateTypeDeclarations() | ||
Bun.write(options.dts ?? './auto-import.d.ts', dtsContent) | ||
|
||
builder.onLoad({ filter: /.*/ }, async (args) => { | ||
const fileContent = await Bun.file(args.path).text() | ||
const transformedFileContent = await injectImports(fileContent) | ||
|
||
return { | ||
contents: transformedFileContent.code, | ||
loader: getLoader(args.path), | ||
} | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
export type AutoImportsOptions = UnimportOptions | ||
|
||
export default autoImports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { PluginBuilder } from 'bun'; | ||
import type { UnimportOptions } from 'unimport'; | ||
|
||
declare function getLoader(path: string): string; | ||
declare interface AutoImportsPlugin { | ||
name: string | ||
setup: (builder: PluginBuilder) => Promise<void> | ||
} | ||
export declare function autoImports(options: Partial<UnimportOptions & { dts: string }>): AutoImportsPlugin; | ||
export declare type AutoImportsOptions = UnimportOptions | ||
|
||
export default autoImports; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters