Giter Club home page Giter Club logo

bun-auto-import's Introduction

Bun Plugin Auto Import

Auto import support for Bun, powered by Unimport.

Please consider give it a star if you like it. Thank you!

Install

bun add bun-auto-import@latest -d

And then add the following code to the beginning of your entry file.

import { plugin } from "bun"

plugin(
    (await import("bun-auto-import")).autoImport({
        // Options
        // For example:
        presets: ["solid-js"],
        imports: [{ name: "z", from: "zod" }],

        // The generated .d.ts file path
        // Default: `./auto-import.d.ts`
        dts: `./src/auto-import.d.ts`,
    })
)

See more options at Unimport's configuration documentation.

After that, you can use the amazing Auto Import in other files (expect for your bun entry file).

For example:

// index.ts
import { plugin } from "bun"
import { handler } from "./server"

plugin(
	(await import("bun-auto-import")).autoImport({
		imports: [{ name: "z", from: "zod" }],
	})
)

Bun.serve({
	fetch: handler,
	port: 3000,
})

// server.ts
// `z` is auto imported from zod
const Body = z.object({
	msg: z.string(),
})

export const handler = async (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 })
	}
}

Then run bun index.ts and curl http://localhost:3000/ --data "{\"msg\":\"Hello Auto Import\"}".

bun-auto-import's People

Contributors

arstnei0 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.