babel-plugin-import-meta.cjs850 BView on GitHub // Babel plugin: transforms `import.meta.env.X` → `(globalThis.__importMeta__ || {}).env?.X`
// at parse time so jsdom-based jest tests can run code that uses Vite's
// `import.meta.env` without a real Vite environment.
//
// Lives in its own file (rather than inline in jest.config.cjs) because
// babel-jest's `loadPartialConfigSync` rejects inline function plugins when
// computing the per-file cache key — passing a string path bypasses that.
module.exports = function importMetaBabelPlugin() {
return {
name: 'transform-import-meta-env',
visitor: {
/** @param {any} path */
MetaProperty(path) {
const { meta, property } = path.node;
if (meta.name === 'import' && property.name === 'meta') {
path.replaceWithSourceString('(globalThis.__importMeta__ || {})');
}
},
},
};
};