babel-vite icon indicating copy to clipboard operation
babel-vite copied to clipboard

Inconsistent import.meta.glob behaviour

Open isneezy opened this issue 2 years ago • 0 comments

  • babel-preset-vite version: 1.0.4
  • babel-plugin-transform-vite-meta-env version: 1.0.3
  • babel-plugin-transform-vite-meta-glob version: 1.0.3
  • @babel/core version: 7.18.9
  • node version: v14.19.3
  • yarn version: 1.22.19

Relevant code or config:

Reproducible repo: https://codesandbox.io/s/beautiful-roman-sjjvdg?file=/test/Posts.spec.tsx

What you did:

I was trying to unit test my vite, react, typescript project which has a working file based routing, done by leveraging the import.meta.glob feature. I used this library to replace the occurrence of import.meta.glob, but it is not behaving like vite's implementation since it is not supported outside the vite environment, jest in this case.

What happened:

While the code is working well with vite (dev server and production build), under tests it is always returning an empty object unless the glob pattern is changed to be relative to the test file that will import it.

Reproduction:

Reproducible repo: https://codesandbox.io/s/beautiful-roman-sjjvdg?file=/test/Posts.spec.tsx

Problem description:

  • It seems like the glob implementation of this library doesn't take the root project path as the root of the glob search
  • The search is done relative to the root script/the script that used the import.meta.glob instead of being relative to the script that used import.meta.glob.

Suggested solution:

  • if the glob pattern starts with / the glob pattern must be applied to the project directory instead of the root of the file system.
  • if the glob pattern starts with . the pattern must be applied to the folder where the file that declared import.meta.glob resides instead of the file that imported, but this isn't important since vite doesn't support this yet.

isneezy avatar Jul 26 '22 11:07 isneezy