babel-plugin-transform-vite-meta-glob
raw JSON → 1.1.2 verified Sat Apr 25 auth: no javascript
Babel plugin that emulates Vite's import.meta.glob and import.meta.globEager functionality for use in non-Vite environments like Node.js test runners. Version 1.1.2 is current, released January 2024. Regularly updated with bug fixes and feature support, including eager imports via options. It provides an approximation of Vite's glob import transformation, not for production use. Differentiates from Vite itself by enabling these features in plain Babel pipelines, often used with Jest or Mocha.
Common errors
error Error: Cannot find module 'babel-plugin-transform-vite-meta-glob' ↓
cause Plugin not installed or Babel is looking in wrong node_modules.
fix
Run npm install --save-dev babel-plugin-transform-vite-meta-glob and ensure Babel's config resolves from your project root.
error TypeError: (0 , _default.default) is not a function ↓
cause Using CJS require() to import the ESM default export incorrectly.
fix
Use string reference in Babel config: plugins: ['babel-plugin-transform-vite-meta-glob'] instead of requiring directly.
error SyntaxError: Unexpected token 'import' ↓
cause Babel is not configured to handle dynamic import() syntax.
fix
Install @babel/plugin-syntax-dynamic-import and add it to your plugins array.
error Error: .globEager is not a function ↓
cause Using deprecated import.meta.globEager with a plugin version that doesn't support it yet.
fix
Use import.meta.glob with { eager: true } option, or update the plugin to v1.1.0+.
error Error: Could not resolve glob pattern 'C:\path' ↓
cause Windows backslashes in glob pattern not normalized; affected versions <1.1.2.
fix
Update to v1.1.2+ or use forward slashes in glob patterns.
Warnings
gotcha On Windows, generated paths use backslashes, causing issues. Fixed in 1.1.2, but ensure you have at least that version. ↓
fix Update to v1.1.2+ or manually replace backslashes in glob patterns.
deprecated import.meta.globEager is deprecated; use import.meta.glob with '{ eager: true }' instead. ↓
fix Replace import.meta.globEager('./path') with import.meta.glob('./path', { eager: true })
gotcha The plugin only transforms static glob patterns; dynamic expressions like import.meta.glob(someVar) are left untouched. ↓
fix Use a static string literal for the glob pattern.
gotcha Eager imports with { eager: true } require a supporting Babel version that understands import() statements; ensure @babel/core is up to date. ↓
fix Install @babel/core@^7.0.0 and @babel/plugin-syntax-dynamic-import if needed.
deprecated The plugin uses __glob__* variable naming, which may conflict with user code. No change planned. ↓
fix Avoid using __glob__ prefixed variables in your code.
Install
npm install babel-plugin-transform-vite-meta-glob yarn add babel-plugin-transform-vite-meta-glob pnpm add babel-plugin-transform-vite-meta-glob Imports
- default wrong
const plugin = require('babel-plugin-transform-vite-meta-glob')correctimport plugin from 'babel-plugin-transform-vite-meta-glob' - plugin as function wrong
plugins: [require('babel-plugin-transform-vite-meta-glob')]correctplugins: ['babel-plugin-transform-vite-meta-glob'] - TypeScript types
import type { PluginObj } from '@babel/core'
Quickstart
// babel.config.js
module.exports = {
plugins: [
'babel-plugin-transform-vite-meta-glob'
]
};
// src/modules.js
const modules = import.meta.glob('./fixtures/*.js');
// After transformation, modules becomes:
// {
// './fixtures/a.js': () => import('./fixtures/a.js'),
// './fixtures/b.js': () => import('./fixtures/b.js')
// }
export default modules;