withastro / compiler

The Astro compiler. Written in Go. Distributed as WASM.
Other
500 stars 59 forks source link

Position of import.meta.glob causes error #944

Open steveoh opened 10 months ago

steveoh commented 10 months ago

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=WzMxLDEzOSw4LDAsMTA1LDE4OCwxNjksMTAxLDIsMywxMjUsODIsNzcsNzksMjIwLDQ4LDE2LDI1Myw0Myw4Niw0NiwyMTksODYsMTA4LDIyMCwxNzAsMTgzLDI0NCw2Nyw2Niw4NCw4NCw3MiwzMiw4NSwxMzIsMTUwLDMwLDE4NCwyMDQsMjE4LDE0NywxNzMsMzcsMTI3LDE2NCw1MSwxNCw0Myw4OCwyNDEsMjIzLDI1LDExMSwxNTAsNCw4OCw5LDIyOSwxOCwxNDMsMjIzLDEyMywxMjYsMjQzLDEwMiwxODIsMTQ5LDI1LDEzNiw0OCwyMzAsMTQ3LDIwLDEyMiwyMzEsMTQ1LDI1NCwzMiwxNzcsNzUsMTc3LDEwNiwxNzAsNzksMjQ1LDIzMSwyNTAsOTksMTE3LDg0LDE1MywxMDAsODEsMTQyLDIwMywyMjksMjQyLDM4LDE4NiwyMDgsMzksMjAyLDQyLDIyMywyNDUsMTY4LDE4MiwyMzQsNDQsMTkyLDI2LDQ3LDQ4LDEzMSwxMzMsMTIsMjM0LDY1LDExNywxNDgsMTMwLDkwLDAsMTAzLDc0LDEzOSw0Nyw1NSwxMTMsMTMwLDE1OSwxOTUsOTMsMjYsMjQyLDI1NCwxODYsMTc0LDE4MSwyMjMsMTU3LDg5LDE1OSwxNCwyMjIs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TMwLDU3LDE2NCw5MywzNCw3NSwxODgsMTczLDczLDIxMCw5NywyMzUsMjExLDE0MCwxODQsMTE3LDE4NCwyNDksMzMsMTEzLDEzOSwxNyw1OSwxMzAsNjgsMTE1LDc2LDEyOCwyMDksMjAzLDIxMCwxNjAsMTg5LDEyMiwxOCwxODUsMTMwLDE0OSwxODgsMTgzLDkxLDIzOSwxMDMsMTgzLDIzNywyMjMsMTAzLDI0NSwxMzUsNzEsMjksMjE4LDExNiwxOTksMzIsMywwLDBd

Participation

ematipico commented 10 months ago

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;
steveoh commented 10 months ago

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 commented 9 months ago

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!