Svelte i-Pack
A Rollup plugin for image optimization and auto generation of responsive images for Svelte.
*This package is inspired by Svelte Image. If you want more feature like Lazy loading with auto generated placeholder you should check Svelte Image out.
Features
- Optimize png/jpg
- Generate And Add Webp
- Generate And Add Responsive Images
- Automatically Converts Images With A Small Fils Size To Inline Base64
- Clean Up And Make Sure There Is No Unused Image Left In The Served Folder
Full Example
Will generate
Installation
yarn add -D svelte-i-pack
(requires svelte >= v3)
Svelte
With Simply add iPack() in the rollup plugins before svelte plugin.
// rollup.config.js; ... plugins: ...
Sapper
With Pretty much the same as Svelte.
However, Sapper has two build configurations, one for the client bundle and one for the server.
To use svelte-i-pack
with Sapper, you need to define it on both configurations.
// ...; const iPackOptions = /* options if needed */ client: plugins: server: plugins: ;
Preprocessor Options and Defaults
iPack processor
can receive an options object.
const options = /** * Specify input directory for your original images. * Original ones are simply treated as source and will not be included in the build. * Thus, you should not set it in ./static. **/ inputDir: 'content/img' /** * Jpeg/webp quality level **/ quality: 75 /** * Png quality level **/ pngCompLevel: 8 /** * Images below the threshold will be encoded to base64 and be inlined. * In this case no image file will be generated. **/ inlineThreshold: 5000 /** * If true, logs all an array of created and deleted images. **/ logging: true
Image component props
<Image>
component can recieve props below:
Standard html attributes. -These will be passed as normal attributes.
-
src
(required) -relative and static path only -
class
-
style
-
media
-
alt
-
sizes
When not specified,
sizes
will fall back to the value of the left part of thewidth
- in the example below,160px
.
You cannot pass
width
andheight
. Use inline style or css instead.
iPack attributes.
-
width
(required)This is completely different from the normal html
width
attribute. This prop controls the size and the number of image generated.syntax:
{actual.displayed.size}*{resolution}
example:
width="160*3"
will generate images with size of[160, 320, 480]
for both png/jpg and webp.
-
aSrc
-
aWIdth
-
aSizes
When you specify
media
in art direction scenarios, you can specify theirsrc
,width
,sizes
with these props. (a = alternative) If you don't specifyaSizes
, value of the left part of theaWidth
will be used. -
no-inline
Pass this prop if you don't want the image to be converted to inlined-base64. This should be desirable for the images that get used multiple times in the same page.
If you want to disable inline-img feature globally, set
inline-threshold
in preprocessor option to0
.
Some thoughts
Class and scoped CSS
As you can see above, you can pass as many classes to <Image>
component.
However, since Svelte's style is scoped by default, you need to tweek a little to apply the style you specified in the parent component to the <Image>
component.
- Use
:global
style
Change
to
)
- Use utility-first css library like Tailwind.css
Both works, but using utility-first css should be slightly optimal.
By configuring Tailwind.css correctly you can eliminate the small overhead caused by :global-ing styles.