Build Hook
Run code when your site is built, and add files to the output.
The build hook of a plugin runs once when you build the site with vuesho build. This one writes a file next to the pages:
createSite(docs.toFumadocsSource()).plugins({
name: "site:report",
build({ paths, emitFile }) {
emitFile("build.json", JSON.stringify({ pages: paths.length }));
},
});The hook runs in the build, before the pages are generated. It doesn't run on the dev server or on a server deployment. The build hooks of several plugins run one after another, in plugin order.
Output
The hook receives the output of the build:
paths: the URLs that the build generates, without the base path. They are the pages the build generates, with the data the browser loads for each, and static routes like the sitemap, without the paths that a route rule excludes. The files thatbuildhooks emit are not in the list, and neither are404.htmland.nojekyll, which the build writes for static hosts.emitFile(path, content): writes a file to the public output, with a string or aUint8Array.pathis relative to the output, sobuild.jsonis served at/build.json.fetch(request): requests a page of the built site, through its middleware and routes. The URL of the request has the base path.
An emitted file exists in the output only, not on the dev server, and a file of public/ at the same path takes its place. A route rule that excludes its path fails the build, since no server could serve the file.
Failing a Build
Throw to fail the build. This plugin requests every page, and fails when one has an error:
createSite(docs.toFumadocsSource()).plugins({
name: "site:check-pages",
async build({ paths, fetch }) {
for (const path of paths) {
const url = new URL(this.withBase(path), "http://localhost");
const response = await fetch(new Request(url));
if (response.status >= 400) throw new Error(`${path} responded with ${response.status}`);
}
},
});A page that fails to render fails the build too, without a hook. See Deploy.