Skip to content

Commit 18be776

Browse files
committed
continue
1 parent dae51d0 commit 18be776

File tree

6 files changed

+53
-54
lines changed

6 files changed

+53
-54
lines changed

@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. */

deno.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
// Development server dependencies
119119
"@www/": "./www/",
120120
"@deno/doc": "jsr:@deno/doc@~0.161",
121-
"@libs/bundle": "jsr:@libs/bundle@12",
121+
"@libs/bundle": "jsr:@libs/bundle@12.5",
122122
"@libs/logger": "jsr:@libs/logger@3",
123123
"@std/collections": "jsr:@std/collections@1",
124124
"@std/http": "jsr:@std/http@1",

deno.lock

Lines changed: 6 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/api/build.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,43 @@ export default async function (request: Request) {
1313
}
1414
try {
1515
const options = await request.json()
16-
const banner = _banner.replace(`${meta.name}${meta.version}\n`, `${meta.name}${meta.version} — Custom build (${new Date().toDateString()})\n`)
17-
const bundled = await js("@mizu/render/client", { format: "iife", banner, minify: options.minify ? "terser" : false })
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+
})
1843
return new Response(bundled, { headers: { "Content-Type": "application/javascript; charset=utf-8" } })
1944
} catch (error) {
45+
if (error instanceof TypeError) {
46+
return new Response(error.message, { status: Status.BadRequest })
47+
}
2048
return new Response(error.message, { status: Status.InternalServerError })
2149
}
2250
}
51+
52+
/** Throws a `TypeError` with the provided message. */
53+
function throws(message: string): never {
54+
throw new TypeError(message)
55+
}

www/html/build.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@
108108
event.preventDefault()
109109
// Prepare data
110110
const raw = new FormData(form)
111-
const data = {}
111+
const data = { format: "", console: [], directives: [], minify: false }
112112
for (const [key, value] of raw) {
113113
switch (key) {
114-
case "directives":
115114
case "console":
116-
data[key] ??= []
115+
case "directives":
117116
data[key].push(value)
118117
break
119118
case "minify":

www/tools.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ function config(name: string, { parse = true } = {} as { parse?: boolean }) {
4646
}
4747

4848
/** Generate JS. */
49-
export function js(exported: string, options = {} as Pick<NonNullable<Arg<typeof bundle, 1>>, "format" | "banner" | "minify"> & { raw?: Record<PropertyKey, unknown> }) {
49+
export function js(exported: string, options = {} as Pick<NonNullable<Arg<typeof bundle, 1>>, "format" | "banner" | "minify" | "overrides"> & { raw?: Record<PropertyKey, unknown> }) {
5050
const packaged = exported.match(jsr)?.groups?.package ?? exported
5151
const url = import.meta.resolve(exported)
5252
log.with({ package: packaged, url }).debug("bundling javascript")
5353
if (options?.format === "iife") {
5454
options.raw ??= {}
55+
options.raw.define ??= {}
5556
options.raw.target = "es2020"
56-
options.raw.define = { "globalThis.MIZU_IIFE": "true" }
57+
Object.assign(options.raw.define as Record<PropertyKey, string>, { ["globalThis.MIZU_IIFE"]: "true" })
5758
}
5859
return bundle(new URL(url), { banner, ...options })
5960
}

0 commit comments

Comments
 (0)