@spirobel/mininext 0.4.2 → 0.5.0
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/dist/mininext.d.ts +3 -2
- package/dist/mininext.js +19 -4
- package/dist/url.d.ts +21 -4
- package/dist/url.js +31 -4
- package/package.json +1 -1
package/dist/mininext.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
/// <reference types="bun-types" />
|
|
3
|
-
import { url, Mini, type HtmlHandler } from "./url";
|
|
3
|
+
import { url, Mini, has, type HtmlHandler } from "./url";
|
|
4
4
|
import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html } from "./html";
|
|
5
5
|
import type { Server, WebSocketHandler } from "bun";
|
|
6
6
|
declare global {
|
|
@@ -12,4 +12,5 @@ declare function makeEntrypoint(): Promise<{
|
|
|
12
12
|
fetch: (req: Request, server: Server) => Promise<Response>;
|
|
13
13
|
websocket: WebSocketHandler;
|
|
14
14
|
}>;
|
|
15
|
-
export
|
|
15
|
+
export declare function getCallerFilePath(): string;
|
|
16
|
+
export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
|
package/dist/mininext.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { url, Mini } from "./url";
|
|
1
|
+
import { url, Mini, has } from "./url";
|
|
2
2
|
import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html, } from "./html";
|
|
3
3
|
import { watch } from "fs/promises";
|
|
4
4
|
import * as path from "path";
|
|
@@ -53,7 +53,12 @@ async function buildBackend(backendPath = "backend/backend.ts") {
|
|
|
53
53
|
global.bundledSVGs = {};
|
|
54
54
|
const i = await import(path.resolve(projectRoot(), backendPath));
|
|
55
55
|
for (const frontend of url.getFrontends()) {
|
|
56
|
-
const
|
|
56
|
+
const firstPlaceToLook = path.resolve(path.dirname(frontend.callerPath), `frontend/${frontend.path}`);
|
|
57
|
+
const secondPlaceToLook = path.resolve(projectRoot(), `frontend/${frontend.path}`);
|
|
58
|
+
const frontEndPath = (await Bun.file(firstPlaceToLook).exists())
|
|
59
|
+
? firstPlaceToLook
|
|
60
|
+
: secondPlaceToLook;
|
|
61
|
+
const f = await buildFrontend(frontEndPath);
|
|
57
62
|
FrontendScriptUrls.push("/" + f.url);
|
|
58
63
|
FrontendScripts.push(f.script);
|
|
59
64
|
}
|
|
@@ -82,7 +87,7 @@ async function buildBackend(backendPath = "backend/backend.ts") {
|
|
|
82
87
|
}
|
|
83
88
|
async function buildFrontend(file) {
|
|
84
89
|
const result = await Bun.build({
|
|
85
|
-
entrypoints: [
|
|
90
|
+
entrypoints: [file],
|
|
86
91
|
outdir: path.resolve(projectRoot(), "dist"),
|
|
87
92
|
naming: "[name]-[hash].[ext]",
|
|
88
93
|
minify: Bun.argv[2] === "dev" ? false : true, //production
|
|
@@ -182,4 +187,14 @@ async function makeEntrypoint() {
|
|
|
182
187
|
}
|
|
183
188
|
return module.default();
|
|
184
189
|
}
|
|
185
|
-
export
|
|
190
|
+
export function getCallerFilePath() {
|
|
191
|
+
// const stack = new Error().stack?.split("\n");
|
|
192
|
+
// //console.log(stack);
|
|
193
|
+
// if (!stack) return "";
|
|
194
|
+
// return stack[2].slice(
|
|
195
|
+
// stack[2].lastIndexOf("(") + 1,
|
|
196
|
+
// stack[2].lastIndexOf(")") + 3
|
|
197
|
+
// );
|
|
198
|
+
return __dirname;
|
|
199
|
+
}
|
|
200
|
+
export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
|
package/dist/url.d.ts
CHANGED
|
@@ -2,12 +2,25 @@
|
|
|
2
2
|
/// <reference types="bun-types" />
|
|
3
3
|
import type { Server, WebSocketHandler } from "bun";
|
|
4
4
|
import { html, json, dangerjson, HtmlString } from "./html";
|
|
5
|
-
import
|
|
5
|
+
import { BasedHtml, type DangerJsonInHtml, type JsonString, type JsonStringValues } from "./html";
|
|
6
|
+
/**
|
|
7
|
+
* A helper function that helps narrow unknown objects
|
|
8
|
+
* @param object - the object of type unknown that is to be narrowed
|
|
9
|
+
* @param key - the key that may or may not exist in object
|
|
10
|
+
* @returns true if the key is present and false if not
|
|
11
|
+
* @example
|
|
12
|
+
* ``` js
|
|
13
|
+
* has(this.form.formJson, "formName") &&
|
|
14
|
+
* this.form.formJson.formName === this.form.formName
|
|
15
|
+
* ```
|
|
16
|
+
* https://stackoverflow.com/questions/70028907/narrowing-an-object-of-type-unknown
|
|
17
|
+
*/
|
|
18
|
+
export declare function has<T, K extends string>(object: T, key: K): object is T & object & Record<K, unknown>;
|
|
6
19
|
export type Form = {
|
|
7
20
|
post: boolean;
|
|
8
21
|
urlencoded: boolean;
|
|
9
22
|
multipart: boolean;
|
|
10
|
-
formJson?:
|
|
23
|
+
formJson?: unknown;
|
|
11
24
|
formData?: FormData;
|
|
12
25
|
formName?: string;
|
|
13
26
|
hiddenField?: HtmlString;
|
|
@@ -79,11 +92,15 @@ export declare class url {
|
|
|
79
92
|
private static frontends;
|
|
80
93
|
private static svgs;
|
|
81
94
|
static svg(path: string, options?: ResponseInit): string | undefined;
|
|
82
|
-
static frontend(path: string, snippet?:
|
|
95
|
+
static frontend<X>(path: string, snippet?: BasedHtml): HtmlString;
|
|
96
|
+
static frontend<X>(path: string, snippet?: HtmlHandler<X>): (mini: Mini<X>) => HtmlString;
|
|
83
97
|
/**
|
|
84
98
|
* This is used by the frontend bundler in order to find all frontends and their corresponding script files.
|
|
85
99
|
*/
|
|
86
|
-
static getFrontends():
|
|
100
|
+
static getFrontends(): {
|
|
101
|
+
path: string;
|
|
102
|
+
callerPath: string;
|
|
103
|
+
}[];
|
|
87
104
|
static getSvgPaths(): string[];
|
|
88
105
|
static serveFrontend(req: Request): Response | undefined;
|
|
89
106
|
static serveSvg(req: Request): Response | undefined;
|
package/dist/url.js
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
import { htmlResponder, html, json, dangerjson, HtmlString } from "./html";
|
|
2
|
+
import { BasedHtml, } from "./html";
|
|
3
|
+
/**
|
|
4
|
+
* A helper function that helps narrow unknown objects
|
|
5
|
+
* @param object - the object of type unknown that is to be narrowed
|
|
6
|
+
* @param key - the key that may or may not exist in object
|
|
7
|
+
* @returns true if the key is present and false if not
|
|
8
|
+
* @example
|
|
9
|
+
* ``` js
|
|
10
|
+
* has(this.form.formJson, "formName") &&
|
|
11
|
+
* this.form.formJson.formName === this.form.formName
|
|
12
|
+
* ```
|
|
13
|
+
* https://stackoverflow.com/questions/70028907/narrowing-an-object-of-type-unknown
|
|
14
|
+
*/
|
|
15
|
+
export function has(object, key) {
|
|
16
|
+
return typeof object === "object" && object !== null && key in object;
|
|
17
|
+
}
|
|
2
18
|
/**
|
|
3
19
|
* Mini - the data object can be filled with url.data
|
|
4
20
|
* @example
|
|
@@ -37,7 +53,7 @@ export class Mini {
|
|
|
37
53
|
this.form.formData.get("formName") === this.form.formName) {
|
|
38
54
|
return cb();
|
|
39
55
|
}
|
|
40
|
-
else if (this.form.formJson &&
|
|
56
|
+
else if (has(this.form.formJson, "formName") &&
|
|
41
57
|
this.form.formJson.formName === this.form.formName) {
|
|
42
58
|
return cb();
|
|
43
59
|
}
|
|
@@ -67,10 +83,21 @@ export class url {
|
|
|
67
83
|
return foundEntry && foundEntry[0];
|
|
68
84
|
}
|
|
69
85
|
static frontend(path, snippet) {
|
|
70
|
-
const
|
|
86
|
+
const stack = new Error().stack?.split("\n");
|
|
87
|
+
let callerPath = "";
|
|
88
|
+
if (stack) {
|
|
89
|
+
callerPath = stack[2].slice(stack[2].lastIndexOf("(") + 1, stack[2].lastIndexOf(".") + 3);
|
|
90
|
+
}
|
|
91
|
+
const frontendIndex = url.frontends.push({ path, callerPath }) - 1;
|
|
71
92
|
const scriptUrl = FrontendScriptUrls[frontendIndex];
|
|
72
|
-
|
|
73
|
-
|
|
93
|
+
if (snippet instanceof BasedHtml || !snippet) {
|
|
94
|
+
return html ` ${snippet}
|
|
95
|
+
<script type="module" src="${scriptUrl}"></script>`; // return an html script tag with the index hash
|
|
96
|
+
}
|
|
97
|
+
return (mini) => {
|
|
98
|
+
return mini.html `${snippet}
|
|
99
|
+
<script type="module" src="${scriptUrl}"></script>`;
|
|
100
|
+
};
|
|
74
101
|
}
|
|
75
102
|
/**
|
|
76
103
|
* This is used by the frontend bundler in order to find all frontends and their corresponding script files.
|