GitHub - stacksjs/bun-plugin-auto-imports: Auto Imports support for Bun. · GitHub
Skip to content

About

Auto Imports support for Bun.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

11 stars

Watchers

1 watching

Forks

Repository files navigation

Social Card of Bun Plugin Auto Imports

npm version GitHub Actions Commitizen friendly npm downloads

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-imports

You 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, AutoImportsOptions proxies UnimportOptions.

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 test

Changelog

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:

Discussions on GitHub

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 💙

About

Auto Imports support for Bun.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

11 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages