You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[🐞] import.meta.glob either works in dev and doesn't in prod, or works in prod but gets slower and slower as the number of imported files increases in dev
#5498
Open
maiieul opened this issue
Nov 29, 2023
· 3 comments
When using import.meta.glob as is, which means with eager: false as it is the default, Qwik isn't able to understand Vite's new bundling for the server build.
So when I run pnpm preview, I get:
❌ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/maieul/dev/qwik-meta-glob-repro/server/assets/@qwik-city-not-found-paths.js' imported from /home/maieul/dev/qwik-meta-glob-repro/server/assets/entry.preview-27aca4ec.mjs
If now I use eager: true, Qwik is able to create a proper /server output in preview/prod, but then the dev server gets slower and slower as the number of imported files increases.
I thought the problem was simply that when using import.meta.glob, Vite places the dynamic import.meta.glob modules in /server/assets, and so that it could be fixed by putting back all the modules in /server.
in the repro's vite.config.ts, you will see that it works just fine in preview. But on larger codebases, it creates other errors.
Ex from qwik-ui's docs site using the same build.rollupOptions.output config:
Workaround
Thanks to @wmertens, we found that using import { isDev } from '@builder.io/qwik/build'; can workaround this issue.
The idea is to conditionally use eager:false in development, so that it's not slow, and eager: true in preview/prod so that qwik can take care of creating the lazy-load boundaries as it normally does.
You can replace <Glob name="example1" /> with <GlobWorkaround name="example1" /> in /routes/index.tsx to see the workaround working.
Potential ways to fix this
We can potentially fix this in the QwikCity or QwikVite plugins by making sure that the plugins can understand the Vite import.meta.glob output structure. Overall the bugs I'm describing seem to be related to modules not being properly imported, so if Qwik can figure out a way to handle those Vite-created dynamic imports, then this is probably the right fix.
If (1) doesn't work, we can also create an AST transform in the optimizer like it's done for props_destructuring.rs to automatically transform import.meta.glob into the workaround version above. But that sounds more like a patch than a real fix.
This is not the most urgent issue as there is a workaround, but I think this is still an important one to fix as using import.meta. glob can save a lot of development time. It allows using a pattern-matching string to define all the files to import instead of having to manually write bucket files where we have to specify which files to import one by one.
I also tried using dynamic imports, but it didn't work and probably has even more drawbacks than import.meta.glob (e.g. imports such as icons/material/zoom_in.txt?raw cannot work)
I think it would be great to have a guide on lazy-loading in the docs with the workaround documented in it until we have a fix for this. I'll be working on it.
The text was updated successfully, but these errors were encountered:
This is not the most urgent issue as there is a workaround, but I think this is still an important one to fix as using import.meta. glob can save a lot of development time. It allows using a pattern-matching string to define all the files to import instead of having to manually write bucket files where we have to specify which files to import one by one.
TLDR: import.meta.glob can save a lot of dev time/effort.
import.meta.glob is currently usable in Qwik with the workaround, but it is definitely not great DX. I know it's a Vite feature but other developers will want to use it too as it saves dev time/effort. That's why I think it would be great if QwikVite plugin knew how to work with it OOB.
Which component is affected?
Qwik Rollup / Vite plugin
Describe the bug
When using import.meta.glob as is, which means with
eager: false
as it is the default, Qwik isn't able to understand Vite's new bundling for the server build.So when I run
pnpm preview
, I get:If now I use
eager: true
, Qwik is able to create a proper/server
output in preview/prod, but then the dev server gets slower and slower as the number of imported files increases.I thought the problem was simply that when using import.meta.glob, Vite places the dynamic import.meta.glob modules in
/server/assets
, and so that it could be fixed by putting back all the modules in/server
.So if you comment out
in the repro's vite.config.ts, you will see that it works just fine in
preview
. But on larger codebases, it creates other errors.Ex from qwik-ui's docs site using the same
![Screenshot from 2023-11-29 09-59-49](https://private-user-images.githubusercontent.com/45822175/286538043-6b05f312-82af-4bb2-af26-af3b1fbee5bd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0NjIzMDEsIm5iZiI6MTczOTQ2MjAwMSwicGF0aCI6Ii80NTgyMjE3NS8yODY1MzgwNDMtNmIwNWYzMTItODJhZi00YmIyLWFmMjYtYWYzYjFmYmVlNWJkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDE1NTMyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZjOTlkMWRkYmE2ZTAxY2U0ZjQyZWE2OWY4ODdmZDk3OGU4NmM4ZmU5NDE2ZjJhOGUxMTY3NWFlM2JkNTZkY2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.w5o1hwbUrAURHMQeX5Alqu0CgjyWfJteYkg0syFgo74)
build.rollupOptions.output
config:Workaround
Thanks to @wmertens, we found that using
import { isDev } from '@builder.io/qwik/build';
can workaround this issue.The idea is to conditionally use
eager:false
in development, so that it's not slow, andeager: true
in preview/prod so that qwik can take care of creating the lazy-load boundaries as it normally does.Ex:
You can replace
<Glob name="example1" />
with<GlobWorkaround name="example1" />
in/routes/index.tsx
to see the workaround working.Potential ways to fix this
props_destructuring.rs
to automatically transform import.meta.glob into the workaround version above. But that sounds more like a patch than a real fix.Reproduction
https://github.com/maiieul/qwik-meta-glob-repro
Steps to reproduce
To see the bug:
To see the workaround:
<Glob name="example1" />
with<GlobWorkaround name="example1" />
in/routes/index.tsx
.System Info
Additional Information
This is not the most urgent issue as there is a workaround, but I think this is still an important one to fix as using import.meta. glob can save a lot of development time. It allows using a pattern-matching string to define all the files to import instead of having to manually write bucket files where we have to specify which files to import one by one.
I also tried using dynamic imports, but it didn't work and probably has even more drawbacks than import.meta.glob (e.g. imports such as icons/material/zoom_in.txt?raw cannot work)
I think it would be great to have a guide on lazy-loading in the docs with the workaround documented in it until we have a fix for this. I'll be working on it.
The text was updated successfully, but these errors were encountered: