withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.43k stars 2.46k forks source link

🐛 BUG: `?raw` doesn't work with import.meta.glob() and import.meta.globEager() #3531

Closed felixsanz closed 2 years ago

felixsanz commented 2 years ago

Edited the issue because the problem was related, so feel free to ignore this message and jump directly to my third message.

What version of astro are you using?

v1.0.0-beta.40

Are you using an SSR adapter? If so, which one?

No

What package manager are you using?

npm

What operating system are you using?

Linux

Describe the Bug

import.meta.glob works fine, but when using import.meta.globEager i get this error:

error   `line` must be greater than 0 (lines start at line 1)

I created this test case but it doesn't work on codesandbox because import.meta doesn't work there, but it's a reproducible minimal example.

Link to Minimal Reproducible Example

https://codesandbox.io/s/priceless-mayer-rhmb7c?file=/src/i18n.ts

Participation

felixsanz commented 2 years ago

I forgot to use ?raw when reading text files, sorry!

felixsanz commented 2 years ago

Reopening because passing ?raw to import.meta.glob or import.meta.globEager doesn't work, it returns an empty object.

const imports = import.meta.glob('../components/**/*.ftl')
console.log(imports)

/*
{
  '../components/ui/About/en.ftl': [Function: ../components/ui/About/en.ftl],
  '../components/ui/About/es.ftl': [Function: ../components/ui/About/es.ftl]
}
*/
const imports = import.meta.glob('../components/**/*.ftl?raw')
console.log(imports)

/*
{}
*/

So.. if using ?raw, nothing is returned. If not using it,... obviously it crash.

?raw works fine when used with import, but not with glob/globEager

natemoo-re commented 2 years ago

Hmm interesting, I wonder if there's still a Vite transformation happening here. Definitely a suspicious edge case in one of our Vite plugins.

sibbng commented 2 years ago

This is intentional. Check https://github.com/vitejs/vite/issues/7017 for more info.

Provide the raw query like this:

const modules = import.meta.glob('./dir/*.js', { as: 'raw' })

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

felixsanz commented 2 years ago

it works! thanks