@spirobel/mininext 0.4.3 → 0.5.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.
@@ -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 declare function getCallerFilePath(): string;
15
16
  export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
package/dist/mininext.js CHANGED
@@ -47,13 +47,30 @@ const cryptoPlugin = {
47
47
  });
48
48
  },
49
49
  };
50
+ const nodeHttpsPlugin = {
51
+ name: "node https in the frontend",
52
+ setup(build) {
53
+ build.onResolve({ filter: /^https$/ }, (args) => {
54
+ const path_to_node_https_lib = path.resolve(projectRoot(), "node_modules/https-browserify/index.js");
55
+ if (path_to_node_https_lib)
56
+ return {
57
+ path: path_to_node_https_lib,
58
+ };
59
+ });
60
+ },
61
+ };
50
62
  async function buildBackend(backendPath = "backend/backend.ts") {
51
63
  global.FrontendScriptUrls = [];
52
64
  global.FrontendScripts = [];
53
65
  global.bundledSVGs = {};
54
66
  const i = await import(path.resolve(projectRoot(), backendPath));
55
67
  for (const frontend of url.getFrontends()) {
56
- const f = await buildFrontend(frontend);
68
+ const firstPlaceToLook = path.resolve(path.dirname(frontend.callerPath), `frontend/${frontend.path}`);
69
+ const secondPlaceToLook = path.resolve(projectRoot(), `frontend/${frontend.path}`);
70
+ const frontEndPath = (await Bun.file(firstPlaceToLook).exists())
71
+ ? firstPlaceToLook
72
+ : secondPlaceToLook;
73
+ const f = await buildFrontend(frontEndPath);
57
74
  FrontendScriptUrls.push("/" + f.url);
58
75
  FrontendScripts.push(f.script);
59
76
  }
@@ -82,12 +99,12 @@ async function buildBackend(backendPath = "backend/backend.ts") {
82
99
  }
83
100
  async function buildFrontend(file) {
84
101
  const result = await Bun.build({
85
- entrypoints: [path.resolve(projectRoot(), `frontend/${file}`)],
102
+ entrypoints: [file],
86
103
  outdir: path.resolve(projectRoot(), "dist"),
87
104
  naming: "[name]-[hash].[ext]",
88
105
  minify: Bun.argv[2] === "dev" ? false : true, //production
89
106
  target: "browser",
90
- plugins: [bufferPlugin, streamPlugin, cryptoPlugin],
107
+ plugins: [bufferPlugin, streamPlugin, cryptoPlugin, nodeHttpsPlugin],
91
108
  });
92
109
  if (!result?.outputs[0]?.path)
93
110
  console.log(result);
@@ -182,4 +199,14 @@ async function makeEntrypoint() {
182
199
  }
183
200
  return module.default();
184
201
  }
202
+ export function getCallerFilePath() {
203
+ // const stack = new Error().stack?.split("\n");
204
+ // //console.log(stack);
205
+ // if (!stack) return "";
206
+ // return stack[2].slice(
207
+ // stack[2].lastIndexOf("(") + 1,
208
+ // stack[2].lastIndexOf(")") + 3
209
+ // );
210
+ return __dirname;
211
+ }
185
212
  export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
package/dist/url.d.ts CHANGED
@@ -2,7 +2,7 @@
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 type { BasedHtml, DangerJsonInHtml, JsonString, JsonStringValues } from "./html";
5
+ import { BasedHtml, type DangerJsonInHtml, type JsonString, type JsonStringValues } from "./html";
6
6
  /**
7
7
  * A helper function that helps narrow unknown objects
8
8
  * @param object - the object of type unknown that is to be narrowed
@@ -92,11 +92,15 @@ export declare class url {
92
92
  private static frontends;
93
93
  private static svgs;
94
94
  static svg(path: string, options?: ResponseInit): string | undefined;
95
- static frontend(path: string, snippet?: HtmlHandler | BasedHtml): HtmlString;
95
+ static frontend<X>(path: string, snippet?: BasedHtml): HtmlString;
96
+ static frontend<X>(path: string, snippet?: HtmlHandler<X>): (mini: Mini<X>) => HtmlString;
96
97
  /**
97
98
  * This is used by the frontend bundler in order to find all frontends and their corresponding script files.
98
99
  */
99
- static getFrontends(): string[];
100
+ static getFrontends(): {
101
+ path: string;
102
+ callerPath: string;
103
+ }[];
100
104
  static getSvgPaths(): string[];
101
105
  static serveFrontend(req: Request): Response | undefined;
102
106
  static serveSvg(req: Request): Response | undefined;
package/dist/url.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { htmlResponder, html, json, dangerjson, HtmlString } from "./html";
2
+ import { BasedHtml, } from "./html";
2
3
  /**
3
4
  * A helper function that helps narrow unknown objects
4
5
  * @param object - the object of type unknown that is to be narrowed
@@ -82,10 +83,21 @@ export class url {
82
83
  return foundEntry && foundEntry[0];
83
84
  }
84
85
  static frontend(path, snippet) {
85
- const frontendIndex = url.frontends.push(path) - 1;
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;
86
92
  const scriptUrl = FrontendScriptUrls[frontendIndex];
87
- return html ` ${snippet}
88
- <script type="module" src="${scriptUrl}"></script>`; // return an html script tag with the index hash
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
+ };
89
101
  }
90
102
  /**
91
103
  * This is used by the frontend bundler in order to find all frontends and their corresponding script files.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "clean":"rm -rf ./dist"
12
12
  },
13
13
  "files": ["dist"],
14
- "version": "0.4.3",
14
+ "version": "0.5.1",
15
15
  "devDependencies": {
16
16
  "@types/bun": "latest"
17
17
  },