method

__internal.ImportMetaGlob.glob

glob<TModule = unknown>(
pattern: string | string[],
options?: ImportMetaGlobOptions & { eager: false }
): Record<string, () => Promise<TModule>>;

Import every module that matches a glob pattern. Compatible with Vite's import.meta.glob.

Bun's transpiler and bundler replace the call with an object literal, so the arguments must be literals.

const pages = import.meta.glob("./pages/*.tsx");
// {
//   "./pages/about.tsx": () => import("./pages/about.tsx"),
//   "./pages/home.tsx": () => import("./pages/home.tsx"),
// }
glob<TModule = unknown>(
pattern: string | string[],
options: ImportMetaGlobOptions & { eager: true }
): Record<string, TModule>;
const pages = import.meta.glob("./pages/*.tsx", { eager: true });
// import * as about from "./pages/about.tsx";
// import * as home from "./pages/home.tsx";
// { "./pages/about.tsx": about, "./pages/home.tsx": home }

Referenced types

interface ImportMetaGlobOptions

Options for ImportMeta.glob

  • base?: string

    The directory that relative patterns and the result's keys are relative to. It starts with ./ or ../ (from the importing file) or / (from the project root).

  • eager?: boolean

    Import the modules with import statements instead of () => import()

  • exhaustive?: boolean

    Also match dotfiles and files in node_modules

  • import?: string

    Import this export, such as "default", instead of the module namespace object

  • query?: string | Record<string, string | number | boolean>

    Query string to append to each import path