vite-plugin-glob-guard
v0.1.0
Published
Opt-in guardrails for import.meta.glob() required globs
Maintainers
Readme
vite-plugin-glob-guard
Fail the build when a required import.meta.glob(...) matches zero files.
What it is
When a glob pattern stops matching (rename/move, wrong extension, negation patterns, etc.),
Vite typically does not fail the build; you can end up with an empty object {}.
This plugin makes that failure mode opt-in and explicit: mark a glob call site as required and fail the build if it matches zero files.
Install
npm i -D vite-plugin-glob-guardUsage
Vite config:
// vite.config.ts
import { defineConfig } from 'vite'
import globGuard from 'vite-plugin-glob-guard'
export default defineConfig({
plugins: [globGuard()],
})Mark required call sites:
// glob-guard:required
const posts = import.meta.glob('./posts/*.md', { eager: true })Options
export interface GlobGuardOptions {
mode?: 'annotated' | 'all'
failOnBuild?: boolean
report?: false | true | { file: string }
ignoreImporters?: (string | RegExp)[]
}Defaults:
mode: 'annotated'failOnBuild: truereport: false
Example error
[glob-guard] Required glob matched 0 files
importer: src/content.ts:12:5
glob: ./posts/*.md
resolved:
- /abs/path/to/project/src/posts/*.md
hints:
- Did you rename/move the directory?
- Did you change file extensions?
- Are you excluding everything via a negated pattern ("!...")?Notes
- Vite 7+ only.
