diff --git a/__tests__/e2e/static-data/static.data.ts b/__tests__/e2e/static-data/static.data.ts index e88b49b4..1a70d74a 100644 --- a/__tests__/e2e/static-data/static.data.ts +++ b/__tests__/e2e/static-data/static.data.ts @@ -1,9 +1,10 @@ import fs from 'fs' +import { defineLoader } from 'vitepress' type Data = Record[] export declare const data: Data -export default { +export default defineLoader({ watch: ['./data/*'], async load(files: string[]): Promise { const foo = fs.readFileSync( @@ -16,4 +17,4 @@ export default { ) return [JSON.parse(foo), JSON.parse(bar)] } -} +}) diff --git a/src/node/index.ts b/src/node/index.ts index c34f3708..0b831b7f 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -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 { diff --git a/src/node/plugins/staticDataPlugin.ts b/src/node/plugins/staticDataPlugin.ts index 4907d5e9..4c977603 100644 --- a/src/node/plugins/staticDataPlugin.ts +++ b/src/node/plugins/staticDataPlugin.ts @@ -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 = Object.create(null)