Skip to content

Commit

Permalink
fix: Add support for eager via the { eager: true } argument to import…
Browse files Browse the repository at this point in the history
….meta.glob in Jest (#51)
  • Loading branch information
vctqs1 committed Dec 26, 2023
1 parent 112381c commit 8abca4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const modules = import.meta.glob("./fixtures/**/*", { eager: true })
↓ ↓ ↓ ↓ ↓ ↓
import * as __glob__0_0 from './fixtures/file1.ts'
import * as __glob__0_1 from './fixtures/file2.ts'
import * as __glob__0_2 from './fixtures/file3.ts'
const __glob__0_0 = require('./fixtures/file1.ts')
const __glob__0_1 = require('./fixtures/file2.ts')
const __glob__0_2 = require('./fixtures/file3.ts')
const modules = {
'./fixtures/file1.ts': __glob__0_0,
'./fixtures/file2.ts': __glob__0_1,
Expand Down Expand Up @@ -113,8 +113,8 @@ const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: true })
↓ ↓ ↓ ↓ ↓ ↓
import * as __glob__0_0 from './fixtures/file1.ts'
import * as __glob__0_1 from './fixtures/file3.ts'
const __glob__0_0 = require('./fixtures/file1.ts')
const __glob__0_1 = require('./fixtures/file3.ts')
const modules = {
'./fixtures/file1.ts': __glob__0_0,
'./fixtures/file3.ts': __glob__0_1
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-plugin-transform-vite-meta-glob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ export default function viteMetaGlobBabelPlugin({
const identifiers = globPaths.map((_, idx) => t.identifier(`__glob__0_${idx}`))

const imports = globPaths.map((globPath, idx) => {
const specifier = t.importNamespaceSpecifier(identifiers[idx])
const modulePath = t.stringLiteral(globPath)
return t.importDeclaration([specifier], modulePath)
return t.variableDeclaration('const', [
t.variableDeclarator(
identifiers[idx],
t.callExpression(t.identifier('require'), [modulePath])
)
]);
})

const variable = t.variableDeclaration('const', [
Expand Down

0 comments on commit 8abca4b

Please sign in to comment.