Babel Plugin Transform Vite Meta Hot
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
A Babel plugin that emulates Vite's import.meta.hot API for non-Vite environments, primarily used in test runners like Jest or Mocha. Version 1.0.0 is the initial stable release (January 2023), with no subsequent updates. It transforms import.meta.hot calls into module.hot equivalents, enabling HMR-like code to run in Node.js. Unlike full Vite presets, this is a focused plugin for HMR simulation only. It ships TypeScript type definitions and is part of the babel-vite suite.
Common errors
error SyntaxError: /path/to/file.js: Support for the experimental syntax 'importMeta' isn't currently enabled ↓
cause Babel parser doesn't recognize import.meta syntax; needs appropriate plugin or preset.
fix
Add '@babel/plugin-syntax-import-meta' or use babel-preset-vite which includes it.
Warnings
gotcha Only transforms import.meta.hot, not other Vite meta properties like import.meta.env or import.meta.glob. ↓
fix Use babel-plugin-transform-vite-meta-env or babel-plugin-transform-vite-meta-glob for those respectively.
gotcha The transformation is an approximation; module.hot may not be defined in all environments, causing runtime errors. ↓
fix Add a conditional check like if (typeof module.hot !== 'undefined') before using the transformed code.
deprecated No active development; last release 1.0.0 over a year ago. Consider using babel-preset-vite for comprehensive Vite compatibility. ↓
fix Switch to babel-preset-vite which includes this plugin and others.
Install
npm install babel-plugin-transform-vite-meta-hot yarn add babel-plugin-transform-vite-meta-hot pnpm add babel-plugin-transform-vite-meta-hot Imports
- default export wrong
const plugin = require('babel-plugin-transform-vite-meta-hot')correctimport plugin from 'babel-plugin-transform-vite-meta-hot'
Quickstart
// Install: npm install --save-dev babel-plugin-transform-vite-meta-hot
// In .babelrc or babel.config.js:
{
"plugins": ["babel-plugin-transform-vite-meta-hot"]
}
// Source code with Vite HMR:
if (import.meta.hot) {
import.meta.hot.accept((newModule) => {
console.log('HMR update');
});
}
// After Babel transform:
if (module.hot) {
module.hot.accept((newModule) => {
console.log('HMR update');
});
}
// Use with Jest: add to jest.config.js transformIgnorePatterns or babel config.