Server side renderer.
See mizu.sh documentation for more details.
new
Server(options?: ServerOptions)
Server
constructor.
generate(sources: Array<>,options?: ServerGenerateOptions,): Promise<void>
Generate static files from various sources.
Options:
output
: Specify the path to the output directory.clean
: Empty theoutput
directory before generating files.
Supported sources:
StringSource
: Generate content from raw strings.GlobSource
: Generate content from local files matching the provided glob patterns.CallbackSource
: Generate content from callback returns.URLSource
: Generate content from fetched URLs.
Each source can be templated using mizu rendering by passing a render
option.
const mizu = new Server({ directives: ["@mizu/test"], generate: { output: "/fake/output" } }) await mizu.generate( [ // Copy content from strings [ "<p>foo</p>", "string.html" ], [ "<p ~test.text='foo'></p>", "string_render.html", { render: { context: { foo: "bar" } } } ], // Copy content from local files [ "**\/*", "public", { directory: "/fake/static" } ], [ "*.html", "public", { directory: "/fake/partials", render: { context: { foo: "bar "} } } ], // Copy content from callback return [ () => JSON.stringify({ foo: "bar" }), "callback.json" ], [ () => `<p ~test.text="'foo'"></p>`, "callback.html", { render: { context: { foo: "bar" } } } ], // Copy content from URL [ new URL(`data:text/html,<p>foobar</p>`), "url.html" ], [ new URL(`data:text/html,<p ~test.text="foo"></p>`), "url_render.html", { render: { context: { foo: "bar" } } } ], ], // No-op: do not actually write files and directories { fs: { readdir: () => Promise.resolve([] as string[]), mkdir: () => null as any, write: () => null as any } }, )
render(content: string | Arg<Renderer["render"]>,options?: ServerRenderOptions & Pick<ServerOptions, "warn">,): Promise<string>
Parse an HTML string and render all subtrees.
The *mizu
attribute is only required if implicit
is set to false
.
const mizu = new Server({ context: { foo: "bar" } }) await mizu.render(`<html><body><a ~test.text="foo"></a></body></html>`)