htmv 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -0
- package/cli.ts +2 -0
- package/index.ts +87 -0
- package/package.json +20 -0
- package/tsconfig.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# HTMV
|
|
2
|
+
Welcome to HTMV. A simple yet fast web framework currently in work in progress. Installation and usage guide can be found below.
|
|
3
|
+
|
|
4
|
+
# Precautions
|
|
5
|
+
HTMV was made to be used with [Bun](https://bun.com/). It has not been tested and is not guaranted to work on Node.js. Please ensure you have Bun installed before continuing reading.
|
|
6
|
+
|
|
7
|
+
# Installation
|
|
8
|
+
## Linking the library
|
|
9
|
+
It's not yet uploaded on NPM so you'll have to link it yourself to your project.
|
|
10
|
+
1. Get the repo
|
|
11
|
+
```bash
|
|
12
|
+
$ git clone https://github.com/Fabrisdev/htmv.git
|
|
13
|
+
```
|
|
14
|
+
2. cd into it
|
|
15
|
+
```bash
|
|
16
|
+
$ cd htmv
|
|
17
|
+
```
|
|
18
|
+
3. Add it to your local registry of packages
|
|
19
|
+
```bash
|
|
20
|
+
$ bun link
|
|
21
|
+
```
|
|
22
|
+
Setup of the library is finished! 🎉 Now onto creating your first **HTMV** project!
|
|
23
|
+
## Creating your first project
|
|
24
|
+
1. Leave that folder and create a new folder for your project
|
|
25
|
+
```bash
|
|
26
|
+
$ cd .. # or cd into wherever you'd like to make your project's folder
|
|
27
|
+
$ mkdir my-first-htmv-project
|
|
28
|
+
```
|
|
29
|
+
2. Create an empty bun project and add HTMV as a dependency
|
|
30
|
+
```bash
|
|
31
|
+
bun init # when prompted for the type, choose EMPTY
|
|
32
|
+
bun link htmv # Adds HTMV dependency
|
|
33
|
+
```
|
|
34
|
+
3. Create two folders. One for routes and the other for views (You can choose any name)
|
|
35
|
+
4. Now in the `index.ts` add this
|
|
36
|
+
```ts
|
|
37
|
+
import path from "node:path";
|
|
38
|
+
import { setup } from "htmv";
|
|
39
|
+
|
|
40
|
+
const dirPath = import.meta.dir;
|
|
41
|
+
setup({
|
|
42
|
+
routes: path.join(dirPath, "routes"), // Change to your routes folder name
|
|
43
|
+
views: path.join(dirPath, "views"), // Change to your views folder name
|
|
44
|
+
port: 3000,
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
5. Let's create your first view! Inside the views folder add a new `example.html` with the contents
|
|
48
|
+
```html
|
|
49
|
+
<h1>Welcome to my blog!</h1>
|
|
50
|
+
<p>{description}</p>
|
|
51
|
+
```
|
|
52
|
+
6. Finally, let's create a route for it. HTMV has file based routing. Every folder inside your `routes` folder indicates a nested route and every `index.ts` is it's entry point. For now, let's keep it simple with just one `index.ts` in it. Create the file and add the next contents
|
|
53
|
+
```ts
|
|
54
|
+
import { type RouteParams, render } from "htmv";
|
|
55
|
+
|
|
56
|
+
export default async function MyRoute(_params: RouteParams) {
|
|
57
|
+
return await render("example", {
|
|
58
|
+
description: "This is my first time using HTMV ❤️",
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## And that's it! We're done.
|
|
64
|
+
Now let's try it! You can simply start it with `bun run index.ts`. After that, you should now be able to see your page in `http://localhost:3000`.
|
|
65
|
+
|
|
66
|
+
## Final note
|
|
67
|
+
Did you see how the `{description}` value on our view changed to the one we gave it on our route? Now that's where HTMV gets fun! Just as we now did you could also do more complex stuff like access your DB's data and show it in a nicely form.
|
|
68
|
+
|
|
69
|
+
# Static files
|
|
70
|
+
Having your views is nice and all... but what about images? videos? or maybe you would like to not have to use `<style>` for CSS and `<script>` for your JS.
|
|
71
|
+
|
|
72
|
+
To fix this, there's actually a folder we haven't made yet. the `public` folder. Make it and just add it to your `setup`
|
|
73
|
+
```ts
|
|
74
|
+
setup({
|
|
75
|
+
//... the other folders
|
|
76
|
+
public: path.join(dirPath, 'public')
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
You can add any **static files** onto this folder and they will be automatically served at `/public`. Take that into account when importing them inside your `view`.
|
|
80
|
+
|
|
81
|
+
# Dynamic routes
|
|
82
|
+
Sometimes you don't know the exact route (this is more common in `APIs`). For example, let's say you want to have a route `/api/user/USER_ID_HERE`. Of course you don't want to have a million folders every single one named with a different user id. That's where dynamic routes come into play.
|
|
83
|
+
|
|
84
|
+
Let's setup one. Just rename your file to `/api/user/:id`.
|
|
85
|
+
|
|
86
|
+
That's it! Now you can access it inside your route like this:
|
|
87
|
+
```ts
|
|
88
|
+
import { type RouteParams } from "htmv";
|
|
89
|
+
|
|
90
|
+
export default function UserEndpoint(routeParams: RouteParams) {
|
|
91
|
+
const { id } = routeParams.params
|
|
92
|
+
return `Hello user ${id}!`
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
Now when you go to `/api/user/1000` you should see `Hello user 1000!`.
|
|
96
|
+
|
|
97
|
+
# Hot reloading
|
|
98
|
+
Having to restart the server every time you make a change can be quite tedious. You can instead serve your server with `bun --watch .` to watch for any changes to the folder. Note that this does not include hot reloading in the browser. As of now, you have to refresh the page to see new changes. It doesn't update in real time.
|
|
99
|
+
|
|
100
|
+
# Recommendations
|
|
101
|
+
Creating a run script is advised. You can easily add it to your `package.json` like this
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
//...
|
|
105
|
+
"scripts": {
|
|
106
|
+
"dev": "bun --watch .",
|
|
107
|
+
"start": "bun run index.ts"
|
|
108
|
+
}
|
|
109
|
+
//...
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
Now you can start your server with hot reloading by running `bun dev` or normally by running `bun start`.
|
package/cli.ts
ADDED
package/index.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import staticPlugin from "@elysiajs/static";
|
|
4
|
+
import { Elysia } from "elysia";
|
|
5
|
+
|
|
6
|
+
let viewsPath = "";
|
|
7
|
+
|
|
8
|
+
export async function view(view: string, props: Record<string, unknown>) {
|
|
9
|
+
if (viewsPath === "")
|
|
10
|
+
throw new Error(
|
|
11
|
+
"Views folder path not yet configured. Use `Htmv.setup` before rendering a view.",
|
|
12
|
+
);
|
|
13
|
+
const file = Bun.file(path.join(viewsPath, `${view}.html`));
|
|
14
|
+
const code = await file.text();
|
|
15
|
+
const replacedCode = code.replace(/{(.+)}/g, (_, propName) => {
|
|
16
|
+
return props[propName] as string;
|
|
17
|
+
});
|
|
18
|
+
return new Response(replacedCode, {
|
|
19
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type RouteParams = {
|
|
24
|
+
query: Record<string, string>;
|
|
25
|
+
request: Request;
|
|
26
|
+
params: Record<string, string>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type Paths = {
|
|
30
|
+
routes: string;
|
|
31
|
+
views: string;
|
|
32
|
+
public: string;
|
|
33
|
+
port: number;
|
|
34
|
+
};
|
|
35
|
+
export async function setup(paths: Paths) {
|
|
36
|
+
viewsPath = paths.views;
|
|
37
|
+
const app = new Elysia().use(
|
|
38
|
+
staticPlugin({
|
|
39
|
+
assets: paths.public,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
await registerRoutes(app, paths.routes);
|
|
44
|
+
app.listen(paths.port);
|
|
45
|
+
console.log("");
|
|
46
|
+
console.log(`HTMV running on port ${paths.port}! 🎉`);
|
|
47
|
+
console.log(`http://localhost:${paths.port}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function registerRoutes(app: Elysia, baseDir: string, prefix = "/") {
|
|
51
|
+
const entries = await fs.readdir(baseDir, { withFileTypes: true });
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
const fullPath = path.join(baseDir, entry.name);
|
|
54
|
+
if (entry.isDirectory()) {
|
|
55
|
+
await registerRoutes(app, fullPath, path.join(prefix, entry.name));
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (entry.name !== "index.ts" && entry.name !== "index.tsx") continue;
|
|
59
|
+
const module = (await import(fullPath)) as Record<string, unknown>;
|
|
60
|
+
const defaultFn = module.default;
|
|
61
|
+
if (defaultFn && typeof defaultFn === "function") {
|
|
62
|
+
app.all(prefix, async ({ request, query, params }) => {
|
|
63
|
+
const result = await defaultFn({ request, query, params });
|
|
64
|
+
return result;
|
|
65
|
+
});
|
|
66
|
+
console.log(`Registered ${fullPath} on ${prefix} route with method all`);
|
|
67
|
+
}
|
|
68
|
+
for (const propName in module) {
|
|
69
|
+
const prop = module[propName];
|
|
70
|
+
if (typeof prop !== "function") continue;
|
|
71
|
+
const fn = prop as RouteFn;
|
|
72
|
+
const name = fn.name.toLowerCase();
|
|
73
|
+
if (!["get", "post", "put", "patch", "delete"].includes(name)) continue;
|
|
74
|
+
app[name as "get"](prefix, async ({ request, query, params }) => {
|
|
75
|
+
const result = await fn({ request, query, params });
|
|
76
|
+
return result;
|
|
77
|
+
});
|
|
78
|
+
console.log(
|
|
79
|
+
`Registered ${fullPath} on ${prefix} route with method ${name}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type RouteFn = (
|
|
86
|
+
_: RouteParams,
|
|
87
|
+
) => Promise<Response> | Response | Promise<string> | string;
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "htmv",
|
|
3
|
+
"module": "index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@biomejs/biome": "2.3.3",
|
|
8
|
+
"@types/bun": "latest"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"typescript": "^5"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@elysiajs/static": "^1.4.6",
|
|
15
|
+
"elysia": "^1.4.15"
|
|
16
|
+
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"htmv": "./cli.ts"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
"noUncheckedIndexedAccess": true,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false
|
|
28
|
+
}
|
|
29
|
+
}
|