@spirobel/mininext 0.2.14 → 0.2.16
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 +6 -0
- package/dist/html.d.ts +1 -0
- package/dist/html.js +38 -7
- package/dist/mininext.d.ts +3 -2
- package/dist/mininext.js +3 -2
- package/mininext/html.ts +32 -8
- package/mininext/mininext.ts +12 -2
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/html.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare class HtmlString extends Array {
|
|
|
7
7
|
/**
|
|
8
8
|
* a HtmlString is by default resolved.
|
|
9
9
|
* if we we pass a function as a value to the html`` template string, it will be unresolved.
|
|
10
|
+
* it can also become unresolved if an unresolved HtmlString is passed into it as a value
|
|
10
11
|
*/
|
|
11
12
|
resolved: boolean;
|
|
12
13
|
resolve<T>(mini: Mini<T>): Promise<this>;
|
package/dist/html.js
CHANGED
|
@@ -2,17 +2,37 @@ export class HtmlString extends Array {
|
|
|
2
2
|
/**
|
|
3
3
|
* a HtmlString is by default resolved.
|
|
4
4
|
* if we we pass a function as a value to the html`` template string, it will be unresolved.
|
|
5
|
+
* it can also become unresolved if an unresolved HtmlString is passed into it as a value
|
|
5
6
|
*/
|
|
6
7
|
resolved = true;
|
|
7
8
|
async resolve(mini) {
|
|
8
9
|
if (this.resolved)
|
|
9
10
|
return this;
|
|
10
11
|
for (const [index, htmlPiece] of this.entries()) {
|
|
11
|
-
if (
|
|
12
|
+
if (htmlPiece instanceof HtmlString) {
|
|
13
|
+
let resolvedHtmlPiece = await htmlPiece.resolve(mini);
|
|
14
|
+
if (this instanceof JsonString || this instanceof DangerJsonInHtml) {
|
|
15
|
+
this[index] = JSON.stringify(resolvedHtmlPiece);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this[index] = resolvedHtmlPiece;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (typeof htmlPiece === "function") {
|
|
12
22
|
let resolvedHtmlPiece = await htmlPiece(mini); //passing mini
|
|
13
23
|
if (resolvedHtmlPiece instanceof HtmlString) {
|
|
14
24
|
resolvedHtmlPiece = await resolvedHtmlPiece.resolve(mini);
|
|
15
25
|
}
|
|
26
|
+
else {
|
|
27
|
+
if (this instanceof JsonString || this instanceof DangerJsonInHtml) {
|
|
28
|
+
resolvedHtmlPiece = JSON.stringify(resolvedHtmlPiece);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const notEmpty = resolvedHtmlPiece || "";
|
|
32
|
+
// values will be escaped by default
|
|
33
|
+
resolvedHtmlPiece = Bun.escapeHTML(notEmpty + "");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
16
36
|
// Replace the function with the resolved HTML piece in place
|
|
17
37
|
this[index] = resolvedHtmlPiece;
|
|
18
38
|
}
|
|
@@ -35,11 +55,12 @@ export function html(strings, ...values) {
|
|
|
35
55
|
htmlStringArray.push(string);
|
|
36
56
|
if (index < values.length) {
|
|
37
57
|
const value = values[index];
|
|
38
|
-
// we can pass arrays of HtmlString and they will get flattened
|
|
58
|
+
// we can pass arrays of HtmlString and they will get flattened in the HtmlResponder
|
|
39
59
|
if (Array.isArray(value) &&
|
|
40
60
|
value.every((val) => val instanceof HtmlString)) {
|
|
41
61
|
// If the value is an array of HtmlString objects, add the whole array as a single value
|
|
42
62
|
values[index] = value;
|
|
63
|
+
htmlStringArray.resolved = false; // we could bother with .find here
|
|
43
64
|
}
|
|
44
65
|
else if (typeof value === "function") {
|
|
45
66
|
htmlStringArray.resolved = false;
|
|
@@ -57,11 +78,15 @@ export function html(strings, ...values) {
|
|
|
57
78
|
// values will be escaped by default
|
|
58
79
|
values[index] = Bun.escapeHTML(notEmpty + "");
|
|
59
80
|
}
|
|
81
|
+
else if (value instanceof HtmlString) {
|
|
82
|
+
if (!value.resolved) {
|
|
83
|
+
htmlStringArray.resolved = false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
60
86
|
htmlStringArray.push(values[index]);
|
|
61
87
|
}
|
|
62
88
|
}
|
|
63
|
-
|
|
64
|
-
return htmlStringArray.flat();
|
|
89
|
+
return htmlStringArray;
|
|
65
90
|
}
|
|
66
91
|
export class JsonString extends HtmlString {
|
|
67
92
|
}
|
|
@@ -79,16 +104,23 @@ function JsonTemplateProcessor(danger = false) {
|
|
|
79
104
|
jsonStringArray.push(string);
|
|
80
105
|
if (index < values.length) {
|
|
81
106
|
const value = values[index];
|
|
82
|
-
// we can pass arrays of HtmlString and they will get flattened
|
|
107
|
+
// we can pass arrays of HtmlString and they will get flattened in the HtmlResponder
|
|
83
108
|
if (Array.isArray(value) &&
|
|
84
109
|
value.every((val) => val instanceof HtmlString)) {
|
|
85
110
|
// If the value is an array of HtmlString objects, add the whole array as a single value
|
|
86
111
|
values[index] = value;
|
|
112
|
+
jsonStringArray.resolved = false; // we could bother with .find here
|
|
87
113
|
}
|
|
88
114
|
else if (typeof value === "function") {
|
|
89
115
|
jsonStringArray.resolved = false;
|
|
90
116
|
values[index] = value;
|
|
91
117
|
}
|
|
118
|
+
else if (value instanceof HtmlString) {
|
|
119
|
+
if (!value.resolved) {
|
|
120
|
+
jsonStringArray.resolved = false;
|
|
121
|
+
}
|
|
122
|
+
values[index] = value;
|
|
123
|
+
}
|
|
92
124
|
else if (!(value instanceof JsonString)) {
|
|
93
125
|
const notEmpty = value || "";
|
|
94
126
|
// values will be turned into a JSON string
|
|
@@ -97,8 +129,7 @@ function JsonTemplateProcessor(danger = false) {
|
|
|
97
129
|
jsonStringArray.push(values[index]);
|
|
98
130
|
}
|
|
99
131
|
}
|
|
100
|
-
|
|
101
|
-
return jsonStringArray.flat();
|
|
132
|
+
return jsonStringArray;
|
|
102
133
|
};
|
|
103
134
|
}
|
|
104
135
|
export const json = JsonTemplateProcessor();
|
package/dist/mininext.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { url, Mini, type HtmlHandler } from "./url";
|
|
2
|
-
import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
|
|
2
|
+
import { html, json, isError, HtmlString, head, commonHead, cssReset } from "./html";
|
|
3
3
|
declare global {
|
|
4
4
|
var PROJECT_ROOT: string | undefined;
|
|
5
5
|
}
|
|
6
6
|
declare function build(backendPath?: string): Promise<void>;
|
|
7
7
|
declare const standardDevReloader: HtmlString;
|
|
8
8
|
declare function makeEntrypoint(): Promise<(w: any) => Promise<Response>>;
|
|
9
|
-
|
|
9
|
+
declare const css: typeof html;
|
|
10
|
+
export { url, css, json, html, head, build, makeEntrypoint, isError, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
|
package/dist/mininext.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { url, Mini } from "./url";
|
|
2
|
-
import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
|
|
2
|
+
import { html, json, isError, HtmlString, head, commonHead, cssReset, } from "./html";
|
|
3
3
|
import { watch } from "fs/promises";
|
|
4
4
|
import * as path from "path";
|
|
5
5
|
function projectRoot() {
|
|
@@ -158,4 +158,5 @@ async function makeEntrypoint() {
|
|
|
158
158
|
}
|
|
159
159
|
return module.default;
|
|
160
160
|
}
|
|
161
|
-
|
|
161
|
+
const css = html;
|
|
162
|
+
export { url, css, json, html, head, build, makeEntrypoint, isError, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
|
package/mininext/html.ts
CHANGED
|
@@ -20,16 +20,32 @@ export class HtmlString extends Array {
|
|
|
20
20
|
/**
|
|
21
21
|
* a HtmlString is by default resolved.
|
|
22
22
|
* if we we pass a function as a value to the html`` template string, it will be unresolved.
|
|
23
|
+
* it can also become unresolved if an unresolved HtmlString is passed into it as a value
|
|
23
24
|
*/
|
|
24
25
|
resolved = true;
|
|
25
26
|
async resolve<T>(mini: Mini<T>) {
|
|
26
27
|
if (this.resolved) return this;
|
|
27
28
|
|
|
28
29
|
for (const [index, htmlPiece] of this.entries()) {
|
|
29
|
-
if (
|
|
30
|
+
if (htmlPiece instanceof HtmlString) {
|
|
31
|
+
let resolvedHtmlPiece = await htmlPiece.resolve(mini);
|
|
32
|
+
if (this instanceof JsonString || this instanceof DangerJsonInHtml) {
|
|
33
|
+
this[index] = JSON.stringify(resolvedHtmlPiece);
|
|
34
|
+
} else {
|
|
35
|
+
this[index] = resolvedHtmlPiece;
|
|
36
|
+
}
|
|
37
|
+
} else if (typeof htmlPiece === "function") {
|
|
30
38
|
let resolvedHtmlPiece = await htmlPiece(mini); //passing mini
|
|
31
39
|
if (resolvedHtmlPiece instanceof HtmlString) {
|
|
32
40
|
resolvedHtmlPiece = await resolvedHtmlPiece.resolve(mini);
|
|
41
|
+
} else {
|
|
42
|
+
if (this instanceof JsonString || this instanceof DangerJsonInHtml) {
|
|
43
|
+
resolvedHtmlPiece = JSON.stringify(resolvedHtmlPiece);
|
|
44
|
+
} else {
|
|
45
|
+
const notEmpty = resolvedHtmlPiece || "";
|
|
46
|
+
// values will be escaped by default
|
|
47
|
+
resolvedHtmlPiece = Bun.escapeHTML(notEmpty + "");
|
|
48
|
+
}
|
|
33
49
|
}
|
|
34
50
|
// Replace the function with the resolved HTML piece in place
|
|
35
51
|
this[index] = resolvedHtmlPiece;
|
|
@@ -60,13 +76,14 @@ export function html<X = undefined>(
|
|
|
60
76
|
if (index < values.length) {
|
|
61
77
|
const value = values[index];
|
|
62
78
|
|
|
63
|
-
// we can pass arrays of HtmlString and they will get flattened
|
|
79
|
+
// we can pass arrays of HtmlString and they will get flattened in the HtmlResponder
|
|
64
80
|
if (
|
|
65
81
|
Array.isArray(value) &&
|
|
66
82
|
value.every((val) => val instanceof HtmlString)
|
|
67
83
|
) {
|
|
68
84
|
// If the value is an array of HtmlString objects, add the whole array as a single value
|
|
69
85
|
values[index] = value;
|
|
86
|
+
htmlStringArray.resolved = false; // we could bother with .find here
|
|
70
87
|
} else if (typeof value === "function") {
|
|
71
88
|
htmlStringArray.resolved = false;
|
|
72
89
|
values[index] = value;
|
|
@@ -80,12 +97,15 @@ export function html<X = undefined>(
|
|
|
80
97
|
const notEmpty = value || "";
|
|
81
98
|
// values will be escaped by default
|
|
82
99
|
values[index] = Bun.escapeHTML(notEmpty + "");
|
|
100
|
+
} else if (value instanceof HtmlString) {
|
|
101
|
+
if (!value.resolved) {
|
|
102
|
+
htmlStringArray.resolved = false;
|
|
103
|
+
}
|
|
83
104
|
}
|
|
84
105
|
htmlStringArray.push(values[index]);
|
|
85
106
|
}
|
|
86
107
|
}
|
|
87
|
-
|
|
88
|
-
return htmlStringArray.flat();
|
|
108
|
+
return htmlStringArray;
|
|
89
109
|
}
|
|
90
110
|
export class JsonString extends HtmlString {}
|
|
91
111
|
export class DangerJsonInHtml extends HtmlString {}
|
|
@@ -120,17 +140,22 @@ function JsonTemplateProcessor(danger: boolean = false) {
|
|
|
120
140
|
|
|
121
141
|
if (index < values.length) {
|
|
122
142
|
const value = values[index];
|
|
123
|
-
|
|
124
|
-
// we can pass arrays of HtmlString and they will get flattened automatically
|
|
143
|
+
// we can pass arrays of HtmlString and they will get flattened in the HtmlResponder
|
|
125
144
|
if (
|
|
126
145
|
Array.isArray(value) &&
|
|
127
146
|
value.every((val) => val instanceof HtmlString)
|
|
128
147
|
) {
|
|
129
148
|
// If the value is an array of HtmlString objects, add the whole array as a single value
|
|
130
149
|
values[index] = value;
|
|
150
|
+
jsonStringArray.resolved = false; // we could bother with .find here
|
|
131
151
|
} else if (typeof value === "function") {
|
|
132
152
|
jsonStringArray.resolved = false;
|
|
133
153
|
values[index] = value;
|
|
154
|
+
} else if (value instanceof HtmlString) {
|
|
155
|
+
if (!value.resolved) {
|
|
156
|
+
jsonStringArray.resolved = false;
|
|
157
|
+
}
|
|
158
|
+
values[index] = value;
|
|
134
159
|
} else if (!(value instanceof JsonString)) {
|
|
135
160
|
const notEmpty = value || "";
|
|
136
161
|
// values will be turned into a JSON string
|
|
@@ -139,8 +164,7 @@ function JsonTemplateProcessor(danger: boolean = false) {
|
|
|
139
164
|
jsonStringArray.push(values[index]);
|
|
140
165
|
}
|
|
141
166
|
}
|
|
142
|
-
|
|
143
|
-
return jsonStringArray.flat();
|
|
167
|
+
return jsonStringArray;
|
|
144
168
|
};
|
|
145
169
|
}
|
|
146
170
|
export const json = JsonTemplateProcessor();
|
package/mininext/mininext.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { url, Mini, type HtmlHandler } from "./url";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
html,
|
|
4
|
+
json,
|
|
5
|
+
isError,
|
|
6
|
+
HtmlString,
|
|
7
|
+
head,
|
|
8
|
+
commonHead,
|
|
9
|
+
cssReset,
|
|
10
|
+
} from "./html";
|
|
3
11
|
import { watch } from "fs/promises";
|
|
4
12
|
import * as path from "path";
|
|
5
13
|
function projectRoot() {
|
|
@@ -170,9 +178,11 @@ async function makeEntrypoint() {
|
|
|
170
178
|
}
|
|
171
179
|
return module.default as (w: any) => Promise<Response>;
|
|
172
180
|
}
|
|
173
|
-
|
|
181
|
+
const css = html;
|
|
174
182
|
export {
|
|
175
183
|
url,
|
|
184
|
+
css,
|
|
185
|
+
json,
|
|
176
186
|
html,
|
|
177
187
|
head,
|
|
178
188
|
build,
|