stacks/bun-plugin-auto-imports
publicClone
Push over the same URL. A password will not work: create a token under access tokens and use it in place of one.
Auto Imports support for Bun.
- .config
- .github
- .vscode
- docs
- src
- test
- .editorconfig 147 B
- .gitattributes 38 B
- .gitignore 107 B
- build.ts 424 B
- bun.lock 40.4 KB
- bunfig.toml 107 B
- CHANGELOG.md 24.7 KB
- CLAUDE.md 1.4 KB
- deps.yaml 32 B
- LICENSE.md 1.1 KB
- package.json 2.3 KB
- pantry.lock 261 B
- README.md 5.5 KB
- test.ts 491 B
- tsconfig.json 631 B

bun-plugin-auto-imports
This Bun plugin allows for support of auto-imports in your server-side code.
Features
- Auto-imports support for Bun
Usage
bun install -d bun-plugin-auto-importsYou may now use the plugin:
// index.ts
import type { AutoImportsOptions } from 'bun-plugin-auto-imports'
import { plugin } from 'bun'
import { autoImports } from 'bun-plugin-auto-imports'
const options: AutoImportsOptions = {
presets: ['solid-js'], // any unimport presets are valid
imports: [{ name: 'z', from: 'zod' }],
dirs: ['./src'],
dts: `./src/auto-import.d.ts`, // default is `./auto-import.d.ts`
}
plugin(autoImports(options))
Bun.serve({
fetch: handler,
port: 3000,
})In your "server file," you may now use the auto-imported modules:
// server.ts
// `z` is auto imported from zod
const Body = z.object({
msg: z.string(),
})
export async function handler(req: Request) {
try {
const body = await req.json()
const data = Body.parse(body)
return new Response(`Received: ${data.msg}`)
}
catch (e) {
return new Response('Invalid body', { status: 400 })
}
}[!NOTE] If you are familiar with
unimport,AutoImportsOptionsproxiesUnimportOptions.
Stable imports from packaged sources
Use moduleSpecifier(filePath, importerPath) when scanned files belong to a
published package. Return its public package specifier, including the source
extension when required by its exports. Return undefined for ordinary local
files to keep relative imports.
import { relative, sep } from 'node:path'
import { generateRuntimeIndex } from 'bun-plugin-auto-imports'
await generateRuntimeIndex([defaultsDir], outputPath, {
moduleSpecifier(filePath) {
const suffix = relative(defaultsDir, filePath)
if (suffix.startsWith(`..${sep}`) || suffix === '..') return undefined
return `@example/defaults/${suffix.split(sep).join('/')}`
},
})The same option is available on autoImports() for runtime imports and generated
declarations, and as the fourth argument to generateGlobalsScript(). This keeps
generated package imports stable when the consumer directory or install layout
changes. The package must export every returned specifier.
Export name collisions
Directory scans use the caller's directory order and sorted file paths. The first registration of an exported name wins, matching auto-import injection. Runtime indexes, their returned export metadata, globals scripts and global declarations use that same selection. Repeated overload signatures and re-exports do not emit duplicate names. Import a module explicitly when helpers in different modules share a name but have different meanings.
Testing
bun testChangelog
Please see our releases page for more information on what has changed recently.
Contributing
Please review the Contributing Guide for details.
Community
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
Postcardware
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where bun-plugin-auto-imports is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
Sponsors
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
Credits
Many thanks to the following core technologies & people who have contributed to this package:
License
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙
[codecov-href]: https://codecov.io/gh/stacksjs/bun-plugin-auto-imports -->