Skip to content

Commit ba8b12a

Browse files
authored
docs: update import.meta.glob with eager false 🍒 (#7894)
1 parent 7ebb315 commit ba8b12a

File tree

2 files changed

+10
-30
lines changed
  • packages/docs/src/routes

2 files changed

+10
-30
lines changed

packages/docs/src/routes/demo/cookbook/glob-import/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import {
44
useSignal,
55
useTask$,
66
} from '@builder.io/qwik';
7-
import { isDev } from '@builder.io/qwik';
87

98
const metaGlobComponents: Record<string, any> = import.meta.glob(
109
'/src/routes/demo/cookbook/glob-import/examples/*',
11-
{
12-
import: 'default',
13-
eager: isDev ? false : true,
14-
}
10+
{ import: 'default' }
1511
);
1612

1713
export default component$(() => {
@@ -29,9 +25,7 @@ export const MetaGlobExample = component$<{ name: string }>(({ name }) => {
2925
const componentPath = `/src/routes/demo/cookbook/glob-import/examples/${name}.tsx`;
3026

3127
useTask$(async () => {
32-
MetaGlobComponent.value = isDev
33-
? await metaGlobComponents[componentPath]() // We need to call `await metaGlobComponents[componentPath]()` in development as it is `eager:false`
34-
: metaGlobComponents[componentPath]; // We need to directly access the `metaGlobComponents[componentPath]` expression in preview/production as it is `eager:true`
28+
await metaGlobComponents[componentPath]();
3529
});
3630

3731
return <>{MetaGlobComponent.value && <MetaGlobComponent.value />}</>;

packages/docs/src/routes/docs/cookbook/glob-import/index.mdx

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Cookbook | Glob Import with import.meta.glob
33
contributors:
44
- maiieul
5+
- gioboa
56
---
67

78
import CodeSandbox from '../../../../components/code-sandbox/index.tsx';
@@ -29,27 +30,20 @@ As written in the Vite documentation, `import.meta.glob` comes with a few featur
2930
By default you can simply use pattern matching to specify which files should be imported from which folder:
3031

3132
```tsx
32-
const metaGlobComponents: any = import.meta.glob('/src/components/*');
33+
const metaGlobComponents: Record<string, any> = await import.meta.glob('/src/components/*');
3334
```
3435

35-
But you can also pass in additional options like [`import`](https://vitejs.dev/guide/features#named-imports), [`as`](https://vitejs.dev/guide/features#glob-import-as), or `eager`:
36+
But you can also pass in additional options like [`import`](https://vitejs.dev/guide/features#named-imports), [`query`](https://vite.dev/guide/features.html#custom-queries):
3637

3738
```tsx
38-
const metaGlobComponents: any = import.meta.glob('/src/components/*', {
39+
const metaGlobComponents: Record<string, any> = await import.meta.glob('/src/components/*', {
3940
import: 'default',
40-
as: 'raw',
41-
eager: true, // defaults to false
41+
query: '?raw',
4242
});
4343
```
4444

4545
## How to
4646

47-
The problem with `import.meta.glob` in Qwik, is that it currently either works in development and doesn't in preview/production, or it works in preview/production but gets slower and slower in development as the number of imported files increases.
48-
49-
The reason for this behavior, is that `import.meta.glob` with `eager.false` breaks the production bundle as it creates lazy-loadable chunks that Qwik doesn't know how to handle. On the other hand, `eager:true` seemingly fixes the issue as it allows Qwik to normally bundle the files, but it also slows down the development server - especially when you import a lot of heavy components with it.
50-
51-
As a workaround for now, you can use the build time `isDev` boolean from `"@builder.io/qwik"`:
52-
5347
<CodeSandbox src="/src/routes/demo/cookbook/glob-import/">
5448
```tsx
5549
import {
@@ -58,14 +52,10 @@ import {
5852
useSignal,
5953
useTask$,
6054
} from '@builder.io/qwik';
61-
import { isDev } from '@builder.io/qwik';
6255

63-
const metaGlobComponents: Record<string, any> = import.meta.glob(
56+
const metaGlobComponents: Record<string, any> = await import.meta.glob(
6457
'/src/examples/*',
65-
{
66-
import: 'default',
67-
eager: isDev ? false : true,
68-
}
58+
{ import: 'default' }
6959
);
7060

7161
export default component$(() => {
@@ -83,11 +73,7 @@ export const MetaGlobExample = component$<{ name: string }>(({ name }) => {
8373
const componentPath = `/src/examples/${name}.tsx`;
8474

8575
useTask$(async () => {
86-
MetaGlobComponent.value = isDev
87-
? await metaGlobComponents[componentPath]()
88-
// We need to call `await metaGlobComponents[componentPath]()` in development as it is `eager:false`
89-
: metaGlobComponents[componentPath];
90-
// We need to directly access the `metaGlobComponents[componentPath]` expression in preview/production as it is `eager:true`
76+
MetaGlobComponent.value = await metaGlobComponents[componentPath]();
9177
});
9278

9379
return <>{MetaGlobComponent.value && <MetaGlobComponent.value />}</>;

0 commit comments

Comments
 (0)