A simple demonstration of Bun's HTML server capabilities with React and Svelte components integration.
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.
# Clone the repository
git clone https://github.com/yourusername/Bun_HTML_Server.git
cd Bun_HTML_Server
# Install dependencies
bun install
# Development mode
bun dev
# Build for production
bun build
# Preview production build
bun preview
The main objectives of this project are:
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.
Component Integration: Test the integration of both React (already supported) and Svelte (newly supported in Bun 1.2.5) components.
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:
@preact/signals-react
can be imported and used effectively in Svelte componentsPersistence: Implement local storage to maintain state between page navigations and browser sessions.
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
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));
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);
}}
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],
});
Thanks to the following tools and libraries that made this project possible:
This README was written with assistance from Claude, Anthropic's AI assistant.