svelte-image

Image (pre)processing with Sharp for Svelte

  • 所有者: matyunya/svelte-image
  • 平台:
  • 许可证: Apache License 2.0
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Svelte Image

Demo

Svelte image is a preprocessor which automates image optimization using sharp.

It parses your img tags, optimizes or inlines them and replaces src accordingly. (External images are not optimized.)

Image component enables lazyloading and serving multiple sizes via srcset.

This package is heavily inspired by gatsby image.

Kudos to @jkdoshi for great video tutorial to Svelte Image.

Installation

yarn add svelte-image -D

svelte-image needs to be added as dev dependency as Svelte requires original component source

In your rollup.config.js add image to preprocess section:

import image from "svelte-image";


svelte({
  preprocess: {
    ...image(),
  }
})

And have fun!

<script>
  import Image from "svelte-image";
</script>

<Image src="fuji.jpg">

Will generate

<img
  src="data:image/png;base64,/9j/2wBDAAYEBQYFBAYG...BwYIChAKCgkJChQODwwQF"
  alt="fuji">
<img
  alt="fuji"
  sizes="(max-width: 1000px) 100vw, 1000px"
  srcset="g/400-fuji.jpg 375w, g/800-fuji.jpg 768w, g/1200-fuji.jpg 1024w"
>

Image path

Please note that the library works only with relative paths in Sapper at the moment.
<Image src="images/fuji.jpg"> works whereas <Image src="/images/fuji.jpg"> doesn't.

Svelte + Rollup

To use without Sapper, the generated static/g folder needs to be copied to the public folder. Use rollup-plugin-copy in rollup.config.js:

import svelte from 'rollup-plugin-svelte'
import image from 'svelte-image'
import copy from 'rollup-plugin-copy'

export default {
  ...
  plugins: [
    ...
    svelte({
      ...
      preprocess: {
        ...image({...})
      }
    }),
    copy({
      targets: [{ src: 'static/g', dest: 'public' }],
    }),
  ]
}

Configuration and defaults

Image accepts following configuration object:

const defaults = {
  optimizeAll: true, // optimize all images discovered in img tags

  // Case insensitive. Only files whose extension exist in this array will be
  // processed by the <img> tag (assuming `optimizeAll` above is true). Empty
  // the array to allow all extensions to be processed. However, only jpegs and
  // pngs are explicitly supported.
  imgTagExtensions: ['jpg', 'jpeg', 'png'],

  // Same as the above, except that this array applies to the Image Component.
  // If the images passed to your image component are unknown, it might be a
  // good idea to populate this array.
  componentExtensions: [],

  inlineBelow: 10000, // inline all images in img tags below 10kb

  compressionLevel: 8, // png quality level

  quality: 70, // jpeg/webp quality level

  tagName: "Image", // default component name

  sizes: [400, 800, 1200], // array of sizes for srcset in pixels

  // array of screen size breakpoints at which sizes above will be applied
  breakpoints: [375, 768, 1024],

  outputDir: "g/",

  // should be ./static for Sapper and ./public for plain Svelte projects
  publicDir: "./static/",

  placeholder: "trace", // or "blur",

  // WebP options [sharp docs](https://sharp.pixelplumbing.com/en/stable/api-output/#webp)
  webpOptions: {
    quality: 75,
    lossless: false,
    force: true
  },

  webp: true,

  // Potrace options for SVG placeholder
  trace: {
    background: "#fff",
    color: "#002fa7",
    threshold: 120
  }

  // Wheter to download and optimize remote images loaded from a url
  optimizeRemote: true,
};

Image component props

Standard image tag props.

  • class default: ""

  • alt default: ""

  • width default: ""

  • height default: ""

  • c default: "" Class string // deprecated in favor of class

  • wrapperClass default: "" Classes passed to Waypoint wrapper

  • placeholderClass default: "" Classes passed to placeholder

  • threshold default: 1.0 "A threshold of 1.0 means that when 100% of the target is visible within the element specified by the root option, the callback is invoked."

  • lazy default: true Disables Waypoint.

Following props are filled by preprocessor:

  • src default: ""
  • srcset default: ""
  • srcsetWebp default: ""
  • ratio default: "100%"
  • blur default: false
  • sizes default: "(max-width: 1000px) 100vw, 1000px"

Features

  • Generate and add responsive images
  • Set base64 placeholder
  • Optimize normal images using img tag
  • Image lazy loading
  • Optional SVG trace placeholder
  • Support WebP
  • Optimize background or whatever images found in CSS
  • Resolve imported images (only works with string pathnames at the moment)

Development

Run yarn && yarn dev in the /dev directory. This is the source code of demo homepage.

Testing

You can test the preprocessor via yarn test. We are using Jest for that, so you can also pass a --watch flag to test while developing.

Currently, the best way to test the Svelte component is by using it in a separate project and using yarn/npm link. The dev directory tends to have issues keeping in sync with changes to the src in the root of the repo.

主要指标

概览
名称与所有者matyunya/svelte-image
主编程语言JavaScript
编程语言HTML (语言数: 5)
平台
许可证Apache License 2.0
所有者活动
创建于2019-07-07 01:08:07
推送于2023-01-06 02:14:33
最后一次提交2021-06-20 13:22:36
发布数10
最新版本名称v0.2.7 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数722
关注者数6
派生数45
提交数208
已启用问题?
问题数67
打开的问题数36
拉请求数54
打开的拉请求数23
关闭的拉请求数17
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?