Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Position of import.meta.glob causes error #944

Open
1 task
steveoh opened this issue Jan 19, 2024 · 3 comments
Open
1 task

Position of import.meta.glob causes error #944

steveoh opened this issue Jan 19, 2024 · 3 comments
Labels
- P2: has workaround Bug, but has workaround (priority)

Comments

@steveoh
Copy link

steveoh commented Jan 19, 2024

Astro Info

Astro                    v4.2.1
Node                     v20.9.0
System                   macOS (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/sitemap
                         @astrojs/tailwind
                         @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

with the following frontmatter astro throws an error that i is not defined.

---
import type { ImageMetadata } from 'astro';

import Layout from '../layouts/FullWidth.astro';
import Section from '../components/page/Section.astro';
import StaffMember from '../components/page/StaffMember.astro';

import { Image } from 'astro:assets';

const images = import.meta.glob<{ default: ImageMetadata }>('/src/images/pillar-contact/*.svg');

import { staff } from '../data/staff';
---

astro compiles to

import {
  Fragment,
  render as $$render,
  createAstro as $$createAstro,
  createComponent as $$createComponent,
  renderComponent as $$renderComponent,
  renderHead as $$renderHead,
  maybeRenderHead as $$maybeRenderHead,
  unescapeHTML as $$unescapeHTML,
  renderSlot as $$renderSlot,
  mergeSlots as $$mergeSlots,
  addAttribute as $$addAttribute,
  spreadAttributes as $$spreadAttributes,
  defineStyleVars as $$defineStyleVars,
  defineScriptVars as $$defineScriptVars,
  createMetadata as $$createMetadata
} from "astro/runtime/server/index.js";
import type { ImageMetadata } from 'astro';
import Layout from '../layouts/FullWidth.astro';
import Section from '../components/page/Section.astro';
import StaffMember from '../components/page/StaffMember.astro';
import { Image } from 'astro:assets';
import { staff } from '../data/staff';


import * as $$module1 from '../layouts/FullWidth.astro';
import * as $$module2 from '../components/page/Section.astro';
import * as $$module3 from '../components/page/StaffMember.astro';
import * as $$module4 from 'astro:assets';
import * as $$module5 from '../data/staff';

export const $$metadata = $$createMetadata("Card.astro", { modules: [{ module: $$module1, specifier: '../layouts/FullWidth.astro', assert: {} }, { module: $$module2, specifier: '../components/page/Section.astro', assert: {} }, { module: $$module3, specifier: '../components/page/StaffMember.astro', assert: {} }, { module: $$module4, specifier: 'astro:assets', assert: {} }, { module: $$module5, specifier: '../data/staff', assert: {} }], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });

const $$Astro = $$createAstro();
const Astro = $$Astro;
const $$Card = $$createComponent(async ($$result, $$props, $$slots) => {
const Astro = $$result.createAstro($$Astro, $$props, $$slots);
Astro.self = $$Card;


const images = import.meta.glob<{ default: ImageMetadata }>('/src/images/pillar-contact/*.svg');

i

return $$render`wediowqed`;
}, 'Card.astro');
export default $$Card;

If i move the import above, the error goes away.

What's the expected result?

it should work no matter the order

Link to Minimal Reproducible Example

https://live-astro-compiler.vercel.app/?editor-state=WzMxLDEzOSw4LDAsMTA1LDE4OCwxNjksMTAxLDIsMywxMjUsODIsNzcsNzksMjIwLDQ4LDE2LDI1Myw0Myw4Niw0NiwyMTksODYsMTA4LDIyMCwxNzAsMTgzLDI0NCw2Nyw2Niw4NCw4NCw3MiwzMiw4NSwxMzIsMTUwLDMwLDE4NCwyMDQsMjE4LDE0NywxNzMsMzcsMTI3LDE2NCw1MSwxNCw0Myw4OCwyNDEsMjIzLDI1LDExMSwxNTAsNCw4OCw5LDIyOSwxOCwxNDMsMjIzLDEyMywxMjYsMjQzLDEwMiwxODIsMTQ5LDI1LDEzNiw0OCwyMzAsMTQ3LDIwLDEyMiwyMzEsMTQ1LDI1NCwzMiwxNzcsNzUsMTc3LDEwNiwxNzAsNzksMjQ1LDIzMSwyNTAsOTksMTE3LDg0LDE1MywxMDAsODEsMTQyLDIwMywyMjksMjQyLDM4LDE4NiwyMDgsMzksMjAyLDQyLDIyMywyNDUsMTY4LDE4MiwyMzQs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DAsMTk5LDIzMiwxNjQsMzAsNSwxMiwyMjIsMjIxLDE2Myw2MSwxNTcsMTcsODIsMTU5LDE0OCwyMTgsNTIsMTQ0LDE5MywwLDI1MywyMjUsMzUsMTk5LDY5LDIyNywxNjcsMTMyLDYsMjU0LDE1MiwyMTQsMjUyLDEzOCw4OCwxNTAsODIsMTMwLDU3LDE2NCw5MywzNCw3NSwxODgsMTczLDczLDIxMCw5NywyMzUsMjExLDE0MCwxODQsMTE3LDE4NCwyNDksMzMsMTEzLDEzOSwxNyw1OSwxMzAsNjgsMTE1LDc2LDEyOCwyMDksMjAzLDIxMCwxNjAsMTg5LDEyMiwxOCwxODUsMTMwLDE0OSwxODgsMTgzLDkxLDIzOSwxMDMsMTgzLDIzNywyMjMsMTAzLDI0NSwxMzUsNzEsMjksMjE4LDExNiwxOTksMzIsMywwLDBd

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 19, 2024
@ematipico
Copy link
Member

ematipico commented Jan 19, 2024

It was really difficult to understand the error, because you didn't explain where is the error. And "If i move the import above" isn't very clear: above of what?

So, is it correct that the error is that i that appears after the import.meta.glob?

const images = import.meta.glob<{ default: ImageMetadata }>('/src/images/pillar-contact/*.svg');

i // this one

return $$render`wediowqed`;
}, 'Card.astro');
export default $$Card;

@ematipico ematipico transferred this issue from withastro/astro Jan 19, 2024
@steveoh
Copy link
Author

steveoh commented Jan 22, 2024

It was really difficult to understand the error, because you didn't explain where is the error. And "If i move the import above" isn't very clear: above of what?

I'm sorry. Yes the random i is the error and invalid syntax.

updating the frontmatter from

---
import type { ImageMetadata } from 'astro';

import Layout from '../layouts/FullWidth.astro';
import Section from '../components/page/Section.astro';
import StaffMember from '../components/page/StaffMember.astro';

import { Image } from 'astro:assets';

const images = import.meta.glob<{ default: ImageMetadata }>('/src/images/pillar-contact/*.svg');

import { staff } from '../data/staff';
---

to

---
import type { ImageMetadata } from 'astro';

import Layout from '../layouts/FullWidth.astro';
import Section from '../components/page/Section.astro';
import StaffMember from '../components/page/StaffMember.astro';

import { Image } from 'astro:assets';

import { staff } from '../data/staff';

const images = import.meta.glob<{ default: ImageMetadata }>('/src/images/pillar-contact/*.svg');
---

which is moving the const images = import.meta.glob< to the end of the frontmatter removes the invalid compiler generated syntax.

@natemoo-re
Copy link
Member

The temporary solution to this is to keep all the static import statements at the top of the file, and any runtime code (import.meta.glob) below the import statements!

@natemoo-re natemoo-re added the - P2: has workaround Bug, but has workaround (priority) label Feb 15, 2024
@MoustaphaDev MoustaphaDev added - P3: minor bug An edge case that only affects very specific usage (priority) - P2: has workaround Bug, but has workaround (priority) and removed - P2: has workaround Bug, but has workaround (priority) needs triage Issue needs to be triaged - P3: minor bug An edge case that only affects very specific usage (priority) labels Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: has workaround Bug, but has workaround (priority)
Projects
None yet
Development

No branches or pull requests

4 participants