vite-plugin-glob

raw JSON →
0.3.2 verified Mon Apr 27 auth: no javascript maintenance

An experimental Vite plugin providing an enhanced glob import API with support for multiple patterns, ignore, custom queries, and selective default/named exports. Current version 0.3.2. It served as a design experiment that influenced Vite's built-in import.meta.glob, offering features like HMR on file changes and Vite alias support before they were integrated into core. Active development but note that Vite v3.0 has since ported some of its features, making this plugin primarily useful for experimentation or using specific options not yet in core. Key differentiator: unified API with options like eager, query, as, and import, allowing fine-grained control over globbed imports.

error ERR_MODULE_NOT_FOUND: Cannot find module 'vite-plugin-glob'
cause Package not installed or installed incorrectly.
fix
Run npm install -D vite-plugin-glob from project root.
error Cannot use import statement outside a module
cause Using ES module import in a CommonJS file.
fix
Ensure your file is .mjs or set type: module in package.json, or use dynamic import().
error Uncaught (in promise) TypeError: import.meta.importGlob is not a function
cause Missing plugin setup in vite.config or plugin not applied before usage.
fix
Add GlobPlugin() to vite.config plugins array and restart Vite dev server.
error No matching export in 'vite-plugin-glob' for import 'Options'
cause Trying to import Options as a value instead of a type.
fix
Use import type { Options } from 'vite-plugin-glob'.
breaking The export option has been renamed to import in v0.3.0.
fix Update usage: { export: 'default' } -> { import: 'default' }
gotcha restoreQueryExtension option is disabled by default since v0.3.0, meaning file extensions may not be restored in queries.
fix Set restoreQueryExtension: true if you need query extension restoration.
deprecated The takeover feature may become unnecessary as Vite core incorporates similar features; check Vite version compatibility.
fix Consider using Vite's native import.meta.glob with Vite >=3.0 if you don't need this plugin's specific options.
gotcha Using import.meta.importGlob with the same pattern as Vite's import.meta.glob may cause conflicts if takeover is not enabled.
fix Either enable takeover: true or use distinct patterns to avoid confusion.
gotcha HMR may not work correctly for newly created files in some scenarios; see issue #22.
fix Upgrade to v0.3.2 or later for improved HMR handling.
npm install vite-plugin-glob
yarn add vite-plugin-glob
pnpm add vite-plugin-glob

Shows how to install the plugin in vite.config and use import.meta.importGlob with eager option to load all .ts files immediately.

// vite.config.ts
import { defineConfig } from 'vite'
import GlobPlugin from 'vite-plugin-glob'

export default defineConfig({
  plugins: [
    GlobPlugin({
      // Enable to have this plugin handle import.meta.glob instead of Vite
      // takeover: true,
    }),
  ],
})

// In any module:
const modules = import.meta.importGlob('./*.ts', { eager: true })
console.log(modules)