Skip to content

jesseagleboy/Bun_HTML_Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Bun_HTML_Server

A simple demonstration of Bun's HTML server capabilities with React and Svelte components integration.

Netlify Status

πŸ“‹ Overview

This project demonstrates Bun's ability to serve HTML files directly as a server using the command bun index.html. It explores the integration of both React and Svelte components within this simple server setup, along with state management using Preact's Signals.

πŸš€ Getting Started

Prerequisites

  • Bun installed (v1.2.5+)

Installation

# Clone the repository
git clone https://github.com/yourusername/Bun_HTML_Server.git
cd Bun_HTML_Server

# Install dependencies
bun install

Running the Server

# Development mode
bun dev

# Build for production
bun build

# Preview production build
bun preview

🎯 Project Goals

The main objectives of this project are:

  1. Test Bun's HTML Server Feature: Explore the simplicity of creating a server using just HTML files with bun index.html as described in the Bun HTML documentation.

  2. Component Integration: Test the integration of both React (already supported) and Svelte (newly supported in Bun 1.2.5) components.

  3. Shared State Management: Experiment with using Preact's Signals (specifically @preact/signals-react) as a shared state management solution that works in both React and Svelte components:

    • Use a single signal instance as the source of truth across different component frameworks
    • Demonstrate that @preact/signals-react can be imported and used effectively in Svelte components
    • Achieve real-time synchronization between React and Svelte components using the same signal
  4. Persistence: Implement local storage to maintain state between page navigations and browser sessions.

πŸ“ Key Features

  • Multi-framework Integration: Seamlessly integrates React and Svelte components in the same application
  • Shared State: Uses Preact's Signals for state management across different component frameworks
  • Modern Styling: Implements Tailwind CSS with plugins for forms, typography, and DaisyUI components
  • Optimized Build Process: Configured for development and production builds
  • Local Storage Persistence: Maintains state between page navigations

🧩 Project Structure

Bun_HTML_Server/
β”œβ”€β”€ src/                        # Source code directory
β”‚   β”œβ”€β”€ react/                  # React components
β”‚   β”‚   β”œβ”€β”€ index.tsx           # Main React component
β”‚   β”‚   └── another.tsx         # Secondary page React component
β”‚   β”œβ”€β”€ svelte/                 # Svelte components
β”‚   β”‚   β”œβ”€β”€ App.svelte          # Main Svelte component
β”‚   β”‚   └── svelte.ts           # Svelte mounting code
β”‚   β”œβ”€β”€ stylesheet/             # CSS styles
β”‚   β”‚   └── styles.css          # Main stylesheet with Tailwind imports
β”‚   β”œβ”€β”€ store.ts                # Shared state management
β”‚   β”œβ”€β”€ index.html              # Main HTML entry point
β”‚   └── another-page.html       # Secondary page HTML
β”œβ”€β”€ build.ts                    # Build configuration
β”œβ”€β”€ bunfig.toml                 # Bun configuration
β”œβ”€β”€ package.json                # Project dependencies and scripts
β”œβ”€β”€ tsconfig.json               # TypeScript configuration
└── README.md                   # Project documentation

πŸ› οΈ Technologies Used

  • Bun: All-in-one JavaScript runtime and toolkit
  • React 19: Library for building user interfaces
  • Svelte 5: Component framework with a new reactive primitive ($state)
  • Preact Signals: Fine-grained reactivity system
  • Tailwind CSS: Utility-first CSS framework
  • DaisyUI: Component library for Tailwind CSS
  • TypeScript: Typed JavaScript

πŸ” Implementation Details

State Management

The application uses Preact's Signals from the @preact/signals-react package to maintain state between React and Svelte components:

// src/store.ts
import {signal} from "@preact/signals-react";

export const count = signal(Number(localStorage.getItem("count") || 0));

Local Storage Persistence

The counter value is persisted in localStorage, allowing the state to be maintained between page navigations and browser refreshes:

// Example from React component
onClick={() => {
  localStorage.setItem("count", `${count.value + 1}`);
  count.value = Number(localStorage.getItem("count") || 0);
}}

Build Configuration

The project is configured for development and production builds using Bun's build API with plugins for Tailwind and Svelte:

// build.ts
import bunPluginTailwind from "bun-plugin-tailwind"
import bunPluginSvelte from "bun-plugin-svelte"

Bun.build({
  entrypoints: ["src/index.html", "src/another-page.html"],
  outdir: "./dist",
  minify: true,
  splitting: true,
  sourcemap: "linked",
  // ...other options
  plugins: [bunPluginTailwind, bunPluginSvelte],
});

πŸ“š Learn More

πŸ™ Acknowledgements

Thanks to the following tools and libraries that made this project possible:

  • Bun - The all-in-one JavaScript runtime and toolkit
  • React - The library for web and native user interfaces
  • Svelte - Cybernetically enhanced web apps
  • Preact - Fast 3kB alternative to React with the same modern API
  • Tailwind CSS - A utility-first CSS framework
  • DaisyUI - The most popular component library for Tailwind CSS

πŸ“„ License

MIT


This README was written with assistance from Claude, Anthropic's AI assistant.

About

Test a Bun HTML Server with React and Svelte

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors