Skip to content

Commit 8abca4b

Browse files
authored
fix: Add support for eager via the { eager: true } argument to import.meta.glob in Jest (#51)
1 parent 112381c commit 8abca4b

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

packages/babel-plugin-transform-vite-meta-glob/src/__tests__/__snapshots__/index.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const modules = import.meta.glob("./fixtures/**/*", { eager: true })
66
77
↓ ↓ ↓ ↓ ↓ ↓
88
9-
import * as __glob__0_0 from './fixtures/file1.ts'
10-
import * as __glob__0_1 from './fixtures/file2.ts'
11-
import * as __glob__0_2 from './fixtures/file3.ts'
9+
const __glob__0_0 = require('./fixtures/file1.ts')
10+
const __glob__0_1 = require('./fixtures/file2.ts')
11+
const __glob__0_2 = require('./fixtures/file3.ts')
1212
const modules = {
1313
'./fixtures/file1.ts': __glob__0_0,
1414
'./fixtures/file2.ts': __glob__0_1,
@@ -113,8 +113,8 @@ const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: true })
113113
114114
↓ ↓ ↓ ↓ ↓ ↓
115115
116-
import * as __glob__0_0 from './fixtures/file1.ts'
117-
import * as __glob__0_1 from './fixtures/file3.ts'
116+
const __glob__0_0 = require('./fixtures/file1.ts')
117+
const __glob__0_1 = require('./fixtures/file3.ts')
118118
const modules = {
119119
'./fixtures/file1.ts': __glob__0_0,
120120
'./fixtures/file3.ts': __glob__0_1

packages/babel-plugin-transform-vite-meta-glob/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,13 @@ export default function viteMetaGlobBabelPlugin({
9191
const identifiers = globPaths.map((_, idx) => t.identifier(`__glob__0_${idx}`))
9292

9393
const imports = globPaths.map((globPath, idx) => {
94-
const specifier = t.importNamespaceSpecifier(identifiers[idx])
9594
const modulePath = t.stringLiteral(globPath)
96-
return t.importDeclaration([specifier], modulePath)
95+
return t.variableDeclaration('const', [
96+
t.variableDeclarator(
97+
identifiers[idx],
98+
t.callExpression(t.identifier('require'), [modulePath])
99+
)
100+
]);
97101
})
98102

99103
const variable = t.variableDeclaration('const', [

0 commit comments

Comments
 (0)