Skip to content

Commit 43033c2

Browse files
committed
Initial commit
0 parents  commit 43033c2

34 files changed

Lines changed: 3197 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup Bun
17+
uses: oven-sh/setup-bun@v1
18+
with:
19+
bun-version: latest
20+
21+
- name: Install dependencies
22+
run: bun install
23+
24+
- name: Build Next.js test app
25+
run: |
26+
cd test-next-app
27+
bun run build
28+
29+
- name: Build CLI
30+
run: bun run build
31+
32+
- name: Run tests
33+
run: bun test

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

AGENTS.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
Default to using Bun instead of Node.js.
3+
4+
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
5+
- Use `bun test` instead of `jest` or `vitest`
6+
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
7+
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
8+
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
9+
- Use `bunx <package> <command>` instead of `npx <package> <command>`
10+
- Bun automatically loads .env, so don't use dotenv.
11+
12+
## APIs
13+
14+
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
15+
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
16+
- `Bun.redis` for Redis. Don't use `ioredis`.
17+
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
18+
- `WebSocket` is built-in. Don't use `ws`.
19+
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
20+
- Bun.$`ls` instead of execa.
21+
22+
## Testing
23+
24+
Use `bun test` to run tests.
25+
26+
```ts#index.test.ts
27+
import { test, expect } from "bun:test";
28+
29+
test("hello world", () => {
30+
expect(1).toBe(1);
31+
});
32+
```
33+
34+
## Frontend
35+
36+
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
37+
38+
Server:
39+
40+
```ts#index.ts
41+
import index from "./index.html"
42+
43+
Bun.serve({
44+
routes: {
45+
"/": index,
46+
"/api/users/:id": {
47+
GET: (req) => {
48+
return new Response(JSON.stringify({ id: req.params.id }));
49+
},
50+
},
51+
},
52+
// optional websocket support
53+
websocket: {
54+
open: (ws) => {
55+
ws.send("Hello, world!");
56+
},
57+
message: (ws, message) => {
58+
ws.send(message);
59+
},
60+
close: (ws) => {
61+
// handle close
62+
}
63+
},
64+
development: {
65+
hmr: true,
66+
console: true,
67+
}
68+
})
69+
```
70+
71+
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
72+
73+
```html#index.html
74+
<html>
75+
<body>
76+
<h1>Hello, world!</h1>
77+
<script type="module" src="./frontend.tsx"></script>
78+
</body>
79+
</html>
80+
```
81+
82+
With the following `frontend.tsx`:
83+
84+
```tsx#frontend.tsx
85+
import React from "react";
86+
import { createRoot } from "react-dom/client";
87+
88+
// import .css files directly and it works
89+
import './index.css';
90+
91+
const root = createRoot(document.body);
92+
93+
export default function Frontend() {
94+
return <h1>Hello, world!</h1>;
95+
}
96+
97+
root.render(<Frontend />);
98+
```
99+
100+
Then, run index.ts
101+
102+
```sh
103+
bun --hot ./index.ts
104+
```
105+
106+
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.mdx`.

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Next.js Single HTML CLI 🚀
2+
3+
A powerful CLI tool that transforms a Next.js static export into a **completely self-contained, single HTML file** with hash-based routing. Regex based, zero deps.
4+
## 📖 How it Works
5+
6+
The tool crawls your `out/` directory, extracts all routes, and bundles them into a single file. It inlines all assets (JS, CSS, Fonts, Images) as Data URIs (base64) and injects a robust hash-based router.
7+
8+
### 🏗 Architecture
9+
10+
```mermaid
11+
graph TD
12+
A[Next.js App] -->|next build| B[out/ Directory]
13+
B -->|Parser| C[Asset Map \u0026 Routes]
14+
C -->|Inliner| D[Data URIs \u0026 CSS/JS Bundles]
15+
D -->|Bundler| E[Single index.html]
16+
F[Hash Router Shim] -->|Injected| E
17+
18+
subgraph "Single HTML File"
19+
E
20+
end
21+
22+
E -->|Browser| G[Hash-based Navigation]
23+
G -->|#/about| H[DOM Swap via ROUTE_MAP]
24+
```
25+
26+
## 🛠 Features
27+
28+
- **Self-Contained**: Zero external dependencies. Fonts, images, and scripts are all inlined.
29+
- **Hash Routing**: Automatically converts path navigation to `#/hash` navigation.
30+
- **Next.js Compatibility**: Supports latest Next.js features like Geist fonts and Turbopack.
31+
- **Robust Escaping**: Uses Base64 encoding for the internal route map to prevent minified JS syntax errors.
32+
- **Shims**: Automatically shims `document.currentScript` and other browser APIs that Next.js expects.
33+
34+
## 🚀 Usage
35+
36+
### 1. Generate Static Export
37+
Ensure your `next.config.js` has `output: 'export'`:
38+
39+
```javascript
40+
/** @type {import('next').NextConfig} */
41+
const nextConfig = {
42+
output: 'export',
43+
};
44+
module.exports = nextConfig;
45+
```
46+
47+
Then build:
48+
```bash
49+
npm run build
50+
# or
51+
bun run build
52+
```
53+
54+
### 2. Run the Inliner
55+
```bash
56+
# you need bun installed (can be run by npm tho)
57+
bunx next-single-file --input out --output dist/index.html
58+
# or npm, needs bun installed
59+
npx next-single-file --input out --output dist/index.html
60+
61+
```
62+
63+
## 🎯 Use Cases
64+
65+
- **Portable Demos**: Send a fully functional web app as a single email attachment.
66+
- **Offline Documentation**: Create rich, interactive docs that work without an internet connection.
67+
- **Embedded UIs**: Embed a Next.js interface into desktop applications or hardware dashboards.
68+
- **Simple Hosting**: Host a multi-page app on GitHub Gists or any basic file server.
69+
70+
## 🧪 Development
71+
72+
To run the tests:
73+
```bash
74+
bun test
75+
```

0 commit comments

Comments
 (0)