0.0.43 • Published 8 months ago

astro-media v0.0.43

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@gvkhna/astro-media 📷

npm version

⚠️ This integration is still experimental/WIP!

Fork of @astro/astro-image designed with more convenience when working with images and vector assets (svgs) in astro. With astro-media you should be able to get 0 CLS (Content Layout Shifts).

Setup

npm i astro-media

astro.config.mjs

import media from "astro-media/integration";

export default defineConfig({
  integrations: [
    // towards the top is better
    media(),
  ],
});

Usage

import Image from 'astro-media'

<Image src={import('asset.png')}>

Support inline asset path

In an attempt to reduce the following code

import MyImage from "images/asset.img";
  <img src={MyImage} ...>

Instead you can inline paths so you don't need to track so many variable names in addition to filenames

import Image from 'astro-media'

<Image src={import('images/asset.img')}>

The file will get resolved as part of the astro asset pipeline and be given a hashed filename, etc.

Auto fill size attributes

<Image src={import('myimage.png')}>

Missing attributes (width,height) will get filled in:

<img src="myimage-ocniwroconwroiu.png" width="3242" height="4460" />

So the browser knows the exact size of the image ahead of time instead of waiting for the image to load and shifting the layout of the page while rendering.

Scale image automatically

<Image src={import("myimage.png")} width="320" />

This will aspect-ratio scale the image automatically to a width of 320 and output the final height in the produced html

<img src="image.img" width="320" height="480" />

Inline SVG

Self explanatory but for many tiny svg files the file will be read, inlined and attributed correctly to reduce CLS.

<Image src={import('icon.svg')}>

No custom types as opposed to @astro/image

As part of the build system the plugin will annotate the import url with it's metadata in import meta format. As part of communicating build metadata to the Astro component at build/runtime. This is powerful and allows for handling of this metadata without any custom setup to your project.

<Image src={import('myimage.png')}>

When the Astro component Image is initialized, it sees the following url

myimage.png?width=180&height=220&format=png&size=3200&...

All of this metadata is then interpreted so if you have specified one attribute such as only the width, it will use the metadata to aspect ratio scale the image to that width.

WIP Status

The core features described work at this time, no testing...

Planned to add better support for Picture component so additional formats such as webp, avif are automatically generated.

As well multiple resolutions are automatically generated for larger images and specified as src-set in the result .

Building

pnpm build
npm publish

Debugging

Run this command to see some internal output in case you encounter errors. DEBUG=astro-media astro dev

Releasing

Bump the version number. Create a new Github Release which will trigger the workflow.

Contributing

You're welcome to submit an issue or PR!

Changelog

See CHANGELOG.md for a history of changes to this integration.

0.0.40

9 months ago

0.0.41

9 months ago

0.0.42

9 months ago

0.0.43

8 months ago

0.0.37

9 months ago

0.0.38

9 months ago

0.0.39

9 months ago

0.0.30

9 months ago

0.0.31

9 months ago

0.0.32

9 months ago

0.0.33

9 months ago

0.0.34

9 months ago

0.0.35

9 months ago

0.0.36

9 months ago

0.0.28

10 months ago

0.0.29

9 months ago

0.0.20

11 months ago

0.0.21

11 months ago

0.0.22

11 months ago

0.0.23

11 months ago

0.0.24

11 months ago

0.0.25

11 months ago

0.0.19

11 months ago

0.0.26

11 months ago

0.0.27

11 months ago

0.0.18

11 months ago

0.0.17

11 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

11 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago