astro icon indicating copy to clipboard operation
astro copied to clipboard

🐛 BUG: Astro/image getImage doesn't work in .ts file

Open nicolasverlhiac opened this issue 3 years ago • 2 comments

What version of astro are you using?

v1.0.0-rc.4

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

yarn

What operating system are you using?

Mac (M1)

Describe the Bug

Hi @tony-sull 👋 (issue following our discussion yesterday)

@astrojs/image: 0.3.0

When is use getImage in a .ts context, and when I run yarn build or yarn dev I get this error message :

You can mark the path "virtual:image-loader" as external to exclude it from the bundle, which will remove this error. You can also add ".catch()" here to handle this failure at run-time instead of bundle-time.

Test it :

import { getImage } from '@astrojs/image';

export async function get() {
  const img = await getImage({
      "src": `https://api.weavescan.dev/storage/uploads/2022/08/02/android-chrome-512x512_uid_62e9856669173.png`,
      "width": 50,
      "height": 50,
      "format": "webp"
    });
    return {
      body: JSON.stringify(
        {"src":img.src} 
      )
  };
 }

Link to Minimal Reproducible Example

N/A

Participation

  • [ ] I am willing to submit a pull request for this issue.

nicolasverlhiac avatar Aug 04 '22 20:08 nicolasverlhiac

Build message :

{
errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 42,
        file: 'node_modules/@astrojs/image/dist/lib/get-image.js',
        length: 22,
        line: 72,
        lineText: '    const { default: mod } = await import("virtual:image-loader");',
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: 'You can mark the path "virtual:image-loader" as external to exclude it from the bundle, which will remove this error. You can also add ".catch()" here to handle this failure at run-time instead of bundle-time.'
        }
      ],
      pluginName: '',
      text: 'Could not resolve "virtual:image-loader"'
    }
  ]
}

nicolasverlhiac avatar Aug 04 '22 20:08 nicolasverlhiac

I have the same error message in a .js flle (and a local image). The astro dev runs fine, however the astro build fails.

oliverpool avatar Aug 07 '22 19:08 oliverpool

Hi,

For information, I redid tests with the following versions : @astrojs/image: 0.3.3 astro: 1.0.2

And the issue persists.

nicolasverlhiac avatar Aug 11 '22 14:08 nicolasverlhiac

Hey @oliverpool and @nicolasverlhiac, just ran into a similar issue. Can you confirm that the image() integration is actually added to your astro.config.mjs file?

https://github.com/withastro/astro/issues/4257#issuecomment-1212502448

natemoo-re avatar Aug 11 '22 21:08 natemoo-re

Hey @natemoo-re, I confirm that I already have these settings in my astro.config.mjs

nicolasverlhiac avatar Aug 11 '22 22:08 nicolasverlhiac

I think I found my issue: I was using getImage at the top of my file, and not inside the export const get = ... function!

oliverpool avatar Aug 12 '22 07:08 oliverpool