OpenSourceRaidGuild / babel-vite

babel preset and plugins that emulate Vite's non-standard functionality
MIT License
60 stars 13 forks source link

`import.meta.glob` not transformed when options are passed #42

Open ppkn opened 1 year ago

ppkn commented 1 year ago

Relevant code or config:

// working 👍 
Object.values(import.meta.glob("src/images/*.jpg"))

// not working 👎 
Object.values(import.meta.glob("src/images/*.jpg", { eager: true, as: "url" }) // or any options parameter value

What you did:

What happened:

This error occurs when attempting to run tests

SyntaxError: Cannot use 'import.meta' outside a module

Reproduction:

TBD (I've got a big application and I'm not in a place to strip it down to the essentials right now)

Problem description:

It looks like import.meta.glob doesn't get transformed when a second argument it passed.

Suggested solution:

I'm not familiar enough with babel plugins, but if we can specify optional arguments when doing the transformation I'm guessing that will solve it. Not sure how involved we want to make the transformation though. It might be a lot to replicate what Vite does with import.meta.glob with options.

jazlalli commented 1 year ago

Running into this same issue myself, when supplying the 2nd argument.

Seems like that usage isn't supported by this plugin (only 1 string arg in the patterns 👇🏽), so it would need to be implemented in order to support this usage.

https://github.com/OpenSourceRaidGuild/babel-vite/blob/883dabde33acfccc863e7356b6e7bc1007a8cc55/packages/babel-plugin-transform-vite-meta-glob/src/index.ts#L9-L13

I can attempt a PR that does this, but no guarantees that a) i'll get it working and b) on when I'll find the time.

vctqs1 commented 10 months ago

I also got same issue

krybinski commented 10 months ago

I encountered the same problem

vctqs1 commented 10 months ago

@krybinski hi, the new version already publish https://github.com/OpenSourceRaidGuild/babel-vite/pull/51#issuecomment-1869427211

CanaryWharf commented 1 month ago

I'm still having the issue. Seems there's still a check where it only transforms if number of args is 1.

I think the issue is here

https://github.com/OpenSourceRaidGuild/babel-vite/blob/1d2192dc5683b808a08ae76a0d7f225e4724c4b6/packages/babel-plugin-transform-vite-meta-glob/src/index.ts#L41

estberg commented 1 month ago

I am not sure if this is the only issue people are still experiencing, but an issue we are running into is that the above mentioned PRs only addressed the eager option.

The import option for named imports and the query option for custom queries do not seem to be handled yet and remain open issues that would ideally be handled as part of this.