create-foxglove-extension icon indicating copy to clipboard operation
create-foxglove-extension copied to clipboard

Add webpack loader for images

Open jtbandes opened this issue 3 years ago • 3 comments

Our default webpack config is lacking loaders for images. If someone wants to import and use an image file, they get errors like this: https://github.com/foxglove/studio/discussions/3264#discussioncomment-2648824

jtbandes avatar Apr 29 '22 02:04 jtbandes

The simplest way to do this right now is

  1. Enable inline Asset Modules. Webpack will copy the entire contents of the file as a base64-encoded data: URL:

    config.module.rules.push({
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: "asset/inline",
    });
    
  2. Add TypeScript declarations that explain that the type of an imported PNG or whatever is a string constant.

    declare module "*.png" {
        const content: string;
        export default content;
    }
    

That said, I have a proposed change to Studio itself that would support asset/resource instead, where the resource is instead copied into the extension's dist/ directory, and Webpack inserts the URL of the resource.

rgov avatar May 24 '22 16:05 rgov

I am trying to load a PNG file in my custom extension. Is there any fix for this issue?

edit: above mentioned answer by @rgov solved the issue for me.

bnbhat avatar Oct 28 '24 10:10 bnbhat

This example also shows how to do this: https://github.com/foxglove/create-foxglove-extension/blob/main/examples/extension-demo/config.ts

defunctzombie avatar Oct 29 '24 04:10 defunctzombie