-2

This returns all png files:

const files = import.meta.glob('/static/123456789/*.png')
console.log('files: ', files)

But this returns file: {}:

const dirname = import.meta.env.VITE_DIRNAME
console.log(dirname) // this outputs 123456789
const files = import.meta.glob(`/static/${dirname}/*.png`)
console.log('files: ', files)

I tried:

if (dirname === '123456789') {
    console.log('yes')
} else {
    console.log('no')
}

And it returns yes.

I also tried:

const mydir = '123456789'
const files = import.meta.glob(`/static/${mydir}/*.png`)
console.log('files: ', files)

And this returns file: {}.

How can I use variable in the argument?

shin
  • 31,901
  • 69
  • 184
  • 271

1 Answers1

2

This is not possible to do, see the documentation of Vite:

| You should also be aware that glob imports do not accept variables, you need to directly pass the string pattern.

https://vitejs.dev/guide/features.html#glob-import

Stephane Vanraes
  • 14,343
  • 2
  • 23
  • 41