@spirobel/mininext 0.2.8 → 0.2.10

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/html.d.ts CHANGED
@@ -53,5 +53,5 @@ export declare function isError(submissionResult: any | {
53
53
  error: HtmlString;
54
54
  };
55
55
  declare global {
56
- var Reloader: string | undefined;
56
+ var Reloader: HtmlString | undefined;
57
57
  }
package/dist/html.js CHANGED
@@ -168,16 +168,18 @@ export async function htmlResponder(mini, maybeUnresolved, head = default_head,
168
168
  when you embed unescaped json elements in an html document.
169
169
  </div>`;
170
170
  }
171
- const definitelyResolved = await maybeUnresolved.resolve(mini);
172
- const flattend = definitelyResolved.flat(Infinity);
173
171
  if (!(maybeUnresolved instanceof JsonString)) {
174
- flattend.unshift(/*html*/ `<!DOCTYPE html>
172
+ const reloader = new HtmlString();
173
+ reloader.push(global.Reloader || "");
174
+ maybeUnresolved = html `<!DOCTYPE html>
175
175
  <html>
176
176
  <head>
177
- ${global.Reloader || ""} ${head}
177
+ ${reloader} ${head}
178
178
  </head>
179
179
  <body>
180
- `);
180
+ ${maybeUnresolved}
181
+ </body>
182
+ </html> `;
181
183
  }
182
184
  else {
183
185
  const headers = {
@@ -186,6 +188,8 @@ export async function htmlResponder(mini, maybeUnresolved, head = default_head,
186
188
  };
187
189
  options.headers = headers;
188
190
  }
191
+ const definitelyResolved = await maybeUnresolved.resolve(mini);
192
+ const flattend = definitelyResolved.flat(Infinity);
189
193
  async function* stepGen() {
190
194
  let index = 0;
191
195
  while (index < flattend.length) {
@@ -1,5 +1,8 @@
1
1
  import { url, Mini, type HtmlHandler } from "./url";
2
2
  import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
3
+ declare global {
4
+ var PROJECT_ROOT: string | undefined;
5
+ }
3
6
  declare function build(backendPath?: string): Promise<void>;
4
- declare const standardDevReloader = "\n <script>\n function reloader() {\n let socket = null;\n\n function connectWebSocket() {\n if (socket) {\n return;\n }\n socket = new WebSocket(\"ws://localhost:3001/reload\");\n\n socket.addEventListener(\"message\", (event) => {\n window.location.reload();\n });\n\n socket.addEventListener(\"close\", (event) => {\n // Reestablish the connection after 1 second\n socket = null;\n });\n\n socket.addEventListener(\"error\", (event) => {\n socket = null;\n });\n }\n connectWebSocket(); // connect to reloader, if it does not work:\n setInterval(connectWebSocket, 1000); // retry every 1 second\n }\n reloader();\n </script>\n ";
7
+ declare const standardDevReloader: HtmlString;
5
8
  export { url, html, head, build, isError, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
package/dist/mininext.js CHANGED
@@ -2,7 +2,9 @@ import { url, Mini } from "./url";
2
2
  import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
3
3
  import { watch } from "fs/promises";
4
4
  import * as path from "path";
5
- const PROJECT_ROOT = import.meta.dir + "/../../../../";
5
+ function projectRoot() {
6
+ return global.PROJECT_ROOT || import.meta.dir + "/../../../../";
7
+ }
6
8
  async function build(backendPath = "backend/backend.ts") {
7
9
  await buildBackend(backendPath);
8
10
  if (Bun.argv[2] === "dev") {
@@ -13,7 +15,7 @@ const myPlugin = {
13
15
  name: "node buffer in the frontend",
14
16
  setup(build) {
15
17
  build.onResolve({ filter: /^buffer$/ }, (args) => {
16
- const path_to_buffer_lib = path.resolve(PROJECT_ROOT, "node_modules/buffer/index.js");
18
+ const path_to_buffer_lib = path.resolve(projectRoot(), "node_modules/buffer/index.js");
17
19
  if (path_to_buffer_lib)
18
20
  return {
19
21
  path: path_to_buffer_lib,
@@ -25,7 +27,7 @@ async function buildBackend(backendPath = "backend/backend.ts") {
25
27
  global.FrontendScriptUrls = [];
26
28
  global.FrontendScripts = [];
27
29
  global.bundledSVGs = {};
28
- const i = await import(path.resolve(PROJECT_ROOT, backendPath));
30
+ const i = await import(path.resolve(projectRoot(), backendPath));
29
31
  for (const frontend of url.getFrontends()) {
30
32
  const f = await buildFrontend(frontend);
31
33
  FrontendScriptUrls.push("/" + f.url);
@@ -33,7 +35,7 @@ async function buildBackend(backendPath = "backend/backend.ts") {
33
35
  }
34
36
  for (const svgPath of url.getSvgPaths()) {
35
37
  const parsedSvgPath = path.parse(svgPath);
36
- const svgContent = Bun.file(path.join(PROJECT_ROOT + "/backend/", svgPath));
38
+ const svgContent = Bun.file(path.join(projectRoot() + "/backend/", svgPath));
37
39
  const svgHash = Bun.hash(await svgContent.arrayBuffer());
38
40
  const svgUrl = `/${parsedSvgPath.name}-${svgHash}.svg`;
39
41
  bundledSVGs[svgUrl] = {
@@ -42,8 +44,8 @@ async function buildBackend(backendPath = "backend/backend.ts") {
42
44
  };
43
45
  }
44
46
  const res = await Bun.build({
45
- entrypoints: [path.resolve(PROJECT_ROOT, backendPath)],
46
- outdir: path.resolve(PROJECT_ROOT, "dist"),
47
+ entrypoints: [path.resolve(projectRoot(), backendPath)],
48
+ outdir: path.resolve(projectRoot(), "dist"),
47
49
  naming: "backend.js",
48
50
  minify: Bun.argv[2] === "dev" ? false : true, //production
49
51
  target: "node",
@@ -56,8 +58,8 @@ async function buildBackend(backendPath = "backend/backend.ts") {
56
58
  }
57
59
  async function buildFrontend(file) {
58
60
  const result = await Bun.build({
59
- entrypoints: [path.resolve(PROJECT_ROOT, `frontend/${file}`)],
60
- outdir: path.resolve(PROJECT_ROOT, "dist"),
61
+ entrypoints: [path.resolve(projectRoot(), `frontend/${file}`)],
62
+ outdir: path.resolve(projectRoot(), "dist"),
61
63
  naming: "[name]-[hash].[ext]",
62
64
  minify: Bun.argv[2] === "dev" ? false : true, //production
63
65
  target: "browser",
@@ -95,7 +97,7 @@ async function devServer() {
95
97
  async function watchAndBuild(dir) {
96
98
  try {
97
99
  //start the file watcher that will rebuild frontend on save
98
- const watcher = watch(path.resolve(PROJECT_ROOT, dir), {
100
+ const watcher = watch(path.resolve(projectRoot(), dir), {
99
101
  recursive: true,
100
102
  });
101
103
  for await (const event of watcher) {
@@ -112,34 +114,34 @@ async function devServer() {
112
114
  watchAndBuild("frontend");
113
115
  watchAndBuild("backend");
114
116
  }
115
- const standardDevReloader = /*html*/ `
116
- <script>
117
- function reloader() {
118
- let socket = null;
117
+ const standardDevReloader = html `
118
+ <script>
119
+ function reloader() {
120
+ let socket = null;
119
121
 
120
- function connectWebSocket() {
121
- if (socket) {
122
- return;
123
- }
124
- socket = new WebSocket("ws://localhost:3001/reload");
122
+ function connectWebSocket() {
123
+ if (socket) {
124
+ return;
125
+ }
126
+ socket = new WebSocket("ws://localhost:3001/reload");
125
127
 
126
- socket.addEventListener("message", (event) => {
127
- window.location.reload();
128
- });
128
+ socket.addEventListener("message", (event) => {
129
+ window.location.reload();
130
+ });
129
131
 
130
- socket.addEventListener("close", (event) => {
131
- // Reestablish the connection after 1 second
132
- socket = null;
133
- });
132
+ socket.addEventListener("close", (event) => {
133
+ // Reestablish the connection after 1 second
134
+ socket = null;
135
+ });
134
136
 
135
- socket.addEventListener("error", (event) => {
136
- socket = null;
137
- });
138
- }
139
- connectWebSocket(); // connect to reloader, if it does not work:
140
- setInterval(connectWebSocket, 1000); // retry every 1 second
137
+ socket.addEventListener("error", (event) => {
138
+ socket = null;
139
+ });
141
140
  }
142
- reloader();
143
- </script>
144
- `;
141
+ connectWebSocket(); // connect to reloader, if it does not work:
142
+ setInterval(connectWebSocket, 1000); // retry every 1 second
143
+ }
144
+ reloader();
145
+ </script>
146
+ `;
145
147
  export { url, html, head, build, isError, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
package/mininext/html.ts CHANGED
@@ -216,16 +216,18 @@ export async function htmlResponder(
216
216
  when you embed unescaped json elements in an html document.
217
217
  </div>`;
218
218
  }
219
- const definitelyResolved = await maybeUnresolved.resolve(mini);
220
- const flattend = definitelyResolved.flat(Infinity);
221
219
  if (!(maybeUnresolved instanceof JsonString)) {
222
- flattend.unshift(/*html*/ `<!DOCTYPE html>
220
+ const reloader = new HtmlString();
221
+ reloader.push(global.Reloader || "");
222
+ maybeUnresolved = html`<!DOCTYPE html>
223
223
  <html>
224
224
  <head>
225
- ${global.Reloader || ""} ${head}
225
+ ${reloader} ${head}
226
226
  </head>
227
227
  <body>
228
- `);
228
+ ${maybeUnresolved}
229
+ </body>
230
+ </html> `;
229
231
  } else {
230
232
  const headers = {
231
233
  ...options.headers,
@@ -233,6 +235,8 @@ export async function htmlResponder(
233
235
  };
234
236
  options.headers = headers;
235
237
  }
238
+ const definitelyResolved = await maybeUnresolved.resolve(mini);
239
+ const flattend = definitelyResolved.flat(Infinity);
236
240
 
237
241
  async function* stepGen() {
238
242
  let index = 0;
@@ -263,5 +267,5 @@ export function isError(
263
267
  }
264
268
 
265
269
  declare global {
266
- var Reloader: string | undefined;
270
+ var Reloader: HtmlString | undefined;
267
271
  }
@@ -2,8 +2,12 @@ import { url, Mini, type HtmlHandler } from "./url";
2
2
  import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
3
3
  import { watch } from "fs/promises";
4
4
  import * as path from "path";
5
- const PROJECT_ROOT = import.meta.dir + "/../../../../";
6
-
5
+ function projectRoot() {
6
+ return global.PROJECT_ROOT || import.meta.dir + "/../../../../";
7
+ }
8
+ declare global {
9
+ var PROJECT_ROOT: string | undefined;
10
+ }
7
11
  async function build(backendPath: string = "backend/backend.ts") {
8
12
  await buildBackend(backendPath);
9
13
  if (Bun.argv[2] === "dev") {
@@ -17,7 +21,7 @@ const myPlugin: BunPlugin = {
17
21
  setup(build) {
18
22
  build.onResolve({ filter: /^buffer$/ }, (args) => {
19
23
  const path_to_buffer_lib = path.resolve(
20
- PROJECT_ROOT,
24
+ projectRoot(),
21
25
  "node_modules/buffer/index.js"
22
26
  );
23
27
  if (path_to_buffer_lib)
@@ -31,7 +35,7 @@ async function buildBackend(backendPath: string = "backend/backend.ts") {
31
35
  global.FrontendScriptUrls = [];
32
36
  global.FrontendScripts = [];
33
37
  global.bundledSVGs = {};
34
- const i = await import(path.resolve(PROJECT_ROOT, backendPath));
38
+ const i = await import(path.resolve(projectRoot(), backendPath));
35
39
 
36
40
  for (const frontend of url.getFrontends()) {
37
41
  const f = await buildFrontend(frontend);
@@ -40,7 +44,9 @@ async function buildBackend(backendPath: string = "backend/backend.ts") {
40
44
  }
41
45
  for (const svgPath of url.getSvgPaths()) {
42
46
  const parsedSvgPath = path.parse(svgPath);
43
- const svgContent = Bun.file(path.join(PROJECT_ROOT + "/backend/", svgPath));
47
+ const svgContent = Bun.file(
48
+ path.join(projectRoot() + "/backend/", svgPath)
49
+ );
44
50
  const svgHash = Bun.hash(await svgContent.arrayBuffer());
45
51
  const svgUrl = `/${parsedSvgPath.name}-${svgHash}.svg`;
46
52
  bundledSVGs[svgUrl] = {
@@ -49,8 +55,8 @@ async function buildBackend(backendPath: string = "backend/backend.ts") {
49
55
  };
50
56
  }
51
57
  const res = await Bun.build({
52
- entrypoints: [path.resolve(PROJECT_ROOT, backendPath)],
53
- outdir: path.resolve(PROJECT_ROOT, "dist"),
58
+ entrypoints: [path.resolve(projectRoot(), backendPath)],
59
+ outdir: path.resolve(projectRoot(), "dist"),
54
60
  naming: "backend.js",
55
61
  minify: Bun.argv[2] === "dev" ? false : true, //production
56
62
  target: "node",
@@ -64,8 +70,8 @@ async function buildBackend(backendPath: string = "backend/backend.ts") {
64
70
 
65
71
  async function buildFrontend(file: string) {
66
72
  const result = await Bun.build({
67
- entrypoints: [path.resolve(PROJECT_ROOT, `frontend/${file}`)],
68
- outdir: path.resolve(PROJECT_ROOT, "dist"),
73
+ entrypoints: [path.resolve(projectRoot(), `frontend/${file}`)],
74
+ outdir: path.resolve(projectRoot(), "dist"),
69
75
  naming: "[name]-[hash].[ext]",
70
76
  minify: Bun.argv[2] === "dev" ? false : true, //production
71
77
  target: "browser",
@@ -103,7 +109,7 @@ async function devServer() {
103
109
  async function watchAndBuild(dir: string) {
104
110
  try {
105
111
  //start the file watcher that will rebuild frontend on save
106
- const watcher = watch(path.resolve(PROJECT_ROOT, dir), {
112
+ const watcher = watch(path.resolve(projectRoot(), dir), {
107
113
  recursive: true,
108
114
  });
109
115
  for await (const event of watcher) {
@@ -121,36 +127,36 @@ async function devServer() {
121
127
  watchAndBuild("frontend");
122
128
  watchAndBuild("backend");
123
129
  }
124
- const standardDevReloader = /*html*/ `
125
- <script>
126
- function reloader() {
127
- let socket = null;
130
+ const standardDevReloader = html`
131
+ <script>
132
+ function reloader() {
133
+ let socket = null;
128
134
 
129
- function connectWebSocket() {
130
- if (socket) {
131
- return;
132
- }
133
- socket = new WebSocket("ws://localhost:3001/reload");
135
+ function connectWebSocket() {
136
+ if (socket) {
137
+ return;
138
+ }
139
+ socket = new WebSocket("ws://localhost:3001/reload");
134
140
 
135
- socket.addEventListener("message", (event) => {
136
- window.location.reload();
137
- });
141
+ socket.addEventListener("message", (event) => {
142
+ window.location.reload();
143
+ });
138
144
 
139
- socket.addEventListener("close", (event) => {
140
- // Reestablish the connection after 1 second
141
- socket = null;
142
- });
145
+ socket.addEventListener("close", (event) => {
146
+ // Reestablish the connection after 1 second
147
+ socket = null;
148
+ });
143
149
 
144
- socket.addEventListener("error", (event) => {
145
- socket = null;
146
- });
147
- }
148
- connectWebSocket(); // connect to reloader, if it does not work:
149
- setInterval(connectWebSocket, 1000); // retry every 1 second
150
+ socket.addEventListener("error", (event) => {
151
+ socket = null;
152
+ });
150
153
  }
151
- reloader();
152
- </script>
153
- `;
154
+ connectWebSocket(); // connect to reloader, if it does not work:
155
+ setInterval(connectWebSocket, 1000); // retry every 1 second
156
+ }
157
+ reloader();
158
+ </script>
159
+ `;
154
160
 
155
161
  export {
156
162
  url,
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  },
14
14
  "files": ["dist", "mininext"],
15
- "version": "0.2.8",
15
+ "version": "0.2.10",
16
16
  "devDependencies": {
17
17
  "@types/bun": "latest"
18
18
  },