defineLoader #
Declares a data loader that reads local files; export it from a .data.ts file and import it from pages and layouts.
Usage #
import { defineLoader } from 'kawapress'
export default defineLoader({
async load({ read }) {
const file = await read('./package.json')
const manifest = JSON.parse(file) as { version: string }
return manifest.version
},
})Parameters
The loader: a load() function and, optionally, options.
Produces the data. Read files through context, so changes to them refresh the data.
Reads a UTF-8 text file and records it as a dependency of this loader. A relative path starts from the loader file.
Finds files and returns their absolute paths, sorted. The pattern is recorded as a dependency of this loader, so files that appear or go also refresh it. A relative pattern starts from the loader file.
Extra options of the loader.
Returns The same loader, now typed.
Declares a data loader in the VitePress form: watch names the files, and load() receives them.
import { defineLoader } from 'kawapress'
export default defineLoader({
watch: ['./posts/*.md'],
load: files => files.length,
})Parameters
The loader: watch patterns and a load() function.
Glob pattern, or a list of them, for the files to watch. Relative patterns start from the loader file.
Produces the data from the absolute paths of the files watch matches.
Extra options of the loader.
Returns The same loader, now typed.
Notes #
Files read through context are tracked, so changing one refreshes the data.
Full type signature
function defineLoader<T>(loader: TrackedDataLoader<T>): TrackedDataLoader<T>
function defineLoader<T>(loader: WatchDataLoader<T>): WatchDataLoader<T>

Comments