feat(data-loader): defineLoader() type helper

pull/2044/head
Evan You 2 years ago
parent e29b6a051e
commit 4673bb1879

@ -1,9 +1,10 @@
import fs from 'fs'
import { defineLoader } from 'vitepress'
type Data = Record<string, boolean>[]
export declare const data: Data
export default {
export default defineLoader({
watch: ['./data/*'],
async load(files: string[]): Promise<Data> {
const foo = fs.readFileSync(
@ -16,4 +17,4 @@ export default {
)
return [JSON.parse(foo), JSON.parse(bar)]
}
}
})

@ -4,6 +4,7 @@ export * from './markdown'
export * from './build/build'
export * from './serve/serve'
export * from './init/init'
export { defineLoader, type LoaderModule } from './plugins/staticDataPlugin'
// shared types
export type {

@ -12,11 +12,18 @@ const loaderMatch = /\.data\.(j|t)s$/
let server: ViteDevServer
interface LoaderModule {
export interface LoaderModule {
watch?: string[] | string
load: (watchedFiles: string[]) => any
}
/**
* Helper for defining loaders with type inference
*/
export function defineLoader(loader: LoaderModule) {
return loader
}
const idToLoaderModulesMap: Record<string, LoaderModule | undefined> =
Object.create(null)

Loading…
Cancel
Save