Makes a bunch of avif's and webp's from jpeg's, png's and such.
* If you're using Netlify, be sure to enable netlify-plugin-cache
to get the max speeed out of builds!
After installing the package you'll be able to use ssga CLI tool. It's basically a cool-kid's launcher for sharp (image conversion library).
By default the cli looks for config files in project directory. Default config names are: (ssgassets|ssga).config.(json|js|mjs|ts|mts)
, eg. ssga.config.json
.
Custom config files can be specified with --config
argument.
Sample config file:
import type { Config } from '@maddsua/ssg-assets';
export const config: Config = {
verbose: true,
inputDir: './assets',
outputDir: './dist',
formats: ['avif', 'webp', 'jpg'],
exclude: [
'**/vector/*.svg'
]
}
config
(string
): Config file locationverbose
(boolean
): Enables verbose loggingclearCache
(boolean
): Resets asset cacseclearDist
(boolean
): Cleares all files from dist dir before copying new assetsnoCache
(boolean
): Disables asset cacheinputDir
(string
): Input directory pathoutputDir
(string
): Output directory pathssga --config=./tests/convert/ssgassets.config.json --verbose
Having multiple image formats is fun, but placing all the source tags for a picture is not. So here, have these UI framework components:
Don't try to import package's root directly, instead import the subpath for the framework you're using.
Component import path: @maddsua/ssg-assets/[framework]
.
For instance, this is how you import a Picture component for Vue:
import { Picture } from '@maddsua/ssg-assets/vue';
There are cases where you'd want browser to reset it's image cache but a hosting provider does not provide an easy way to do it. The solution here is to change image's url search params so that on each deploy it will be different thus forcing browser to re-download assets.
In order to activate this feature make sure that your bundler replaces string literal __SSGA_DEPLOY_CACHE_HASH__
with a unique deploy identifier. Or a random string.
Here's an example how it's done using Vite when deploying from GitLab:
...
define: {
__SSGA_DEPLOY_CACHE_HASH__: process.env['CI_COMMIT_SHA']?.slice(0, 8)
}
...
Your bundler/compiler may choke on this, so try using this config line for Webpack:
...
transpilePackages: ['@maddsua/ssg-assets']
...
or this one for Vite:
...
ssr: {
noExternal: '@maddsua/ssg-assets'
}
...