Skip to content
/ buni Public

A custom plugin for Bun that enables importing modules from HTTP/HTTPS URLs, similar to Deno's approach. This plugin automatically detects the appropriate loader based on file extensions and supports TypeScript, JavaScript, and JSX files

Notifications You must be signed in to change notification settings

mew-sh/buni

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Buni - HTTP Imports Plugin for Bun

A custom plugin for Bun that enables importing modules from HTTP/HTTPS URLs, similar to Deno's approach. This plugin automatically detects the appropriate loader based on file extensions and supports TypeScript, JavaScript, and JSX files.

Features

Import from URLs: Support for importing modules from HTTP and HTTPS URLs
Auto Loader Detection: Automatically selects the correct loader based on file extension
TypeScript Support: Native support for .ts, .tsx, and .mts files
JavaScript Support: Compatible with .js, .jsx, and .mjs files
Relative & Absolute Paths: Supports relative (./, ../) and absolute (/) path resolution from URL imports
Bun Integration: Works seamlessly with bun build and bun run

Installation

mew install buni

Usage

CLI Usage (Recommended)

After installing globally, you can use the buni command directly:

# Run a local TypeScript file with HTTP imports
buni main.ts

# Run a remote script directly from URL
buni https://example.com/cli.ts --option1 --flag2

# Build with HTTP imports support
buni build main.ts --outdir ./dist

# Pass any Bun arguments
buni --watch main.ts
buni test

Remote Script Execution: The CLI automatically detects URLs (starting with http:// or https://) and downloads them to a temporary file before execution. This allows you to run scripts directly from the internet with HTTP imports support.

Manual Setup

Use the plugin with Bun's --preload flag:

bun --preload ./index.js main.ts

Example Usage

With CLI:

# Install globally first
mew install buni

# Run local files
buni main.ts

# Run remote scripts directly
buni https://deno.land/[email protected]/examples/chat/server.ts --port 8080

Manual:

bun --preload ./index.js main.ts

Example Imports

// Import from a TypeScript file
import { helper } from "https://example.com/utils.ts";

// Import from a JavaScript module
import { api } from "https://cdn.skypack.dev/some-package";

// Relative imports work too (when importing from URL modules)
import { config } from "./config.ts";
import { types } from "../types/index.ts";

Build with Plugin

# Build your project with HTTP imports support
bun --preload ./index.js build main.ts --outdir ./dist

How It Works

The plugin implements three main resolve handlers:

  1. Relative Path Resolution (./, ../): When importing from an HTTP module, resolves relative paths against the base URL
  2. Absolute Path Resolution (/): Resolves absolute paths against the origin of the HTTP module
  3. Direct HTTP URLs: Handles direct HTTP/HTTPS imports

Loader Selection

The plugin automatically selects the appropriate loader based on file extensions:

Extension Loader
.ts, .mts ts
.tsx tsx
.js, .mjs js
.jsx jsx
default js

Example Project Structure

buni/
├── index.js          # Your main entry file
├── buni              # Entrypoint
└── package.json

main.ts:

// Import from remote TypeScript module
import { createServer } from "https://deno.land/[email protected]/http/server.ts";
import { serve } from "https://deno.land/[email protected]/http/mod.ts";

console.log("Server created with remote imports!");

Run the project:

bun --preload ./index.js main.ts

Configuration

The plugin works out of the box with no configuration needed. It registers itself as a Bun plugin with the following settings:

  • Name: http_imports
  • Supported Protocols: HTTP and HTTPS
  • File Extensions: All common web file types (.ts, .tsx, .js, .jsx, .mjs, .mts)

Comparison with Deno

Feature Buni Plugin Deno
HTTP/HTTPS imports
TypeScript support
Auto loader detection
Relative path resolution
Build integration ✅ (via Bun)
Runtime Bun Deno

Error Handling

The plugin includes robust error handling:

  • Network Errors: Failed HTTP requests are properly reported with context
  • Invalid URLs: Malformed URLs are caught and reported
  • Missing Files: 404 errors include the failed URL for debugging

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Inspired by Deno's HTTP import system
  • Built for the Bun runtime and ecosystem
  • Thanks to the Bun team for the excellent plugin API

About

A custom plugin for Bun that enables importing modules from HTTP/HTTPS URLs, similar to Deno's approach. This plugin automatically detects the appropriate loader based on file extensions and supports TypeScript, JavaScript, and JSX files

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published