Skip to content

Commit 65c9f55

Browse files
committed
feat: add custom builder
1 parent 1807534 commit 65c9f55

File tree

10 files changed

+360
-57
lines changed

10 files changed

+360
-57
lines changed

@mizu/internal/engine/renderer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,9 @@ export class Renderer {
11691169
* ```
11701170
*/
11711171
debug(message: string, target?: Nullable<HTMLElement | Comment>): void {
1172-
this.#debug?.(message, target)
1172+
if (this.#debug) {
1173+
this.#debug(message, target)
1174+
}
11731175
}
11741176
}
11751177

@@ -1178,9 +1180,9 @@ export type RendererOptions = {
11781180
/** Initial {@linkcode Directive}s. */
11791181
directives?: Arg<Renderer["load"]>
11801182
/** Warnings callback. */
1181-
warn?: (message: string, target?: Nullable<HTMLElement | Comment>) => unknown
1183+
warn?: false | ((message: string, target?: Nullable<HTMLElement | Comment>) => unknown)
11821184
/** Debug callback. */
1183-
debug?: (message: string, target?: Nullable<HTMLElement | Comment>) => unknown
1185+
debug?: false | ((message: string, target?: Nullable<HTMLElement | Comment>) => unknown)
11841186
}
11851187

11861188
/** {@linkcode Renderer.evaluate()} options. */

@mizu/render/client/client.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ export class Client {
1717
static defaults = {
1818
window: globalThis.window,
1919
directives: defaults,
20-
context: {},
20+
// @ts-expect-error: custom builder
21+
context: globalThis.MIZU_CUSTOM_DEFAULTS_CONTEXT ?? {},
22+
// @ts-expect-error: custom builder
2123
// deno-lint-ignore no-console
22-
warn: console.warn,
23-
debug: undefined,
24+
warn: globalThis.MIZU_CUSTOM_DEFAULTS_WARN ?? console.warn,
25+
// @ts-expect-error: custom builder
26+
debug: globalThis.MIZU_CUSTOM_DEFAULTS_DEBUG ?? false,
2427
} as unknown as Required<ClientOptions>
2528

2629
/** {@linkcode Client} constructor. */

api/build.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env DENO_DIR=/tmp deno run --version=v2.1.2
2+
// Imports
3+
import { STATUS_CODE as Status, STATUS_TEXT as StatusText } from "@std/http"
4+
import { banner as _banner, js, meta } from "@www/tools.ts"
5+
6+
/** API: Minify css */
7+
export default async function (request: Request) {
8+
if (request.method !== "POST") {
9+
return new Response(StatusText[Status.MethodNotAllowed], { status: Status.MethodNotAllowed })
10+
}
11+
if (new URL(`https://${request.headers.get("Host")}`).hostname !== new URL(`https://${Deno.env.get("ALLOWED_HOST") || "localhost"}`).hostname) {
12+
return new Response("Custom builds may not be linked directly", { status: Status.Forbidden })
13+
}
14+
try {
15+
const options = await request.json()
16+
const bundled = await js("@mizu/render/client", {
17+
banner: _banner.replace(`${meta.name}${meta.version}\n`, `${meta.name}${meta.version} — Custom build (${new Date().toDateString()})\n`),
18+
minify: options.minify === true ? "terser" : false,
19+
format: ["iife", "esm"].includes(options.format) ? options.format : throws("format: must be 'iife' or 'esm'"),
20+
raw: {
21+
define: {
22+
"globalThis.MIZU_CUSTOM_DEFAULTS_CONTEXT": options.context ? options.context : "null",
23+
"globalThis.MIZU_CUSTOM_DEFAULTS_WARN": options.console?.includes("warn") ? "console.warn" : "false",
24+
"globalThis.MIZU_CUSTOM_DEFAULTS_DEBUG": options.console?.includes("debug") ? "console.debug" : "false",
25+
},
26+
},
27+
overrides: {
28+
imports: {
29+
"./defaults.ts": Array.isArray(options.directives) && options.directives.every((name) => typeof name === "string")
30+
? `data:text/javascript;base64,${
31+
btoa(
32+
`
33+
import _mizu from "@mizu/mizu"
34+
${options.directives.map((name: string, i: number) => `import _${i} from "@mizu/${name}"`).join("\n")}
35+
export default [${options.directives.map((_: string, i: number) => `_${i}`).join(", ")}].flat(Infinity)
36+
`.split("\n").map((line) => line.trim()).filter(Boolean).join("\n"),
37+
)
38+
}`
39+
: throws("directives: must be an array of string"),
40+
},
41+
},
42+
})
43+
return new Response(bundled, { headers: { "Content-Type": "application/javascript; charset=utf-8" } })
44+
} catch (error) {
45+
if (error instanceof TypeError) {
46+
return new Response(error.message, { status: Status.BadRequest })
47+
}
48+
return new Response(error.message, { status: Status.InternalServerError })
49+
}
50+
}
51+
52+
/** Throws a `TypeError` with the provided message. */
53+
function throws(message: string): never {
54+
throw new TypeError(message)
55+
}

deno.jsonc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
},
8383
"serve": {
8484
"description": "Serve website locally",
85-
"command": "deno serve --allow-read --allow-env --allow-net --allow-run --allow-write=/tmp --port=4649 --location=http://localhost:4649 www/serve.ts"
85+
"command": "deno serve --allow-read --allow-env --allow-net --allow-run --allow-write=/tmp --allow-sys=osRelease --port=4649 --location=http://localhost:4649 www/serve.ts"
8686
},
8787
// Miscellaneous tasks
8888
"repo:fill-scopes": {
@@ -116,9 +116,10 @@
116116
"@libs/testing": "jsr:@libs/testing@4",
117117
"@std/fmt": "jsr:@std/fmt@1",
118118
// Development server dependencies
119+
"@api/": "./api/",
119120
"@www/": "./www/",
120121
"@deno/doc": "jsr:@deno/doc@~0.161",
121-
"@libs/bundle": "jsr:@libs/bundle@12",
122+
"@libs/bundle": "jsr:@libs/bundle@12.5",
122123
"@libs/logger": "jsr:@libs/logger@3",
123124
"@std/collections": "jsr:@std/collections@1",
124125
"@std/http": "jsr:@std/http@1",

0 commit comments

Comments
 (0)