@spirobel/mininext 0.5.1 → 0.6.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.
@@ -2,11 +2,11 @@
2
2
  /// <reference types="bun-types" />
3
3
  import { url, Mini, has, type HtmlHandler } from "./url";
4
4
  import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html } from "./html";
5
- import type { Server, WebSocketHandler } from "bun";
5
+ import { type Server, type WebSocketHandler } from "bun";
6
6
  declare global {
7
7
  var PROJECT_ROOT: string | undefined;
8
8
  }
9
- declare function build(backendPath?: string): Promise<void>;
9
+ declare function build(backendPath?: string): Promise<0 | undefined>;
10
10
  declare const standardDevReloader: BasedHtml;
11
11
  declare function makeEntrypoint(): Promise<{
12
12
  fetch: (req: Request, server: Server) => Promise<Response>;
package/dist/mininext.js CHANGED
@@ -1,11 +1,17 @@
1
1
  import { url, Mini, has } from "./url";
2
2
  import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html, } from "./html";
3
+ import { $ } from "bun";
3
4
  import { watch } from "fs/promises";
4
5
  import * as path from "path";
5
6
  function projectRoot() {
6
7
  return global.PROJECT_ROOT || import.meta.dir + "/../../../../";
7
8
  }
8
9
  async function build(backendPath = "backend/backend.ts") {
10
+ if (Bun.argv[2] === "frontend") {
11
+ const newFrontend = await buildFrontend(Bun.argv[3]);
12
+ process.stdout.write(JSON.stringify(newFrontend));
13
+ return 0;
14
+ }
9
15
  await buildBackend(backendPath);
10
16
  if (Bun.argv[2] === "dev") {
11
17
  await devServer();
@@ -70,18 +76,29 @@ async function buildBackend(backendPath = "backend/backend.ts") {
70
76
  const frontEndPath = (await Bun.file(firstPlaceToLook).exists())
71
77
  ? firstPlaceToLook
72
78
  : secondPlaceToLook;
73
- const f = await buildFrontend(frontEndPath);
74
- FrontendScriptUrls.push("/" + f.url);
75
- FrontendScripts.push(f.script);
79
+ try {
80
+ const f = await $ `bun run build.ts frontend ${frontEndPath}`.json();
81
+ FrontendScriptUrls.push("/" + f.url);
82
+ FrontendScripts.push(f.script);
83
+ }
84
+ catch (error) {
85
+ console.log(await $ `bun run build.ts frontend ${frontEndPath}`.text());
86
+ }
76
87
  }
77
- for (const svgPath of url.getSvgPaths()) {
78
- const parsedSvgPath = path.parse(svgPath);
79
- const svgContent = Bun.file(path.join(projectRoot() + "/backend/", svgPath));
88
+ for (const svg of url.getSvgs()) {
89
+ const firstPlaceToLook = path.resolve(path.dirname(svg.callerPath), `svgs/${svg.svgFilePath}`);
90
+ const secondPlaceToLook = path.resolve(projectRoot(), `${svg.svgFilePath}`);
91
+ const svgResolvedFilePath = (await Bun.file(firstPlaceToLook).exists())
92
+ ? firstPlaceToLook
93
+ : secondPlaceToLook;
94
+ const parsedSvgPath = path.parse(svgResolvedFilePath);
95
+ const svgContent = Bun.file(svgResolvedFilePath);
80
96
  const svgHash = Bun.hash(await svgContent.arrayBuffer());
81
97
  const svgUrl = `/${parsedSvgPath.name}-${svgHash}.svg`;
82
98
  bundledSVGs[svgUrl] = {
83
99
  svgContent: await svgContent.text(),
84
- svgPath,
100
+ svgFilePath: svg.svgFilePath,
101
+ options: svg.options,
85
102
  };
86
103
  }
87
104
  const res = await Bun.build({
package/dist/url.d.ts CHANGED
@@ -78,7 +78,8 @@ declare global {
78
78
  var FrontendScriptUrls: Array<string>;
79
79
  var bundledSVGs: Record<string, {
80
80
  svgContent: string;
81
- svgPath: string;
81
+ svgFilePath: string;
82
+ options: ResponseInit;
82
83
  }>;
83
84
  }
84
85
  export type ScriptTag = (...params: any[]) => Promise<HtmlString>;
@@ -91,7 +92,20 @@ export declare class url {
91
92
  static direct_handlers_html: Map<string, HtmlHandler>;
92
93
  private static frontends;
93
94
  private static svgs;
94
- static svg(path: string, options?: ResponseInit): string | undefined;
95
+ /**
96
+ * This function takes care of bundling your svg (icons?) into the webapp
97
+ * they will have a hash in the name to break the cache when needed
98
+ * @param svgFilePath first place to look: svgs folder in the same path the calling file, after that path from project root.
99
+ * @param options ResponseInit, default headers for an svg
100
+ * @returns url to the svg
101
+ */
102
+ static svg(svgFilePath: string, options?: ResponseInit): string | undefined;
103
+ /**
104
+ * this function helps you build frontends with any kind of framework (no framework at all) and get the bundle
105
+ * @param path first place to look: frontend folder in the same path the calling file, after that /frontend path from project root.
106
+ * @param snippet this is handy to pass in a piece of html that often goes along with a certain frontend
107
+ * @returns a html script element with the bundled frontend as the src
108
+ */
95
109
  static frontend<X>(path: string, snippet?: BasedHtml): HtmlString;
96
110
  static frontend<X>(path: string, snippet?: HtmlHandler<X>): (mini: Mini<X>) => HtmlString;
97
111
  /**
@@ -101,7 +115,11 @@ export declare class url {
101
115
  path: string;
102
116
  callerPath: string;
103
117
  }[];
104
- static getSvgPaths(): string[];
118
+ static getSvgs(): {
119
+ svgFilePath: string;
120
+ callerPath: string;
121
+ options: ResponseInit;
122
+ }[];
105
123
  static serveFrontend(req: Request): Response | undefined;
106
124
  static serveSvg(req: Request): Response | undefined;
107
125
  /**
package/dist/url.js CHANGED
@@ -71,16 +71,28 @@ export class url {
71
71
  static direct_handlers_html = new Map();
72
72
  // An array of the uncompiled frontend files, example frontends[0] = "index.tsx" -> frontend/index.tsx (from the project root)
73
73
  static frontends = [];
74
- static svgs = new Map();
75
- static svg(path, options = {
74
+ static svgs = [];
75
+ /**
76
+ * This function takes care of bundling your svg (icons?) into the webapp
77
+ * they will have a hash in the name to break the cache when needed
78
+ * @param svgFilePath first place to look: svgs folder in the same path the calling file, after that path from project root.
79
+ * @param options ResponseInit, default headers for an svg
80
+ * @returns url to the svg
81
+ */
82
+ static svg(svgFilePath, options = {
76
83
  headers: {
77
84
  "Content-Type": "image/svg+xml",
78
85
  "Content-Disposition": "attachment",
79
86
  },
80
87
  }) {
81
- url.svgs.set(path, options);
82
- var foundEntry = Object.entries(bundledSVGs).find(([key, value]) => value.svgPath === path);
83
- return foundEntry && foundEntry[0];
88
+ const stack = new Error().stack?.split("\n");
89
+ let callerPath = "";
90
+ if (stack) {
91
+ callerPath = stack[2].slice(stack[2].lastIndexOf("(") + 1, stack[2].lastIndexOf(".") + 3);
92
+ }
93
+ url.svgs.push({ svgFilePath, callerPath, options });
94
+ var foundSvg = Object.entries(bundledSVGs).find(([key, value]) => value.svgFilePath === svgFilePath);
95
+ return foundSvg && foundSvg[0];
84
96
  }
85
97
  static frontend(path, snippet) {
86
98
  const stack = new Error().stack?.split("\n");
@@ -105,8 +117,8 @@ export class url {
105
117
  static getFrontends() {
106
118
  return url.frontends;
107
119
  }
108
- static getSvgPaths() {
109
- return [...url.svgs.keys()];
120
+ static getSvgs() {
121
+ return url.svgs;
110
122
  }
111
123
  static serveFrontend(req) {
112
124
  const reqPath = new URL(req.url).pathname;
@@ -123,7 +135,7 @@ export class url {
123
135
  const reqPath = new URL(req.url).pathname;
124
136
  const resolvedSvg = bundledSVGs[reqPath];
125
137
  if (resolvedSvg) {
126
- return new Response(resolvedSvg.svgContent, url.svgs.get(resolvedSvg.svgPath));
138
+ return new Response(resolvedSvg.svgContent, resolvedSvg.options);
127
139
  }
128
140
  }
129
141
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spirobel/mininext",
3
- "license" : "MIT" ,
3
+ "license": "MIT",
4
4
  "module": "dist/mininext/mininext.js",
5
5
  "type": "module",
6
6
  "main": "dist/mininext.js",
@@ -8,10 +8,10 @@
8
8
  "scripts": {
9
9
  "publish": "bun run build && npm publish",
10
10
  "build": "tsc",
11
- "clean":"rm -rf ./dist"
11
+ "clean": "rm -rf ./dist"
12
12
  },
13
13
  "files": ["dist"],
14
- "version": "0.5.1",
14
+ "version": "0.6.1",
15
15
  "devDependencies": {
16
16
  "@types/bun": "latest"
17
17
  },