domco

API Reference

Classes

Injector

Defined in: injector/index.ts:33

Inject tags into an HTML string.

import { Injector } from "domco/injector";

const injector = new Injector(
	`<!doctype html><html><body><!-- comment --></body></html>`,
);

injector
	// Set or change the title
	.title("My Title")
	// pass a TagDescriptor
	.head([{ name: "script", attrs: { type: "module", src: "./script.js" } }])
	// pass a string of text
	.body("Prepended to the body! ", "prepend")
	// replace comments
	.comment("comment", "My comment")
	// stringify HTML
	.toString();

Produces the following HTML.

<!doctype html>
<html>
	<head>
		<title>My Title</title>
		<script type="module" src="./script.js"></script>
	</head>
	<body>
		Prepended to the body! My comment
	</body>
</html>

Constructors

new Injector()

new Injector(html?): Injector

Defined in: injector/index.ts:45

Parameters
html?

string

The HTML string.

Returns

Injector

Default
<!doctype html>
<html>
	<head></head>
	<body></body>
</html>

Methods

body()

body(tags, method): Injector

Defined in: injector/index.ts:196

Inject tags into the body element.

Parameters
tags

TagInput

Tags to inject.

method

InjectMethod = "append"

Add tags at the end, beginning, or replace. - defaults to "append"

Returns

Injector

The Injector instance.

comment()

comment(text, tags): Injector

Defined in: injector/index.ts:140

Replace comments with tags.

Parameters
text

string

Text within comment.

tags

TagInput

Tags to replace the comment with.

Returns

Injector

The Injector instance.

head()

head(tags, method): Injector

Defined in: injector/index.ts:170

Inject tags into the head element.

Parameters
tags

TagInput

Tags to inject.

method

InjectMethod = "append"

Add tags at the end, beginning, or replace. - defaults to "append"

Returns

Injector

The Injector instance.

title()

title(text): Injector

Defined in: injector/index.ts:155

Set or change the document’s title element.

Parameters
text

string

Text to set or change the title to.

Returns

Injector

The Injector instance.

toString()

toString(): string

Defined in: injector/index.ts:51

Returns

string

The HTML.

serializeTags()

static serializeTags(tags): string

Defined in: injector/index.ts:86

Serializes an array of TagDescriptors into a string.

Parameters
tags

undefined | TagInput

Returns

string

Type Aliases

Adapter

Adapter: object

Defined in: types/index.ts:63

A domco adapter that configures the build to a target production environment.

Type declaration

devMiddleware?

optional devMiddleware: AdapterMiddleware[]

Middleware to apply in dev mode.

entry

entry: AdapterEntry

Entry point for the server application.

message

message: string

Message to log when the build is complete.

name

name: string

The name of the adapter.

noExternal?

optional noExternal: SSROptions["noExternal"]

Passed into Vite config.ssr.noExternal.

previewMiddleware?

optional previewMiddleware: AdapterMiddleware[]

Middleware to apply in preview mode.

run()?

optional run: () => any

The script to run after Vite build is complete.

Returns

any

target?

optional target: SSRTarget

Passed into Vite config.ssr.target.


AdapterBuilder()<AdapterOptions>

AdapterBuilder<AdapterOptions>: (AdapterOptions?) => MaybePromise<Adapter>

Defined in: types/index.ts:93

Use this type to create your own adapter. Pass any options for the adapter in as a generic.

Type Parameters

AdapterOptions = never

Parameters

AdapterOptions?

AdapterOptions

Returns

MaybePromise<Adapter>


AdapterEntry()

AdapterEntry: (AdapterEntryOptions) => object

Defined in: types/index.ts:47

A function that returns an additional entry point to include in the SSR build.

Parameters

AdapterEntryOptions
funcId

string

The function entry point to import handler from.

Returns

object

code

code: string

Code for the entry point.

id

id: string

The name of the entry point without extension.

Example
"main";

AdapterMiddleware

AdapterMiddleware: Connect.NextHandleFunction

Defined in: types/index.ts:44

Middleware used in the Vite server for dev and preview.


DomcoConfig

DomcoConfig: object

Defined in: types/index.ts:119

domco Config

Use if you want to create a separate object for your domco config. Pass the config into the domco vite plugin.

Type declaration

adapter?

optional adapter: ReturnType<AdapterBuilder>

domco adapter.

Defaults to undefined - creates a func build only.

Default
undefined;
Example
import { adapter } from `"domco/adapter/...";`

Example

// vite.config.ts
import { domco, type DomcoConfig } from "domco";
import { defineConfig } from "vite";

const config: DomcoConfig = {
	// options...
};

export default defineConfig({
	plugins: [domco(config)],
});

FuncModule

FuncModule: object

Defined in: types/index.ts:7

Exports from the SSR +func entry point.

Type declaration

handler

handler: Handler

prerender

prerender: Prerender


Handler()

Handler: (req) => MaybePromise<Response>

Defined in: types/index.ts:24

Request handler, takes a web request and returns a web response.

// src/server/+func.ts
import type { Handler } from "domco";

export const handler: Handler = async (req) => {
	return new Response("Hello world");
};

Parameters

req

Request

Returns

MaybePromise<Response>


InjectMethod

InjectMethod: "append" | "prepend" | "replace"

Defined in: types/index.ts:175

How to inject tags into the HTML string.


MaybePromise<T>

MaybePromise<T>: T | Promise<T>

Defined in: types/index.ts:4

Helper type for a type that could be a promise.

Type Parameters

T


Prerender

Prerender: string[] | Set<string> | () => MaybePromise<string[] | Set<string>>

Defined in: types/index.ts:38

Paths to prerender at build time.

Example

// src/server/+func.ts
import type { Prerender } from "domco";

export const prerender: Prerender = ["/", "/post-1", "/post-2"];

TagDescriptor

TagDescriptor: object

Defined in: types/index.ts:137

An object that describes a tag and its children.

Type declaration

attrs?

optional attrs: Record<string, string | boolean | undefined>

The attributes on the tag.

Example

These attributes,

{
	class: "text-black",
	open: true,
}

would produce the following HTML.

<dialog class="text-black" open>...</dialog>

children?

optional children: TagInput

Children of the tag. Tags or a string of HTML.

name

name: string

The tagName of the element.

Example
"h1";

TagInput

TagInput: string | TagDescriptor[]

Defined in: types/index.ts:172

Tags can be a string, or an array of TagDescriptors.

Functions

domco()

domco(domcoConfig): Promise<Plugin[]>

Defined in: plugin/index.ts:30

Creates domco Vite plugin, add to your plugins array within your vite.config to start using domco.

Parameters

domcoConfig

DomcoConfig = {}

Your domco config object.

Returns

Promise<Plugin[]>

The domco Vite plugin.

Example

// vite.config.ts
import { domco } from "domco";
import { defineConfig } from "vite";

export default defineConfig({
	plugins: [domco()],
});
Edit this page